[英]Can't override super class's method in sub class
我想在子類中重寫超類的方法。 但是,無論我對程序進行了什么更改。 它仍指示超類方法進行的結果。 那么,我的代碼有什么問題呢?
我想重寫getChildAge方法和getGreeting方法。 覆蓋后,它應該顯示14(如果設置了12),這只是報告實際年齡加2。對於getGreeting方法,它應該顯示“我是最好的”。 無論將什么參數傳遞給getGreeting方法。
這是我的代碼:
public class SchoolKid
{
private String childName;
private int childAge;
private String childTeacher;
private String greeting;
//Constructor sets the childName,age,childTeacher and greeting.
public SchoolKid(String childN,int a,String childT,String g)
{
childName = childN;
childAge = a;
childTeacher = childT;
greeting = g;
}
public void setChildName(String childN)
{
childName = childN;
}
public void setChildAge(int a)
{
childAge = a;
}
public void setChildTeacher(String childT)
{
childTeacher = childT;
}
public void setGreeting(String g)
{
greeting = g;
}
public String getChildName()
{
return childName;
}
public int getChildAge()
{
return childAge;
}
public String getChildTeacher()
{
return childTeacher;
}
public String getGreeting()
{
return greeting;
}
}
public class ExaggeratingKid extends SchoolKid
{
private int childAge;
private String greeting;
//Constructor sets the child name,age,childTeacher and greeting.
public ExaggeratingKid(String childNAME,int childAGE,
String childTEACHER,String GREETING)
{
super(childNAME,childAGE,childTEACHER,GREETING);
}
public void setChildAge(int a)
{
childAge = a;
super.setChildAge(childAge+2);
}
public void setGreeting(String g)
{
greeting = "I am the best.";
super.setGreeting("I am the best.");
}
public String toString()
{
String str;
str = "The child's name is "+super.getChildName()+
"\nThe child's age is "+super.getChildAge()+
"\nThe child's teacher is "+super.getChildTeacher()+
"\nThe greeting is: "+super.getGreeting();
return str;
}
}
如果您調用super方法,那么有什么好處呢? 不要在toString()
調用super
方法! 調用當前對象的方法this
對象。 另外,請勿給孩子打招呼或childAge字段。
例如,這沒有任何意義:
public void setChildAge(int a)
{
childAge = a;
super.setChildAge(childAge+2);
}
而是做:
@Override
public void setChildAge(int a)
{
// childAge = a; // the child shouldn't have this variable
super.setChildAge(a + 2);
}
同樣,這不好:
public void setGreeting(String g)
{
greeting = "I am the best.";
super.setGreeting("I am the best.");
}
而是簡單地做:
@Override
public void setGreeting(String g)
{
// but what is g for???
// greeting = "I am the best.";
super.setGreeting("I am the best.");
}
也許更好:
@Override
public String getGreeting() {
return "I am the best.";
}
並擺脫子類中的greeting和childAge字段。 但是,如果這樣做,則必須在子級的構造函數中調用此函數,以確保已設置問候語。
這是你的問題
public String toString()
{
String str;
str = "The child's name is "+super.getChildName()+
"\nThe child's age is "+super.getChildAge()+
"\nThe child's teacher is "+super.getChildTeacher()+
"\nThe greeting is: "+super.getGreeting();
return str;
}
每次您使用super
會員時。 只需從此方法中刪除super
關鍵字
public String toString()
{
String str;
str = "The child's name is "+ getChildName()+
"\nThe child's age is "+ getChildAge()+
"\nThe child's teacher is "+ getChildTeacher()+
"\nThe greeting is: "+ getGreeting();
return str;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.