无法覆盖子类中的超类方法

时间:2013-07-10 01:54:12

标签: java override subclass superclass

我想在子类中覆盖超类的方法。但无论我对我的节目做了什么改变。它仍然表示超类的方法继续进行的结果。那么,我的代码有什么问题?

我想覆盖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 个答案:

答案 0 :(得分:4)

如果你打电话给超级方法有什么好处?不要在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字段。但是如果你这样做,你必须在孩子的构造函数中调用它,以确保设置问候语。

答案 1 :(得分: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;
}