第三知识单元

面向对象程序设计

Java程序设计 第6讲,主讲人:李欣

Created: 2023-09-12 Tue 00:26

0.1. 互动课堂

Click to host the seminar.

0.2. 签到

https://xin.blue/tool/attendance/

0.3. 本次课的目标

  • 理解多态的含义
  • 理解方法的重载
  • 理解构造函数的重载
  • 包结构与使用
  • 接口与接口实现

1. 方法覆盖实现的多态

public class Main {
    public static void main(String[] args) {
        ChinesePeople c = new ChinesePeople();
        c.setName("张三");
        c.sayHello();
        JapanesePeople j = new JapanesePeople();
        j.setName("田中一郎");
        j.sayHello();
        People p = new People();
        p.setName("Lucy");
        p.sayHello();
    }
}
class People {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void sayHello() {
        System.out.println("Hi, I'm " + this.name + ". Nice to meet you!");
    }
}
class ChinesePeople extends People {
    public void sayHello(){
        System.out.println("你好,我是" + this.getName() + "。很高兴认识你!");
    }
}
class JapanesePeople extends People {
    public void sayHello(){
        System.out.println("こんにちは、" + this.getName() + "です。よろしくお願いします。");
    }
}
你好,我是张三。很高兴认识你!
こんにちは、田中一郎です。よろしくお願いします。
Hi, I'm Lucy. Nice to meet you!

2. 方法重载实现的多态

public class Main {
    public static void main(String[] args) {
        ChinesePeople c = new ChinesePeople();
        c.setName("张三");
        c.sayHello();
        System.out.println("。");
        c.sayHello("李四");
        System.out.println();
        c.sayHello("李四", "王五");
        System.out.println();
    }
}
class People {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void sayHello() {
        System.out.println("Hi, I'm " + this.name + ". Nice to meet you!");
    }
}
class ChinesePeople extends People {
    public void sayHello(){
        System.out.print("我是" + this.getName() + "。很高兴认识你");
    }
    public void sayHello(String guestName) {
        System.out.print("你好,请问你是" + guestName + "吗?");
        sayHello();
        System.out.print("。");
    }
    public void sayHello(String firstGuestName, String secondGuestName) {
        System.out.print("你好,请问你们是" + firstGuestName + "和" + secondGuestName + "吗?");
        sayHello();
        System.out.print("们。");
    }
}
我是张三。很高兴认识你。
你好,请问你是李四吗?我是张三。很高兴认识你。
你好,请问你们是李四和王五吗?我是张三。很高兴认识你们。

3. 对象引用的多态

对象的 引用型 变量 具有 多态性 ,因为一个 引用型 变量 可以指向不同形式的对象, 这主要是基于一个事实:

子类 对象可以作为 父类 对象来使用。

public class Main {
    public static void main(String[] args) {
        People[] p = new People[4];
        p[0] = new ChinesePeople("张三");
        p[1] = new JapanesePeople("田中一郎");
        p[2] = new ChinesePeople("李四");
        p[3] = new JapanesePeople("山下次郎");
        for (int i = 0; i < 4; i++) {
            p[i].sayHello();
        }
    }
}
class People {
    private String name;
    People(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void sayHello() {
        System.out.println("Hi, I'm " + this.name + ". Nice to meet you!");
    }
}
class ChinesePeople extends People {
    ChinesePeople(String name) {
        super(name);
    }
    public void sayHello(){
        System.out.println("你好,我是" + this.getName() + "。很高兴认识你。");
    }
    public void sayHelloToSomebody(String guestName) {
        System.out.println("你好,请问你是" + guestName + "吗?我是" +
                           this.getName() + "。很高兴认识你。");
    }
    public void sayHelloToSomebody(String firstGuestName, String secondGuestName) {
        System.out.print("你好,请问你们是" + firstGuestName + "和" + secondGuestName + "吗?我是" +
                         this.getName() + "。很高兴认识你们。");
    }
}
class JapanesePeople extends People {
    JapanesePeople(String name) {
        super(name);
    }
    public void sayHello(){
        System.out.println("こんにちは、" + this.getName() + "です。よろしくお願いします。");
    }
}
你好,我是张三。很高兴认识你。
こんにちは、田中一郎です。よろしくお願いします。
你好,我是李四。很高兴认识你。
こんにちは、山下次郎です。よろしくお願いします。
public class Main {
    public static void main(String[] args) {
        People[] p = new People[4];
        p[0] = new ChinesePeople("张三");
        p[1] = new JapanesePeople("田中一郎");
        p[2] = new ChinesePeople("李四");
        p[3] = new JapanesePeople("山下次郎");
        for (int i = 0; i < 4; i++) {
            p[i].sayHello();
        }
        ChinesePeople c = new People("张三");
    }
}
class People {
    private String name;
    People(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void sayHello() {
        System.out.println("Hi, I'm " + this.name + ". Nice to meet you!");
    }
}
class ChinesePeople extends People {
    ChinesePeople(String name) {
        super(name);
    }
    public void sayHello(){
        System.out.println("你好,我是" + this.getName() + "。很高兴认识你。");
    }
    public void sayHelloToSomebody(String guestName) {
        System.out.println("你好,请问你是" + guestName + "吗?我是" +
                           this.getName() + "。很高兴认识你。");
    }
    public void sayHelloToSomebody(String firstGuestName, String secondGuestName) {
        System.out.print("你好,请问你们是" + firstGuestName + "和" + secondGuestName + "吗?我是" +
                         this.getName() + "。很高兴认识你们。");
    }
}
class JapanesePeople extends People {
    JapanesePeople(String name) {
        super(name);
    }
    public void sayHello(){
        System.out.println("こんにちは、" + this.getName() + "です。よろしくお願いします。");
    }
}
javac Main.java
javac Main.java
# Main.java:11: error: incompatible types: People cannot be converted to ChinesePeople
#         ChinesePeople c = new People("张三");
#                           ^
# 1 error
public class Main {
    public static void main(String[] args) {
        People[] p = new People[4];
        p[0] = new ChinesePeople("张三");
        p[1] = new JapanesePeople("田中一郎");
        p[2] = new ChinesePeople("李四");
        p[3] = new JapanesePeople("山下次郎");
        for (int i = 0; i < 4; i++) {
            p[i].sayHello();
        }
        p[0].sayHelloToSomebody("李四");
    }
}
class People {
    private String name;
    People(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void sayHello() {
        System.out.println("Hi, I'm " + this.name + ". Nice to meet you!");
    }
}
class ChinesePeople extends People {
    ChinesePeople(String name) {
        super(name);
    }
    public void sayHello(){
        System.out.println("你好,我是" + this.getName() + "。很高兴认识你。");
    }
    public void sayHelloToSomebody(String guestName) {
        System.out.println("你好,请问你是" + guestName + "吗?我是" +
                           this.getName() + "。很高兴认识你。");
    }
    public void sayHelloToSomebody(String firstGuestName, String secondGuestName) {
        System.out.print("你好,请问你们是" + firstGuestName + "和" + secondGuestName + "吗?我是" +
                         this.getName() + "。很高兴认识你们。");
    }
}
class JapanesePeople extends People {
    JapanesePeople(String name) {
        super(name);
    }
    public void sayHello(){
        System.out.println("こんにちは、" + this.getName() + "です。よろしくお願いします。");
    }
}
javac Main.java
javac Main.java
# Main.java:11: error: cannot find symbol
#         p[0].sayHelloToSomebody("李四");
#             ^
#   symbol:   method sayHelloToSomebody(String)
#   location: class People
# 1 error
public class Main {
    public static void main(String[] args) {
        People[] p = new People[4];
        p[0] = new ChinesePeople("张三");
        p[1] = new JapanesePeople("田中一郎");
        p[2] = new ChinesePeople("李四");
        p[3] = new JapanesePeople("山下次郎");
        for (int i = 0; i < 4; i++) {
            p[i].sayHello();
        }
        ((ChinesePeople)p[0]).sayHelloToSomebody("李四");
    }
}
class People {
    private String name;
    People(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void sayHello() {
        System.out.println("Hi, I'm " + this.name + ". Nice to meet you!");
    }
}
class ChinesePeople extends People {
    ChinesePeople(String name) {
        super(name);
    }
    public void sayHello(){
        System.out.println("你好,我是" + this.getName() + "。很高兴认识你。");
    }
    public void sayHelloToSomebody(String guestName) {
        System.out.println("你好,请问你是" + guestName + "吗?我是" +
                           this.getName() + "。很高兴认识你。");
    }
    public void sayHelloToSomebody(String firstGuestName, String secondGuestName) {
        System.out.print("你好,请问你们是" + firstGuestName + "和" + secondGuestName + "吗?我是" +
                         this.getName() + "。很高兴认识你们。");
    }
}
class JapanesePeople extends People {
    JapanesePeople(String name) {
        super(name);
    }
    public void sayHello(){
        System.out.println("こんにちは、" + this.getName() + "です。よろしくお願いします。");
    }
}
你好,我是张三。很高兴认识你。
こんにちは、田中一郎です。よろしくお願いします。
你好,我是李四。很高兴认识你。
こんにちは、山下次郎です。よろしくお願いします。
你好,请问你是李四吗?我是张三。很高兴认识你。
public class Main {
    public static void main(String[] args) {
        People[] p = new People[4];
        p[0] = new ChinesePeople("张三");
        p[1] = new JapanesePeople("田中一郎");
        p[2] = new ChinesePeople("李四");
        p[3] = new JapanesePeople("山下次郎");
        for (int i = 0; i < 4; i++) {
            p[i].sayHello();
        }
        People person = new People("张三");
        ChinesePeople c = (ChinesePeople) person;
    }
}
class People {
    private String name;
    People(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void sayHello() {
        System.out.println("Hi, I'm " + this.name + ". Nice to meet you!");
    }
}
class ChinesePeople extends People {
    ChinesePeople(String name) {
        super(name);
    }
    public void sayHello(){
        System.out.println("你好,我是" + this.getName() + "。很高兴认识你。");
    }
    public void sayHelloToSomebody(String guestName) {
        System.out.println("你好,请问你是" + guestName + "吗?我是" +
                           this.getName() + "。很高兴认识你。");
    }
    public void sayHelloToSomebody(String firstGuestName, String secondGuestName) {
        System.out.print("你好,请问你们是" + firstGuestName + "和" + secondGuestName + "吗?我是" +
                         this.getName() + "。很高兴认识你们。");
    }
}
class JapanesePeople extends People {
    JapanesePeople(String name) {
        super(name);
    }
    public void sayHello(){
        System.out.println("こんにちは、" + this.getName() + "です。よろしくお願いします。");
    }
}
javac Main.java
javac Main.java
java Main
javac Main.java
java Main
# 你好,我是张三。很高兴认识你。
# こんにちは、田中一郎です。よろしくお願いします。
# 你好,我是李四。很高兴认识你。
# こんにちは、山下次郎です。よろしくお願いします。
# Exception in thread "main" java.lang.ClassCastException: class People cannot be cast to class ChinesePeople (People and ChinesePeople are in unnamed module of loader 'app')
#         at Main.main(Main.java:12)
public class Main {
    public static void main(String[] args) {
        People[] p = new People[4];
        p[0] = new ChinesePeople("张三");
        p[1] = new JapanesePeople("田中一郎");
        p[2] = new ChinesePeople("李四");
        p[3] = new JapanesePeople("山下次郎");
        for (int i = 0; i < 4; i++) {
            System.out.print((p[i] instanceof People) + " " +
                             (p[i] instanceof ChinesePeople) + " " +
                             (p[i] instanceof JapanesePeople) + " ");
            p[i].sayHello();
        }
        System.out.print((((ChinesePeople)p[0]) instanceof People) + " " +
                         (((ChinesePeople)p[0]) instanceof ChinesePeople) + " ");
        ((ChinesePeople)p[0]).sayHelloToSomebody("李四");
    }
}
class People {
    private String name;
    People(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void sayHello() {
        System.out.println("Hi, I'm " + this.name + ". Nice to meet you!");
    }
}
class ChinesePeople extends People {
    ChinesePeople(String name) {
        super(name);
    }
    public void sayHello(){
        System.out.println("你好,我是" + this.getName() + "。很高兴认识你。");
    }
    public void sayHelloToSomebody(String guestName) {
        System.out.println("你好,请问你是" + guestName + "吗?我是" +
                           this.getName() + "。很高兴认识你。");
    }
    public void sayHelloToSomebody(String firstGuestName, String secondGuestName) {
        System.out.print("你好,请问你们是" + firstGuestName + "和" + secondGuestName + "吗?我是" +
                         this.getName() + "。很高兴认识你们。");
    }
}
class JapanesePeople extends People {
    JapanesePeople(String name) {
        super(name);
    }
    public void sayHello(){
        System.out.println("こんにちは、" + this.getName() + "です。よろしくお願いします。");
    }
}
true true false 你好,我是张三。很高兴认识你。
true false true こんにちは、田中一郎です。よろしくお願いします。
true true false 你好,我是李四。很高兴认识你。
true false true こんにちは、山下次郎です。よろしくお願いします。
true true 你好,请问你是李四吗?我是张三。很高兴认识你。

4. 构造函数的重载

4.1. 构造函数的重载

构造函数重载 是指 同一个类 中存在着 若干个 具有 不同参数列表构造函数

当一个类定义了 若干个构造函数 时,创建该类对象的语句会 自动 根据给出的 实际参数数目类型顺序 来确定调用 哪个构造函数 来完成对新对象的 初始化工作

public class Main {
    public static void main(String[] args) {
        People[] p = new People[4];
        p[0] = new People();
        p[1] = new People("张三");
        p[2] = new People("张三", "男");
        p[3] = new People("张三", "男", 28);
        for (int i = 0; i < 4; i++) {
            System.out.print(p[i].getName() + " " + p[i].getGender() + " " + p[i].getAge() + " : ");
            p[i].sayHello();
        }
    }
}
class People {
    private String name;
    private String gender;
    private int age;
    People() { }
    People(String name) {
        this.name = name;
    }
    People(String name, String gender) {
        this.name = name;
        this.gender = gender;
    }
    People(String name, String gender, int age) {
        this.name = name;
        this.gender = gender;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void sayHello() {
        System.out.println("Hi, I'm " + this.name + ". Nice to meet you!");
    }
}
null null 0 : Hi, I'm null. Nice to meet you!
张三 null 0 : Hi, I'm 张三. Nice to meet you!
张三 男 0 : Hi, I'm 张三. Nice to meet you!
张三 男 28 : Hi, I'm 张三. Nice to meet you!
public class Main {
    public static void main(String[] args) {
        People[] p = new People[4];
        p[0] = new People();
        p[1] = new People("张三");
        p[2] = new People("张三", "男");
        p[3] = new People("张三", "男", 28);
        for (int i = 0; i < 4; i++) {
            System.out.print(p[i].getName() + " " + p[i].getGender() + " " + p[i].getAge() + " : ");
            p[i].sayHello();
        }
    }
}
class People {
    private String name;
    private String gender;
    private int age;
    People() { }
    People(String name) {
        this.name = name;
    }
    People(String name, String gender) {
        this(name);
        this.gender = gender;
    }
    People(String name, String gender, int age) {
        this(name, gender);
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void sayHello() {
        System.out.println("Hi, I'm " + this.name + ". Nice to meet you!");
    }
}
null null 0 : Hi, I'm null. Nice to meet you!
张三 null 0 : Hi, I'm 张三. Nice to meet you!
张三 男 0 : Hi, I'm 张三. Nice to meet you!
张三 男 28 : Hi, I'm 张三. Nice to meet you!

4.2. 调用父类的构造函数

4.2.1. 子类没有定义构造函数的情况

public class Main {
    public static void main(String[] args) {
        ChinesePeople c = new ChinesePeople();
    }
}
class People {
    private String name;
    private String gender;
    private int age;
    People() {
        System.out.println("Constructor People() from People class.");
    }
    People(String name) {
        this.name = name;
    }
    People(String name, String gender) {
        this(name);
        this.gender = gender;
    }
    People(String name, String gender, int age) {
        this(name, gender);
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void sayHello() {
        System.out.println("Hi, I'm " + this.name + ". Nice to meet you!");
    }
}
class ChinesePeople extends People {
    public void sayHello(){
        System.out.println("你好,我是" + this.getName() + "。很高兴认识你。");
    }
    public void sayHelloToSomebody(String guestName) {
        System.out.println("你好,请问你是" + guestName + "吗?我是" +
                           this.getName() + "。很高兴认识你。");
    }
    public void sayHelloToSomebody(String firstGuestName, String secondGuestName) {
        System.out.print("你好,请问你们是" + firstGuestName + "和" + secondGuestName + "吗?我是" +
                         this.getName() + "。很高兴认识你们。");
    }
}
Constructor People() from People class.

4.2.2. 子类定义了构造函数的情况

public class Main {
    public static void main(String[] args) {
        People p = new People("Lucy");
        System.out.println(p.getName());
        ChinesePeople c = new ChinesePeople("张三");
        System.out.println(c.getName());
    }
}
class People {
    private String name;
    private String gender;
    private int age;
    People() {
        System.out.println("Constructor People() from People class.");
    }
    People(String name) {
        this.name = name;
    }
    People(String name, String gender) {
        this(name);
        this.gender = gender;
    }
    People(String name, String gender, int age) {
        this(name, gender);
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void sayHello() {
        System.out.println("Hi, I'm " + this.name + ". Nice to meet you!");
    }
}
class ChinesePeople extends People {
    ChinesePeople(String name) {
        setName(name);
    }
    public void sayHello(){
        System.out.println("你好,我是" + this.getName() + "。很高兴认识你。");
    }
    public void sayHelloToSomebody(String guestName) {
        System.out.println("你好,请问你是" + guestName + "吗?我是" +
                           this.getName() + "。很高兴认识你。");
    }
    public void sayHelloToSomebody(String firstGuestName, String secondGuestName) {
        System.out.print("你好,请问你们是" + firstGuestName + "和" + secondGuestName + "吗?我是" +
                         this.getName() + "。很高兴认识你们。");
    }
}
Lucy
Constructor People() from People class.
张三

4.2.3. 子类定义的构造函数中使用 super 调用父类构造函数的情况

public class Main {
    public static void main(String[] args) {
        People p = new People("Lucy");
        System.out.println(p.getName());
        ChinesePeople c = new ChinesePeople("张三");
        System.out.println(c.getName());
    }
}
class People {
    private String name;
    private String gender;
    private int age;
    People() {
        System.out.println("Constructor People() from People class.");
    }
    People(String name) {
        this.name = name;
    }
    People(String name, String gender) {
        this(name);
        this.gender = gender;
    }
    People(String name, String gender, int age) {
        this(name, gender);
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void sayHello() {
        System.out.println("Hi, I'm " + this.name + ". Nice to meet you!");
    }
}
class ChinesePeople extends People {
    ChinesePeople(String name) {
        super(name);
    }
    public void sayHello(){
        System.out.println("你好,我是" + this.getName() + "。很高兴认识你。");
    }
    public void sayHelloToSomebody(String guestName) {
        System.out.println("你好,请问你是" + guestName + "吗?我是" +
                           this.getName() + "。很高兴认识你。");
    }
    public void sayHelloToSomebody(String firstGuestName, String secondGuestName) {
        System.out.print("你好,请问你们是" + firstGuestName + "和" + secondGuestName + "吗?我是" +
                         this.getName() + "。很高兴认识你们。");
    }
}
Lucy
张三

4.2.4. 构造函数重载及调用父类构造函数综合应用

public class Main {
    public static void main(String[] args) {
        People[] p = new People[4];
        p[0] = new ChinesePeople("张三", "北京市石景山区");
        p[1] = new ChinesePeople("李四", "男", "北京市海淀区");
        p[2] = new ChinesePeople("王五", "男", 28, "北京市朝阳区");
        p[3] = new People("Lucy", "Female", 23);
        for (int i = 0; i < 4; i++) {
            System.out.print(p[i].getName() + " " + p[i].getGender() + " " + p[i].getAge() + " " +
                             ((p[i] instanceof ChinesePeople) ?
                              ((ChinesePeople)p[i]).getAncestralHome() : "") +
                             " : ");
            p[i].sayHello();
        }
    }
}
class People {
    private String name;
    private String gender;
    private int age;
    People() {
        System.out.println("Constructor People() from People class.");
    }
    People(String name) {
        this.name = name;
    }
    People(String name, String gender) {
        this(name);
        this.gender = gender;
    }
    People(String name, String gender, int age) {
        this(name, gender);
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void sayHello() {
        System.out.println("Hi, I'm " + this.name + ". Nice to meet you!");
    }
}
class ChinesePeople extends People {
    private String ancestralHome;
    ChinesePeople(String name, String ancestralHome) {
        super(name);
        this.ancestralHome = ancestralHome;
    }
    ChinesePeople(String name, String gender, String ancestralHome) {
        super(name, gender);
        setAncestralHome(ancestralHome);
    }
    ChinesePeople(String name, String gender, int age, String ancestralHome) {
        super(name, gender, age);
        setAncestralHome(ancestralHome);
    }
    public String getAncestralHome() {
        return ancestralHome;
    }
    public void setAncestralHome(String ancestralHome) {
        this.ancestralHome = ancestralHome;
    }
    public void sayHello(){
        System.out.println("你好,我是" + this.getName() + "。很高兴认识你。");
    }
}
张三 null 0 北京市石景山区 : 你好,我是张三。很高兴认识你。
李四 男 0 北京市海淀区 : 你好,我是李四。很高兴认识你。
王五 男 28 北京市朝阳区 : 你好,我是王五。很高兴认识你。
Lucy Female 23  : Hi, I'm Lucy. Nice to meet you!

4.3. 对象初始化的过程

参考教材

  • 5.5.3 pp.131-133

4.3.1. 为对象分配内存空间后的属性单元初始化

class Main {
    public static void main(String[] args) {
        int i;
        boolean b;
        String s;
        System.out.println("i: " + i + ", b: " + b + ", s: " + s);
    }
}
javac Main.java
# Main.java:6: error: variable i might not have been initialized
#         System.out.println("i: " + i + ", b: " + b + ", s: " + s);
#                                    ^
# Main.java:6: error: variable b might not have been initialized
#         System.out.println("i: " + i + ", b: " + b + ", s: " + s);
#                                                  ^
# Main.java:6: error: variable s might not have been initialized
#         System.out.println("i: " + i + ", b: " + b + ", s: " + s);
#                                                                ^
# 3 errors
class Main {
    public static void main(String[] args) {
        Test test = new Test();
        System.out.println("i: " + test.i + ", b: " + test.b + ", s: " + test.s);
    }
}
class Test {
    int i;
    boolean b;
    String s;
}
i: 0, b: false, s: null
class Main {
    public static void main(String[] args) {
        Test test;
        System.out.println("test: " + test);
    }
}
class Test {
    int i;
    boolean b;
    String s;
}
javac Main.java
# Main.java:4: error: variable test might not have been initialized
#         System.out.println("test: " + test);
#                                       ^
# 1 error
class Main {
    public static void main(String[] args) {
        Test t = new Test();
        System.out.println("t.test: " + t.test);
    }
}
class Test {
    int i;
    boolean b;
    String s;
    Test test;
}
t.test: null
class Main {
    static int i;
    static boolean b;
    static String s;
    public static void main(String[] args) {
        System.out.println("i: " + i + ", b: " + b + ", s: " + s);
    }
}
i: 0, b: false, s: null

4.3.2. 执行显式初始化

class Main {
    public static void main(String[] args) {
        Test t = new Test();
        System.out.println("i: " + t.i + ", b: " + t.b + ", s: " + t.s);
    }
}
class Test {
    int i;
    boolean b = true;
    String s;
}
i: 0, b: true, s: null

4.3.3. 执行构造方法中的初始化

class Main {
    public static void main(String[] args) {
        Test t = new Test();
        System.out.println("i: " + t.i + ", b: " + t.b + ", s: " + t.s);
    }
}
class Test {
    int i;
    boolean b;
    String s;
    Test() {
        i = 100;
        b = true;
        s = "test";
    }
}
i: 100, b: true, s: test
public class Main {
    public static void main(String[] args) {
        People p = new ChinesePeople("张三");
        System.out.println(p.getName() + ", " +
                           p.getGender() + ", " +
                           p.getAge() + ", " +
                           ((ChinesePeople)p).getAncestralHome());
    }
}
class People {
    private String name;
    private String gender;
    private int age;
    People() {
        name = "Lucy";
        gender = "?";
    }
    People(String name) {
        this.name = name;
    }
    People(String name, String gender) {
        this(name);
        this.gender = gender;
    }
    People(String name, String gender, int age) {
        this(name, gender);
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public String getGender() {
        return gender;
    }
    public int getAge() {
        return age;
    }
}
class ChinesePeople extends People {
    private String ancestralHome;
    ChinesePeople(String name) {
        ancestralHome = "籍贯未填写";
    }
    ChinesePeople(String name, String ancestralHome) {
        super(name);
        this.ancestralHome = ancestralHome;
    }
    ChinesePeople(String name, String gender, String ancestralHome) {
        super(name, gender);
        this.ancestralHome = ancestralHome;
    }
    ChinesePeople(String name, String gender, int age, String ancestralHome) {
        super(name, gender, age);
        this.ancestralHome = ancestralHome;
    }
    public String getAncestralHome() {
        return ancestralHome;
    }
}
Lucy, ?, 0, 籍贯未填写

5. 包及其使用

参考教材

  • 5.6 pp.133-138

6. 接口

参考教材

  • 5.7 pp.138-142