01. 封装之private关键字
-
private 概述
- 使用private修饰属性和行为,那么该属性和行为只能在本类中访问
-
需求
- 人类年龄问题
-
代码实现
- Human.java
public class Human { //隐藏实现细节 private String name; private int age; //对外提供name属性、age属性的访问方式:获取属性、设置属性 public void setAge(int inputAge) { if (inputAge >= 0 && inputAge <= 130) { age = inputAge; } else { System.out.println("你是火星来的吧~~~"); } } public int getAge() { return age; } public void setName(String inputName) { name = inputName; } public String getName() { return name; } public void eat() { System.out.println("吃饭"); } public void study() { System.out.println("学习"); } }
-
Demo01_Private.java
//创建Human对象 Human human = new Human(); human.setName("张三"); human.setAge(-18); System.out.println("姓名 :" + human.getName() + " , 年龄 :" + human.getAge()); human.eat();
02. this关键字
-
概述
- this就是当前对象
-
代码实现
public class Human { //隐藏实现细节 private String name; private int age; //对外提供name属性、age属性的访问方式:获取属性、设置属性 public void setAge(int age) { if(age >= 0 && age <= 130) { this.age = age; } else { System.out.println("你怕是火星来的吧~~~"); } } public int getAge() { return this.age; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } }
-
内存图解
03. 无参构造器
-
作用
- 创建对象,并给对象中的成员变量赋值
-
赋值
修饰符 方法名(){ 方法体; }
- 修饰符: public
- 方法名:类名
-
代码实现
public class Person { String name ; int age; public Person() {//无参构造器 System.out.println("无参构造器"); this.name = "张三"; this.age = 18; } }
-
注意事项
- 定义类时,如果没有给出无参构造器,系统会默认给初值
04. 有参构造器
-
作用
- 解决无参构造器初始化成员变量初值无法从外部传入的问题
-
语法
修饰符 方法名(参数类型 参数名1,参数类型 参数名2..){ 方法体; }
- 修饰符:public
- 方法名: 和类名保持一致
- 参数类型 参数名1:外部传入的参数
-
代码实现
-
Person.java
public class Person { String name ; int age; public Person() {//无参构造器 System.out.println("无参构造器"); this.name = "王八"; this.age = 18; } public Person(String name) { this.name = name; } public Person(String name,int age) { this.name = name; this.age = age; } }
-
Demo04_ArgsConstructor.java
//调用无参 Person p1 = new Person(); System.out.println(p1.name); //调用一个参数的有参 Person p2 = new Person("小王八"); System.out.println(p2.name); Person p3 = new Person("大王八"); System.out.println(p3.name); Person p4 = new Person("中王八",100); System.out.println(p4.name);
-
05. 成员变量赋值的两种方式的区别
-
成员变量赋值
- 使用set方法赋值
- 使用有参构造器赋值
-
代码实现
06. 创建对象的内存执行流程
- Student stu = new Student();
- 将main方法所在的class文件加载进方法区
- main方法进栈
- 将Student.class加载进方法区
- 在main方法中声明stu局部变量
- Student的无参构造器进栈,创建对象,并对该对象的成员变量赋初值
- Student的无参构造器出栈,并将Student对象的地址交给stu引用
07. 长方形案例
-
需求
- 定义一个长方形类,定义 求周长和面积的方法, 然后定义一个测试类进行测试
-
代码实现
-
Rectangle.java
/** * 需求:定义一个长方形类,定义 求周长和面积的方法, 然后定义一个测试类进行测试 * @author 海锋 * */ public class Rectangle { private int length; private int width; public Rectangle() { // TODO Auto-generated constructor stub } /** * @param length * @param width */ public Rectangle(int length, int width) { this.length = length; this.width = width; } /** * @return the length */ public int getLength() { return length; } /** * @param length the length to set */ public void setLength(int length) { this.length = length; } /** * @return the width */ public int getWidth() { return width; } /** * @param width the width to set */ public void setWidth(int width) { this.width = width; } /** * 求周长 * @return */ public int getSize() { return 2 * (length + width); } /** * 求面积 * @return */ public int getArea() { return length * width; } }
-
Demo03_Rectangle.java
/** * 长方形的测试类 * @author 海锋 * */ public class Demo03_Rectangle { public static void main(String[] args) { Rectangle rec = new Rectangle(); rec.setLength(40); rec.setWidth(68); System.out.println(rec.getSize()); System.out.println(rec.getArea()); System.out.println("-----------"); Rectangle rec2 = new Rectangle(40, 68); System.out.println(rec2.getSize()); System.out.println(rec2.getArea()); } }
-
08. 迪米特法则
-
概述
- 最少知道原则
- Law Of Demeter
- 一个模块对另一个模块要尽可能少的知道它的细节
-
反例
-
Computer.java
public class Computer { public void saveData() { System.out.println("保存数据"); } public void closeScreen() { System.out.println("关闭屏幕"); } public void powerOff() { System.out.println("关闭电源"); } }
-
Demo07_LOD.java
Computer computer = new Computer(); computer.saveData(); computer.closeScreen(); computer.powerOff();
-
-
正例
-
Computer.java
public class Computer { private void saveData() { System.out.println("保存数据"); } private void closeScreen() { System.out.println("关闭屏幕"); } private void powerOff() { System.out.println("关闭电源"); } public void shutDown() { saveData(); closeScreen(); powerOff(); } }
-
Demo07_LOD.java
Computer computer = new Computer(); computer.shutDown();
-
09. static关键字
- 概述
- static修饰的成员被该类的所有对象共享
10. static关键字特点
-
特点
- 随着类的加载而加载,而且只加载一次
- 优先于对象存在
- 被类的所有对象共享
- 节省内存
- 可以使用类名调用
-
使用场景
- 当一个成员需要被该类的所有对象共享时就使用static
-
代码实现
-
Demo09.Static.java
-
ystem.out.println(Person.country); Person.study(); System.out.println(new Person().country); new Person().study();
-
11. static关键字注意事项
- 注意事项
- 静态只能访问静态
- 总结
- 普通方法
- 可以访问普通成员
- 可以访问静态成员
- 静态方法
- 不可以访问普通成员
- 可以访问静态成员
- 普通方法
12. 静态变量、成员变量、局部变量的区别
- 定义位置
- 静态变量,类中方法外
- 成员变量,类中方法外
- 局部变量,方法中
- 所属不同
- 静态变量,属于类,也叫类变量
- 成员变量,属于对象,也叫对象变量
- 局部变量,属于方法,也叫方法变量
- 内存位置
- 静态变量,在静态方法区
- 成员变量,在堆中
- 局部变量,在栈中
- 生命周期
- 静态变量,随着类的加载而创建,随着类的销毁而销毁。且只加载一次。
- 成员变量,随着对象的创建而创建,随着对象的销毁而销毁
- 局部变量,随着方法的执行而创建,随着方法的结束而销毁
- 初始化
- 静态变量,系统会默认给初值
- 成员变量,系统会默认给初值
- 局部变量,系统不会默认给初值
13. 自定义工具类
-
工具类
- 一个类中的方法全部都是静态方法
-
代码实现
-
ArrayTool.java
/** * 数组自定义工具类 * @author 海锋 * */ public class ArrayTool { /** * 数组最大值 * @param num * @return */ public static int getMax(int[] num) { int max = num[0]; for (int i = 0; i < num.length; i++) { if(max < num[i]) { max = num[i]; } } return max; } /** * 数组的遍历 * @param num */ public static void traversal(int[] num) { for (int i = 0; i < num.length; i++) { System.out.println(num[i]); } } /** * 数组的反转 * @param num */ public static void reverse(int[] num) { for (int i = num.length-1; i >= 0; i--) { System.out.println(num[i]); } } }
-
Demo06_Tool.java
/** * 自定义工具类的测试类 * @author 海锋 * */ public class Demo06_Tool { public static void main(String[] args) { int[] num = {21,45,67,867,23,545}; int max = ArrayTool.getMax(num); System.out.println("数组的最大值:"+ max); ArrayTool.traversal(num); System.out.println("------------"); ArrayTool.reverse(num); } }
-
t[] num) {
for (int i = num.length-1; i >= 0; i–) {
System.out.println(num[i]);
}
}
}
```
-
Demo06_Tool.java
/** * 自定义工具类的测试类 * @author 海锋 * */ public class Demo06_Tool { public static void main(String[] args) { int[] num = {21,45,67,867,23,545}; int max = ArrayTool.getMax(num); System.out.println("数组的最大值:"+ max); ArrayTool.traversal(num); System.out.println("------------"); ArrayTool.reverse(num); } }