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

2 回答 2

4

如果调用 super 方法,重写有什么好处?不要调用super方法toString()!调用当前对象的方法,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 字段。但是如果你这样做,你必须在孩子的构造函数中调用 this 以确保设置了问候语。

于 2013-07-10T01:58:05.000 回答
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;
}
于 2013-07-10T01:58:00.600 回答