1. 封装之private关键字
-
概述
- 使用private关键字修饰属性和行为,那么该属性的行为只能在本类中访问
-
代码
Human类
public class Human { private String name; private int age; public void setName(String str) { name = str; } public String getName() { return name; } public void setAge(int str) { age = str; } public int getAge() { return age; } }
Demo01
public class Demo01 { public static void main(String[] args) { Human human = new Human(); human.setName("海风"); human.setAge(18); System.out.println(human.getAge()); System.out.println(human.getName()); } }
2. this关键字
-
概述
- this就是当前对象
-
代码
public class Human2 { private String name; private int age; public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { if(age >= 0 && age <= 130) { this.age = age; }else { System.out.println("输入错误"); } } public int getAge() { return age; } }
3. 无参构造器
-
作用
- 创建对象,并对对象的的成员变量赋初值
-
语法
public 类名(){ 方法体; }
-
注意事项
- 如果自己不给出,系统会自动给出
-
代码
public class Person { String name; int age; public Person() { System.out.println("我是无参构造器"); this.name = "海风"; this.age = 24; } }
4. 有参构造器
-
作用
- 解决无参构造器初始化成员初值时无法从外界输入的问题
-
注意事项
- 若果手动定义了有参构造器,而没有定义有参构造器,系统不会自动生成无参构造器
-
总结
- 多个有参构造器叫有参构造器的重载
-
代码
public class Person { String name; int age; private String sex; public Person() { System.out.println("我是无参构造器"); this.name = "海风"; this.age = 24; this.sex = "man"; } public Person(String name) { this.name = name; } }
public class Demo02 { public static void main(String[] args) { Person person = new Person(); Person person1 = new Person("傻狗"); System.out.println(person.age); System.out.println(person.name); System.out.println(person1.name); } }
5. 成员变量赋值的两种方式的区别
-
使用set方法
-
使用有参构造器
-
代码
public class Computer { String brand; double price; String color; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public Computer(String brand, double price, String color) { super(); this.brand = brand; this.price = price; this.color = color; } public Computer() { super(); } }
public class Demo03 { public static void main(String[] args) { Computer computer = new Computer(); //set方法赋值 computer.setBrand("华为"); computer.setColor("银色"); computer.setPrice(8999); System.out.println(computer.price + "可以买到" + computer.brand + "的" + computer.color + "电脑"); //有参构造赋值 Computer computer1 = new Computer("小米", 5999, "黑色"); System.out.println(computer1.price + "可以买到" + computer1.brand + "的" + computer1.color + "电脑"); } }
6. 创建对象的步骤
tudent s = new Student();
-
1,将main方法所在class类加载进内存方法区
-
2,main方法进栈
-
3,Student.class加载进内存方法区
-
4,声明一个Student类型引用stu
-
5,构造方法进栈,对对象中的属性赋值,构造方法弹栈
-
6,将对象的地址值赋值给stu
-
图解
7. 练习
-
长方形的面积和周长
public class Rectangle { double length; double width; /** * @return the length */ public double getLength() { return length; } public Rectangle() { super(); } public Rectangle(double length, double width) { super(); this.length = length; this.width = width; } /** * @param length the length to set */ public void setLength(double length) { this.length = length; } /** * @return the width */ public double getWidth() { return width; } /** * @param width the width to set */ public void setWidth(double width) { this.width = width; } public void getPerimeter () { System.out.println("周长为:" + 2 * (length + width)); } public void getArea () { System.out.println("面积为:" + length * width); } }
public class Demo04_Rectangle_Text { public static void main(String[] args) { Rectangle rectangle = new Rectangle(); rectangle.length = 20; rectangle.width = 30; rectangle.getPerimeter(); rectangle.getArea(); Rectangle rectangle2 = new Rectangle(40, 60); rectangle2.getPerimeter(); rectangle2.getArea(); } }
8.迪米特法则
- 又称最小知道原则
- 一个对象对其他对象有尽可能少的了解
- 不要和陌生人说话
9. static关键字
-
特点
- 随类的加载而加载,且只加载一次
- 优先于对象存在
- 被类的所有对象共享
- 节省内存
- 可以直接使用类名调用
-
总结
-
需要共享成员是可以使用static
-
代码
public class Student { String name; static String gard; }
public class Demo05_Static { public static void main(String[] args) { Student stu = new Student(); stu.name = "海风"; stu.gard = "2004"; Student stu1 = new Student(); stu1.name = "求问号"; Student stu2 = new Student(); stu2.name = "小王八"; System.out.println(stu.name + "是" + stu.gard + "班的"); System.out.println(stu1.name + "是" + stu1.gard + "班的"); System.out.println(stu2.name + "是" + stu2.gard + "班的"); } }
- 图解
10. static关键字注意事项
- 静态只能访问静态
- 总结
- 普通方法
- 可以调用普通,也可以调用静态
- 静态方法
- 只能调用静态
- 普通方法
11. 静态变量,成员变量,局部变量
-
定义位置
- 静态变量
- 类中方法外
- 成员变量
- 类中方法外
- 局部变量
- 方法中
- 静态变量
-
所属不同
- 静态变量
- 属于类,又叫类变量
- 成员变量
- 属于对象,又叫对象变量
- 局部变量
- 属于方法,又叫方法变量
- 静态变量
-
内存位置
- 静态变量
- 方法区的静态方法区
- 成员变量
- 堆中
- 局部变量
- 栈中
- 静态变量
-
生命周期
- 静态变量
- 随着类的加载而加载,随着类的消失而消失
- 成员变量
- 随着对象的创建而存在,随着对象的消失而消失
- 局部变量
- 随着方法的执行而创建,随着方法的结束而销毁
- 静态变量
-
初始化
- 静态变量
- 系统给定初值
- 成员变量
- 系统给定初值
- 局部变量
- 系统不会给定初值
- 静态变量
12. 自定义工具类
成员变量
* 类中方法外
-
局部变量
- 方法中
-
所属不同
- 静态变量
- 属于类,又叫类变量
- 成员变量
- 属于对象,又叫对象变量
- 局部变量
- 属于方法,又叫方法变量
- 静态变量
-
内存位置
- 静态变量
- 方法区的静态方法区
- 成员变量
- 堆中
- 局部变量
- 栈中
- 静态变量
-
生命周期
- 静态变量
- 随着类的加载而加载,随着类的消失而消失
- 成员变量
- 随着对象的创建而存在,随着对象的消失而消失
- 局部变量
- 随着方法的执行而创建,随着方法的结束而销毁
- 静态变量
-
初始化
- 静态变量
- 系统给定初值
- 成员变量
- 系统给定初值
- 局部变量
- 系统不会给定初值
- 静态变量
12. 自定义工具类
- 方法绝大部分是静态的,方便调用