【问题标题】:trying to call superclass method in subclass试图在子类中调用超类方法
【发布时间】:2018-11-19 19:37:12
【问题描述】:

可能是一个非常菜鸟的问题,但我无法弄清楚。我有一个 Person 类来存储从键盘输入的名称

public class Person {
    private String firstName;
    private String lastName;

    public Person()
    {
        firstName = "";
        lastName = "";
    }

    public Person(String first, String last)
    {
        setName(first, last);
    }

    public String toString()
    {
        return(firstName + " " + lastName);
    }

    public void setName(String first, String last)
    {
        firstName = first;
        lastName = last;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public String getLastName()
    {
        return lastName;
    }
}

我正在尝试在名为 Patient 的子类中调用 toString 方法

public class Patient extends Person {
    private int patientID, patientAge;
    public Patient()
    {
       patientID = 0; //for a different part of the class that works
       patientAge = 0;
    }
    @Override
    public String toString()
    {
        return ("Patient Name: "+super.toString());
    }
}

当我从 Patient 类调用 toString 方法时,我无法让它在 main 中输出名称,但是当我测试它时,当我从 Person 类调用 toString 方法时它会输出名称。 main 中的方法调用看起来像 Patient pnt = new Patient(); System.out.print(Pnt.toString()); 它在控制台“Patient Name:”中打印出来。关于我做错了什么的任何反馈或关于如何让它发挥作用的想法

【问题讨论】:

  • 你是如何构建你的Patient 对象的?名字和姓氏似乎没有设置。
  • 请发表您的主要方法
  • 只是为了记录,因为新手经常忘记这一点:请不要忘记在某个时候接受答案!
  • 问题是你没有设置名字。现在已经在 cmets 上提出了解决方案。要么使用你的设置器或伸缩构造函数,并在你的主类或子类构造函数中设置名称。还要记住,setter 是继承的,所以你可以构造一个病人,使用 setter 作为名称并打印它

标签: java subclass superclass


【解决方案1】:

这里:

public Person()
{
    firstName = "";
    lastName = "";
}

您的子类缺少对超类构造函数的合理调用。因此,当您实例化您的 Patient 对象时,会使用上述构造函数,并且所有患者都以“”作为名字和姓氏结束!

当您创建患者时,患者也应该有一个姓名!但是您在 Patient 中的构造函数仅设置 Patient 相关字段。并且隐含地调用了默认的超级构造函数。因此,Person 字段都设置为空字符串!

更好的方法如下所示:

class Person {
  private final String firstName;
  ... lastName

  public Person(String firstName, String lastName) {
   this.firstName = firstName;
  ...

然后

class Patient extends Person {
  private final int patientID;

  public Patient(int patientID, String firstName, String lastName) {
    super(firstName, lastName);
    this.patientID = patientID;
  )

为什么会更好:名称和 ID 不会改变(通常)。为它们设置吸气剂是没有意义的。您创建一次对象,然后该数据就被修复了!在 Person 中使用默认构造函数也有没有点。 名字的人没有意义。因此:不要创建允许您创建“无效”对象的类。你的类模型现实。没有名字就没有真正的人!

还有一个提示:在覆盖方法时使用@Override,以便编译器可以在您出错时告诉您!

【讨论】:

    【解决方案2】:

    如果问题是当您从 Patient 类调用 toString 方法时在 main 中输出名称,我认为下面的代码会对您有所帮助。

    您是否尝试过像这样构造Patient 对象?

        public static void main(String[] args) {
    
        Patient p = new Patient();
        System.out.println(p.toString());
    
    }
    

    【讨论】:

      【解决方案3】:

      其实我没有看到你的代码有问题。

      Person person = new Person();
      person.setName("aa", "bb");
      System.out.println(person);  // aa bb
      
      Patient patient = new Patient();
      patient.setName("cc", "dd");
      System.out.println(patient);  // Patient Name: cc dd
      

      我认为您设置的名称错误 pr 使用不正确的参考。检查一下。

      【讨论】:

        【解决方案4】:

        您的 PATIENT 子类没有任何构造函数。您没有为任何患者设置任何名字或姓氏。 为了保持您在父类中使用的熟悉的构造函数,请使用:

        public Patient() {
            super("default_firstName", "default_lastName");
            this.patientID = 0;
            this.patientAge = 0;
        }
        public Patient(String firstName, String lastName, int patientAge) {
            super(firstName, lastName);
            this.patientID = 0; //can be implemented some method for automatically setting numbers
            this.patientAge = patientAge;
        }
        

        这样,即使构造函数被调用为空,你也总能得到 firstName 和 lastName。 根据你的 toString 方法,它是正确的,它调用了超类方法:

        @Override
        public String toString()
        {
            return("Patient name is "+super.toString());
        }
        

        但请注意,您返回 STRING 值以便使其在屏幕上可见,请记住使用:

        System.out.println(patient.toString());
        

        然后它就会可见:)

        【讨论】:

          【解决方案5】:

          我在您的 Person 类中添加了一些 cmets 和代码,应该可以解决您的问题。

          public class Person {
          private String firstName; //store the first name
          private String lastName; //sore the last name
          
          //initialize firstName and lastName to an empty string
          public Person() {
              firstName = "";
              lastName = "";
          }
          
          //set firstname and lastname according to the parameters.
          public Person(String first, String last) {
              //setName(first, last); remove this crap.
              // Use the contructor properly when initialize your person object. Like this:
              this.firstName = first;
              this.lastName = last;
          }
          
          //method to output the first name and last name
          @Override
          public String toString() {
              return (firstName + " " + lastName);
          }
          
          //method to set firstName and lastName according to the paramters
          public void setName(String first, String last) {
              //
              firstName = first;
              lastName = last;
          }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-10-24
            • 2012-04-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多