java繼承之super關鍵字
阿新 • • 發佈:2021-02-05
技術標籤:java
class Person{
String name;
String address;
public Person(String name, String address) {//父類構造方法
this.name = name;
this.address = address;
System.out.println("父類構造方法被呼叫");
}
public void eat() {
System.out.println("人吃飯");
}
public void drink() {
System. out.println("人喝水");
}
public void printInfo() {
System.out.println("name="+name);
}
}
class Student extends Person{
public Student(String name, String address) {//子類構造方法
super(name,address);
System.out.println("子類構造方法被呼叫");
}
int score;
void goToScool() {
System. out.println("去學校");
}
public void eat() {
super.eat();//子類可以呼叫父類的方法,變數也可以呼叫
System.out.println("學生吃飯");
}
}
public class Test {
public static void main(String[] args) {
Student s = new Student("czx", "tdj");
s.eat();
}
}
paivate屬性與方法是不能被繼承的
執行結果