2013-07-10 130 views
1

我想在我的子類中覆蓋超類的方法。但是不管我對我的程序做了什麼改變。它仍然表明由超類的方法進行的結果。那麼,我的代碼有什麼問題?無法在子類中覆蓋超類的方法

我想重寫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; 
} 
} 
+2

如果你打電話給超級方法有什麼好處呢?不要在'toString()'中調用'super'方法!調用當前對象的方法,'this'對象。此外,不要給孩子一個問候或childAge字段。 –

+0

我之前曾嘗試過,但會顯示孩子的年齡爲o,問候語爲空。 – zihaow

回答

4

如果您調用超級方法,重寫有什麼好處?不要撥打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."; 
} 

,擺脫在子類的問候和childAge領域。但是,如果你這樣做,你必須在孩子的構造函數中調用它,以確定問候已經設置。

+0

和String g是爲了什麼? – nachokk

+0

@nachokk:不錯,不知道他想幹什麼。或者,最好只是爲了重寫'getGreeting()' –

+0

upvote for setter方法 –

1

這是你的問題

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; 
}