【问题标题】:Can't override super class's method in sub class不能在子类中覆盖超类的方法
【发布时间】:2013-07-07 19:53:13
【问题描述】:

我想在我的子类中覆盖超类的方法。但无论我对我的程序做了什么改变。它仍然指示超类方法处理的结果。那么,我的代码有什么问题?

我想重写 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 字段。
  • 我之前试过了,但是会提示孩子的年龄是o,问候语是null。

标签: java overriding subclass superclass


【解决方案1】:

如果调用 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 字段。但是如果你这样做,你必须在孩子的构造函数中调用 this 以确保设置了问候语。

【讨论】:

  • 而字符串 g 是为了什么??
  • @nachokk:是的,不知道他想做什么。或者最好只是覆盖getGreeting()
  • 补充说明:super类最好有默认构造函数,否则子类创建默认构造函数时会出现意外编译错误,++。
【解决方案2】:

这是你的问题

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-14
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    相关资源
    最近更新 更多