我對多態性感到困惑,我想知道這是否考慮多態?這是考慮多態嗎?
我覺得它看起來很奇怪,但它仍然編譯正確。
public class Family {
void FamilyInfo() {
System.out.println("This is a family super class");
}
}
public class Grandparents extends Family {
void FamilyInfo() {
System.out.println("Graparents are the elders in the family and they are the sub class of family");
}
}
public class Parents extends Grandparents {
void FamilyInfo() {
System.out.println("The parents are the children of the grandparents and they are the sub sub class for family");
}
}
public class FamilyDemo {
public static void main(String ary[]) {
Grandparents Gp = new Grandparents();
Parents P1 = new Parents();
Gp.FamilyInfo();
P1.FamilyInfo();
}
}
我想表現出多態,你應該建立一個家庭變量,分配給它一個新的父母()對象,並調用其'FamilyInfo()'方法。這樣你就可以看到對象的方法被調用,而不是變量類型的方法。另外,您應該學習Java命名約定,並在代碼中使用它。變量和方法名稱應以小寫字母開頭,而類名稱應以大寫字母開頭。 –