面向对象程序设计
Created: 2023-09-12 Tue 00:25
子类 可以继承 父类 的 所有 非私有域 。
参考教材
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
this
与 super
this
参考教材
super
参考教材