- /*
- 编程实现People类的封装
- */
- public class People {
-
- // 1.私有化成员变量,使用private关键字修饰
- private String name;
- private int age;
- //private String country; // 隶属于对象层级,也就是每个对象都拥有独立的一份
- //public static String country; // 隶属于类层级,也就是整个类只有一份并且被所有对象共享
- private static String country;
-
- // 3.在构造方法中调用set方法进行合理值的判断
- public People() {}
- public People(String name, int age/*, String country*/) {
- setName(name);
- setAge(age);
- //setCountry(country);
- }
-
- // 2.提供公有的get和set方法,并在方法体中进行合理值的判断
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- if(age > 0 && age < 150) {
- this.age = age;
- } else {
- System.out.println("年龄不合理哦!!!");
- }
- }
- public static String getCountry() {
- return country;
- }
- public static void setCountry(String country) {
- //this.country = country;
- People.country = country;
- }
-
- public void show() {
- System.out.println("我是" + getName() + ",今年" + getAge() + "岁了,来自" + getCountry());
- }
- }
- /*
- 编程实现People类的测试
- */
- public class PeopleTest {
-
- public static void main(String[] args) {
-
- // 3.验证static关键字修饰的静态成员(类成员)是否与创建对象无关 类名.的方式 => 无关
- //System.out.println("获取到的国籍信息是:" + People.country); // null
- System.out.println("获取到的国籍信息是:" + People.getCountry()); // null
-
- // 1.使用有参方式构造两个People类型的对象并打印特征
- People p1 = new People("zhangfei", 30/*, "China"*/);
- p1.show(); // zhangfei 30 China
-
- People p2 = new People("guanyu", 35/*, "China"*/);
- p2.show(); // guanyu 35 China
-
- System.out.println("--------------------------------------------");
- // 2.验证static关键字修饰的静态成员(类成员) 是否被所有对象共享 => 共享
- //p1.country = "蜀国";
- p1.setCountry("蜀国");
- //System.out.println("第一个对象的国籍是:" + p1.country); // 蜀国
- //System.out.println("第二个对象的国籍是:" + p2.country); // 蜀国
- System.out.println("第一个对象的国籍是:" + p1.getCountry()); // 蜀国
- System.out.println("第二个对象的国籍是:" + p2.getCountry()); // 蜀国
-
- People p3 = new People();
- //System.out.println("第三个对象的国籍是:" + p3.country); // 蜀国
- System.out.println("第三个对象的国籍是:" + p3.getCountry()); // 蜀国
- }
- }
static关键字的由来

static关键字的作用:

基本概念
- /*
- 编程实现static关键字的使用
- */
- public class StaticTest {
-
- private int cnt = 1; // 隶属于对象层级,也就是每个对象都拥有独立的一份
- private static int snt = 2; // 隶属于类层级,也就是所有对象都共享同一份
-
- // 自定义非静态的成员方法 需要使用引用.的方式访问
- public void show() {
- System.out.println("cnt = " + this.cnt); // 1
- System.out.println("snt = " + this.snt); // 2 静态成员被所有对象共享,this关键字可以省略
- }
- // 自定义静态的成员方法 推荐使用类名.的方式访问
- public static void test() {
- // StaticTest st = new StaticTest();
- //System.out.println("cnt = " + cnt); // 1 静态成员方法中没有this关键字,因为是可以通过类名.方式调用的
- System.out.println("snt = " + snt); // 2
- }
-
- public static void main(String[] args) {
-
- StaticTest st = new StaticTest();
- st.show();
-
- System.out.println("--------------------------------");
- StaticTest.test();
- }
- }
构造块和静态代码块(熟悉)
- /*
- 编程实现构造块和静态代码块的使用
- */
- public class BlockTest {
-
- // 当需要在执行构造方法体之前做一些准备工作时,则将准备工作的相关代码写在构造块中即可,比如:对成员变量进行的统一初始化操作
- {
- System.out.println("构造块!"); // (2)
- }
-
- // 静态代码块会随着类的加载而准备就绪,会先于构造块执行
- // 当需要在执行代码块之前随着类的加载做一些准备工作时,则编写代码到静态代码块中,比如:加载数据库的驱动包等
- static {
- System.out.println("#####################静态代码块!"); // (1)
- }
-
- // 自定义构造方法
- public BlockTest() {
- System.out.println("====构造方法体!"); // (3)
- }
-
- public static void main(String[] args) {
-
- BlockTest bt = new BlockTest();
-
-
- BlockTest bt2 = new BlockTest();
- }
- }
又见main方法
- /*
- 编程实现main方法的测试
- */
- public class MainTest {
-
- public static void main(String[] args) {
-
- System.out.println("参数数组中元素的个数是:" + args.length);
- System.out.println("传递给main方法的实际参数为:");
- for(int i = 0; i < args.length; i++) {
- System.out.println("下标为" + i + "的形参变量数值为:" + args[i]);
- }
- }
- }
案例题目(重中之重)
- /*
- 编程实现Singleton类的封装
- */
- public class Singleton {
-
- // 2.声明本类类型的引用指向本类类型的对象,使用private static关键字共同修饰
- //private static Singleton sin = new Singleton(); // 饿汉式
- private static Singleton sin = null; // 懒汉式
-
- // 1.私有化构造方法,使用private关键字修饰
- private Singleton() {}
-
- // 3.提供公有的get方法负责将对象返回出去,使用public static关键字共同修饰
- public static Singleton getInstance() {
- //return sin;
- if(null == sin) {
- sin = new Singleton();
- }
- return sin;
- }
- }
- /*
- 编程实现Singleton类的测试
- */
- public class SingletonTest {
-
- public static void main(String[] args) {
-
- // 1.声明Singleton类型的引用指向该类型的对象
- //Singleton s1 = new Singleton();
- //Singleton s2 = new Singleton();
- //System.out.println(s1 == s2); // 比较变量s1的数值是否与变量s2的数值相等 false
- //Singleton.sin = null; 可以使得引用变量无效
- Singleton s1 = Singleton.getInstance();
- Singleton s2 = Singleton.getInstance();
- System.out.println(s1 == s2); // true
- }
- }
单例设计模式的执行流程:

单例设计模式的概念
- /*
- 编程实现Person类的封装
- */
- public class Person {
-
- // 1.私有化成员变量,使用private关键字修饰
- private String name;
- private int age;
- //private boolean gender; // 性别
-
- // 3.在构造方法中调用set方法进行合理值的判断
- public Person() {
- System.out.println("Person()");
- }
- public Person(String name, int age) {
- System.out.println("Person(String, int)");
- setName(name);
- setAge(age);
- }
-
- // 2.提供公有的get和set方法并在方法体中进行合理值的判断
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- if(age > 0 && age < 150) {
- this.age = age;
- } else {
- System.out.println("年龄不合理哦!!!");
- }
- }
-
- // 自定义成员方法实现特征的打印
- public void show() {
- System.out.println("我是" + getName() + ",今年" + getAge() + "岁了!");
- }
- // 自定义成员方法描述吃饭的行为
- public void eat(String food) {
- System.out.println(food + "真好吃!");
- }
- // 自定义成员方法描述娱乐的行为
- public void play(String game) {
- System.out.println(game + "真好玩!");
- }
- }
- /*
- 自定义Worker类继承自Person类
- */
- public class Worker extends Person {
-
- private int salary;
-
- public Worker() {
- super(); // 表示调用父类的无参构造方法,若没有加则编译器自动添加
- System.out.println("Worker()");
- }
- public Worker(String name, int age, int salary) {
- super(name, age); // 表示调用父类的有参构造方法
- System.out.println("Worker(String, int, int)");
- //setName(name);
- //setAge(age);
- setSalary(salary);
- }
-
- public int getSalary() {
- return salary;
- }
- public void setSalary(int salary) {
- if(salary >= 2200) {
- this.salary = salary;
- } else {
- System.out.println("薪水不合理哦!!!");
- }
- }
-
- // 自定义成员方法描述工作的行为
- public void work() {
- System.out.println("今天的砖头有点烫手...");
- }
- // 自定义show方法覆盖从父类中继承的版本
- @Override // 标注/注解,用于说明下面的方法是对父类方法的重写,若没有构成重写则编译报错
- public void show() {
- super.show(); // 表示调用父类的show方法
- System.out.println("我的薪水是:" + getSalary());
- }
- }
- /*
- 编程实现Worker类的测试
- */
- public class WorkerTest {
-
- public static void main(String[] args) {
-
- // 1.使用无参方式构造Worker类型的对象并打印特征
- Worker w1 = new Worker();
- // 当子类重写show方法后,则下面调用的是重写以后的版本
- w1.show(); // null 0
-
- System.out.println("----------------------------------");
- // 2.使用有参方式构造Worker类型的对象并打印特征
- Worker w2 = new Worker("zhangfei", 30, 3000);
- w2.show(); // zhangfei ...
- // 调用成员方法测试
- w2.eat("豆芽");
- w2.play("王者荣耀");
- w2.work();
- }
- }
- /*
- 编程实现Teacher类继承自Person类
- */
- public class Teacher extends Person {
-
- }
- /*
- 编程实现Teacher类的测试
- */
- public class TeacherTest {
-
- public static void main(String[] args) {
-
- // 1.使用无参方式构造Teacher类型的对象并打印特征
- Teacher t1 = new Teacher();
- t1.show(); // null 0
- }
- }

- /*
- 编程实现ComputerTeacher类继承自Teacher类
- */
- public class ComputerTeacher extends Teacher {
-
- }
继承关系图:

继承的特点
- package com.lagou.task08;
-
- public class Animal {
- private String name;
- private String color;
-
- public Animal() {
- }
- public Animal(String name, String color) {
- setName(name);
- setColor(color);
- }
-
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getColor() {
- return color;
- }
- public void setColor(String color) {
- this.color = color;
- }
-
- public void show() {
- // sout 回车 生成打印的语句
- System.out.println("名字:" + getName() + ", 颜色:" + getColor());
- }
- }
- package com.lagou.task08;
-
- public class Dog extends Animal {
- private int tooth;
-
- public Dog() {
- super(); // 表示调用父类的无参构造方法 自动保存
- }
- public Dog(String name, String color, int tooth) {
- super(name, color); // 表示调用父类的有参构造方法
- setTooth(tooth);
- }
-
- public int getTooth() {
- return tooth;
- }
- public void setTooth(int tooth) {
- if (tooth > 0) {
- this.tooth = tooth;
- } else {
- System.out.println("牙齿数量不合理哦!!!");
- }
- }
-
- @Override
- public void show() {
- super.show();
- System.out.println("牙齿数量是:" + getTooth());
- }
- }
- package com.lagou.task08;
-
- public class DogTest {
-
- public static void main(String[] args) {
- // 1.使用无参方式构造Dog类型的对象并打印特征
- Dog d1 = new Dog();
- d1.show(); // null null 0
-
- // 2.使用有参方式构造Dog类型的对象并打印特征
- Dog d2 = new Dog("旺财", "白色", 10);
- d2.show(); // 旺财 白色 10
- }
- }
- package com.lagou.task08;
-
- import java.sql.SQLOutput;
-
- public class SuperTest {
-
- {
- System.out.println("SuperTest类中的构造块!"); // (2) c
- }
-
- static {
- System.out.println("SuperTest类中的静态代码块!"); // (1) a
- }
-
- public SuperTest() {
- System.out.println("SuperTest类中的构造方法体!"); // (3) d
- }
-
- public static void main(String[] args) {
-
- // 使用无参方式构造对象
- SuperTest st = new SuperTest();
- }
- }
- package com.lagou.task08;
-
- // 导入java目录中lang目录中System类中的静态成员out 很少使用
- import static java.lang.System.out;
-
- public class SubSuperTest extends SuperTest {
-
- {
- System.out.println("==========SubSuperTest类中的构造块!"); // (2) e
- }
-
- static {
- System.out.println("==========SubSuperTest类中的静态代码块!"); // (1) b
- }
-
- public SubSuperTest() {
- //System.out.println("==========SubSuperTest类中的构造方法体!"); // (3) f
- out.println("==========SubSuperTest类中的构造方法体!");
- }
-
- public static void main(String[] args) {
-
- // 使用无参方式构造子类的对象
- SubSuperTest sst = new SubSuperTest();
- }
- }
第三节:访问控制
- package com.lagou.task08;
-
- // ctrl+shift+/ 进行选中内容的多行注释 ,再来一次取消注释
- // ctrl+/ 进行当前行的单行注释 ,再来一次取消注释
- public /*final*/ class FinalClass {
-
- public final void show() {
- System.out.println("FinalClass类中的show方法!");
- }
-
- }
- package com.lagou.task08;
-
- public class SubFinalClass extends FinalClass {
-
- /*@Override
- public void show() {
- super.show();
- }*/
-
- public static void main(String[] args) {
- SubFinalClass sfc = new SubFinalClass();
- sfc.show();
- }
- }
- package com.lagou.task08;
-
- public class FinalMemberTest {
- // private final int cnt = 1; // 显式初始化
- private final int cnt;
-
- /*{
- cnt = 2; // 构造块中进行初始化
- }*/
-
- public FinalMemberTest() {
- cnt = 3; // 构造方法体中进行初始化
- }
-
- public static void main(String[] args) {
-
- // 声明FinalMemberTest类型的引用指向该类的对象
- FinalMemberTest fmt = new FinalMemberTest();
- // 打印成员变量的数值
- System.out.println("fmt.cnt = " + fmt.cnt); // 0 1 2 3
- }
- }
使用方式