第三知识单元

面向对象程序设计

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

Created: 2023-09-12 Tue 00:25

0.1. 互动课堂

Click to host the seminar.

0.2. 签到

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

0.3. 本次课的目标

  • 掌握类的继承
  • 学会在设计类时使用继承

1. 域的继承与隐藏

子类 可以继承 父类所有 非私有域

参考教材

  • 5.2.2 pp.101-105

2. 方法的继承与覆盖

public class Main {
    public static void main(String [] args) {
        Campus201Card my201Card = new Campus201Card();
        my201Card.setCampusName();
        my201Card.performCharge();
        System.out.println(my201Card.getInfo());
    }
}
class Campus201Card extends PhoneCard {
    String campusName;
    void setCampusName() {
        campusName = "NCUT";
    }
    String getInfo() {
        // String s = "CampusName: " + campusName + ", Balance: " + balance;
        String s = "CampusName: " + campusName + ", " + super.getInfo();
        return s;
    }
}
class PhoneCard {
    double balance;
    void performCharge() {
        balance += 100.0;
    }
    String getInfo() {
        String s = "Balance: " + balance;
        return s;
    }
}
CampusName: NCUT, Balance: 100.0

3. thissuper

3.1. this

参考教材

  • 5.2.4 pp.108-110

3.2. super

参考教材

  • 5.2.4 pp.110-111