Lecture:波哥
Java
是第一大编程语言和开发平台。它有助于企业降低成本、缩短开发周期、推动创新以及改善应用服务。如今全球有数百万开发人员运行着超过 51 亿个 Java
虚拟机,Java
仍是企业和开发人员的首选开发平台。
隐藏对象的属性和方法的实现,仅对外提供公共访问的方式。
特点:
封装的步骤:
private关键字是一个访问权限修饰符
private关键字的特点:
private的使用:
package com.bobo.oop02;
public class OOPDemo04 {
public static void main(String[] args) {
User user = new User();
// user.userName="张三";
// user.age = -10;
user.setUserName("李四");
user.setAge(22);
//System.out.println(user.userName + "\t" + user.age );
System.out.println(user.getAge() + "\t" + user.getUserName());
}
}
/**
* 在Java类中
* 类体以内及方法体以外 我们是不能写逻辑代码的!
* @author dpb
*
*/
class User{
// 外部不能访问,但是本类中是可以访问的
private String userName;
private int age;
/*if(){
}*/
/**
* 提供给外界用来修改userName的setter方法
* @param name
*/
public void setUserName(String name){
userName = name;
}
/**
* 提供给外界用来修改age信息的方法
* @param a
*/
public void setAge(int a){
if(a < 150 && a > 0){
age = a;
}else{
System.out.println("请输入一个合法的age信息:" + a);
}
}
/**
* 提供给外界用来获取userName属性的方法
* @return
*/
public String getUserName(){
return userName;
}
/**
* 提供给外界用来获取age属性的方法
* @return
*/
public int getAge(){
return age;
}
}
代码演示封装一个苹果手机类
package com.bobo.oop02;
public class OOPDemo05 {
/**
* 代码演示封装一个苹果手机类
* @param args
*/
public static void main(String[] args) {
IPhone phone = new IPhone();
phone.setBrand("ipone12Plus");
phone.setColor("中国红");
phone.setPrice(6666.6);
System.out.println(phone.getBrand() + "\t"
+ phone.getColor() + "\t"
+ phone.getPrice());
}
}
class IPhone{
private String brand; // 品牌
private String color; // 颜色
private double price; // 价格
public void setBrand(String b){
brand = b;
}
public void setColor(String c){
color = c;
}
public void setPrice(double p){
price = p;
}
public String getBrand(){
return brand;
}
public String getColor(){
return color;
}
public double getPrice(){
return price;
}
}
this:代表当前类的对象引用,其实这个this
和我们现实生活中的很多案例相似,比如: 每个人都有一个名字,张三、 李四、 王五,代词,你,我,他等,this
就相当于 我
为什么要使用this:
this的本质就是一个对象,引用,实例,变量。只不过和创建的对象指向了同一个块堆区的空间,
使用this对堆的空间做了修改那么一样会修改对象本身。
应用场景:
通过Eclipse工具块封装相关的属性
在对应的class文件中 鼠标右击,选择 sources --> Generate Getters and Setters
也可以通过快捷键 shift + alt + s
class Person{
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
构造方法的作用:能够在对象创建之后对对象的成员变量快速的赋值
普通方法的语法格式
[访问权限修饰符] 返回类型 方法名(参数列表){
方法体;
return 返回值;
}
构造方法的语法格式
[访问权限修饰符] 类名(参数列表){
方法体;
}
构造方法的特点:
void
关键字也没有通过Eclipse快速生成构造方法
选择要添加进构造器中的字段
小结:一个最基本的类,书写应该具体的特点
class StudentBean{
// 成员变量
private int id;
private String name;
// 构造方法
public StudentBean(int id, String name) {
this.id = id;
this.name = name;
}
public StudentBean() {
}
// 普通的Getter/Setter方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
1、定义一个长方形类,定义 求周长和面积的方法,然后定义一个测试类,进行测试。
package com.bobo.oop03;
/**
* 定义一个长方形类,定义 求周长和面积的方法,然后定义一个测试类,进行测试。
* @author dpb
*
*/
public class OOPDemo01 {
public static void main(String[] args) {
Rectangle rec = new Rectangle(5, 3);
System.out.println("长方形的周长是:" + rec.getPermeter());
System.out.println("长方形的面积是:" + rec.getArea());
}
}
class Rectangle{
// 定义属性 长度和宽度
private int length;
private int width;
public int getLength() {
return length;
}
public Rectangle() {
super();
}
public Rectangle(int length, int width) {
super();
this.length = length;
this.width = width;
}
/**
* 获取周长的方法
*/
public int getPermeter(){
return this.length*2 + this.width*2;
}
/**
* 获取面积的方法
* @return
*/
public int getArea(){
return this.length*this.width;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
}
2、封装一个学生类,有姓名,有年龄,有性别,有英语成绩,数学成绩,语文成绩,封装方法,求总分,平均分,以及打印学生的信息。
package com.bobo.oop03;
public class OOPDemo02 {
/**
* 封装一个学生类,有姓名,有年龄,有性别,有英语成绩,数学成绩,语文成绩,封装方法,求总分,平均分,以及打印学生的信息。
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student stu = new Student("张三", 18, "男", 88, 92, 96);
System.out.println(stu.getName() + "的总分:" + stu.getTotalScore());
System.out.println(stu.getName() + "的平均分:" + stu.getAvgScore());
stu.printStudentInfo();
}
}
class Student{
private String name;
private int age;
private String gender;
private double englishScore;
private double mathScore;
private double chineseScore;
public Student() {
super();
}
public Student(String name, int age, String gender, double englishScore, double mathScore, double chineseScore) {
super();
this.name = name;
this.age = age;
this.gender = gender;
this.englishScore = englishScore;
this.mathScore = mathScore;
this.chineseScore = chineseScore;
}
/**
* 获取总分
* @return
*/
public double getTotalScore(){
return this.chineseScore+ this.englishScore + this.mathScore;
}
/**
* 获取平均分
* @return
*/
public double getAvgScore(){
return this.getTotalScore()/3;
}
/**
* 打印学生信息
*/
public void printStudentInfo(){
System.out.println("name=" + this.name + "\t age=" + this.age
+"\t gender" + gender + "\t englishScore=" + this.englishScore
+"\t mathScore=" + this.mathScore + "\t chineseScore=" + chineseScore);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
public double getChineseScore() {
return chineseScore;
}
public void setChineseScore(double chineseScore) {
this.chineseScore = chineseScore;
}
}
3、定义一个“点”(Point)x y set get类用来表示二维空间中的点。要求如下: A 可以生成具有特定坐标的点对象。 B 提供可以设置坐标的方法。 C 提供可以计算该点距离另一点距离的方法。 D 提供可以计算三个点构成图形的面积的方法。 面积可以使用海伦公式:边长分别为a,b,c p=(a+b+c)/2 s=Math.sqrt(p*(p-a)(p-b)(p-c))
package com.bobo.oop03;
public class OOPDemo03 {
/**
* 3、定义一个“点”(Point)x y set get类用来表示二维空间中的点。要求如下:
A 可以生成具有特定坐标的点对象。
B 提供可以设置坐标的方法。
C 提供可以计算该点距离另一点距离的方法。
D 提供可以计算三个点构成图形的面积的方法。
面积可以使用海伦公式:边长分别为a,b,c p=(a+b+c)/2
s=Math.sqrt(p*(p-a)*(p-b)*(p-c))
* @param args
*/
public static void main(String[] args) {
Point p1 = new Point(1,1);
Point p2 = new Point(2,1);
Point p3 = new Point(2,2);
System.out.println("p1和p3之间的距离是:" + p1.getDistance(p3));
System.out.println("p1、p2、p3三点之间构成的图案的面积是:" + p1.getArea(p1, p2, p3));
}
}
class Point{
private double x;
private double y;
public Point() {
super();
}
// A 可以生成具有特定坐标的点对象。
public Point(double x, double y) {
super();
this.x = x;
this.y = y;
}
/**
* 获取两个点之间的距离
* 计算当前点和另外一个点之间的距离
* @return
*/
public double getDistance(Point p){
double _x = this.x - p.getX();
double _y = this.y - p.getY();
Math.pow(_x, 2);
//return Math.sqrt(_x*_x + _y*_y);
return Math.sqrt(Math.pow(_x, 2)+Math.pow(_y, 2));
}
/**
* 计算任意两个点之间的距离
* @param p1
* @param p2
* @return
*/
public double getDistance(Point p1,Point p2){
double _x = p1.x - p2.getX();
double _y = p1.y - p2.getY();
Math.pow(_x, 2);
//return Math.sqrt(_x*_x + _y*_y);
return Math.sqrt(Math.pow(_x, 2)+Math.pow(_y, 2));
}
/**
* 提供可以计算三个点构成图形的面积的方法。
面积可以使用海伦公式:边长分别为a,b,c p=(a+b+c)/2
s=Math.sqrt(p*(p-a)*(p-b)*(p-c))
* @return
*/
public double getArea(Point p1,Point p2,Point p3){
double a = getDistance(p1, p2);
double b = getDistance(p3, p2);
double c = getDistance(p1,p3);
double p = (a+b+c)/2;
return Math.sqrt(p*(p-a)*(p-b)*(p-c));
}
public double getX() {
return x;
}
public double getY() {
return y;
}
//B 提供可以设置坐标的方法。
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
}
static修饰的变量我叫静态变量/共享变量/类变量
static的特点:
使用"{}"包裹的就是代码块
定义在类的局部位置,作用是:限定局部变量的作用域
public void show(){
{
// 局部代码块
int i = 10;
System.out.println(i);
}
{
// System.out.println(i);
}
System.out.println("name=" + name + " country=" + country);
}
定义在类的成员变量的位置,用来抽取多个构造方法重复的代码,做成员变量的初始化操作
会优先于构造方法执行
{
// 构造代码块 执行顺序在构造方法之前执行
System.out.println("构造代码块....");
}
static修饰的代码块就称为静态代码块
作用:一般用于初始化静态成员变量
静态代码块只会执行一次,在类加载的时候执行
static{
// 静态代码块
System.out.println("静态代码块执行了...");
country = "中国";
}
这三者的执行顺序:
静态代码块 > 构造代码块 > 构造方法
静态代码块只执行一次,在类加载的时候
构造代码块和构造方法在类对象创建的时候执行,可以执行很多次
包
(package)的本质就是文件夹
格式
package 包名; // 包名表示的是当前这个类 所处的文件夹路径
有了这个路径我们就可以将当前类供其他类来使用
在使用其他类的功能使用有几个要注意点
import语法格式
import 类的全路径;
包名:满足标识符的规则和规范即可
注意事项:
//
package com.bobo.oop04;
import com.bobo.oop05.*;
/*import com.bobo.oop05.Student;
import com.bobo.oop05.User;*/
public class OOPDemo01 {
public static void main(String[] args) {
UserBean user = new UserBean();
user.run();
// java.lang包的类型可以直接使用
String s = "...";
/*com.bobo.oop05.Student stu = new com.bobo.oop05.Student();
com.bobo.oop03.OOPDemo01 d1 = new com.bobo.oop03.OOPDemo01();*/
Student stu = new Student();
User u = new User();
// 我们需要使用到 oop06下面的 Student 类型
com.bobo.oop06.Student s2 = new com.bobo.oop06.Student();
}
}
在弹出窗口中选择 java–>javadoc
如果出现了编码出错的情况
-encoding utf-8 -charset utf-8
生成成功