简体   繁体   English

super()的正确语法?

[英]Proper syntax for super()?

When I use super() to use a superclass method, what is the proper way to return? 当我使用super()使用超类方法时,正确的返回方法是什么?

Let's say I have a class Hourly. 假设我有一小时的课。 It contains a constructor also called Hourly that has parameters "String name" and "double rate. The UML version looks like: 它包含一个也称为Hourly的构造函数,其参数为“字符串名称”和“双倍速率”。UML版本如下所示:

+Hourly(name:String, rate:double) +小时(名称:字符串,速率:双精度)

However, the "name" variable is a private attribute of the Employee class, of which the class hourly is related to via inheritance. 但是,“名称”变量是Employee类的私有属性,每小时与该类通过继承相关联。 By this I mean that the UML show a clear arrow (not a diamond) going from the class Hourly and pointing to the class Employee. 我的意思是说,UML从Hourly类指向Employee类显示一条清晰的箭头(不是菱形)。

How would I write the constructor Hourly??? 我将如何编写构造函数Hourly?

I have a basic skeleton of: 我有以下基本知识:

public Hourly(String name, double rate){


}

Please help me fill it in. 请帮我填写。

On a separate note, let's say that there was a return in a method. 单独说,方法有一个返回。 Say I wanted to return the double rate . 假设我想退还double rate What would be the proper syntax to return some that uses super() as I know I couldn't simply use: 返回使用Super()的某些语法的正确语法是什么,我知道我不能简单地使用:

return this.rate;

Your Employee surely has a name. 您的Employee肯定有一个名字。

private String name;

public Employee(String name) {
    this.name = name;
}

Following that, your Hourly must also have a name. 之后,您的Hourly还必须有一个名称。 Since an Hourly is-an Employee , you must set the name on it as well. 由于“ Hourly是“ Employee ,因此您还必须在其上设置名称。 That is accomplished like so. 这样就完成了。

public Hourly(String name, double rate) {
    super(name);
    this.rate = rate;
}

According to your comments, name is stored by the superclass; 根据您的评论,名称由超类存储; rate is not. 率不是。 So you should store rate in the Hourly class you're defining, while passing name to the super constructor, as follows: 因此,应在将名称传递给超级构造函数的同时,将速率存储在要定义的Hourly类中,如下所示:

public class Hourly {

  private double rate;

  public Hourly(String name, double rate) {
    super(name);
    this.rate = rate;
  }

  public double getRate() {
    return rate;
  }
}

If Hourly is sub class of Employee and if you want to parse the name to super class (Employee) then in your Hourly constructor call super(name); 如果Hourly是Employee的子类,并且您想将名称解析为超类(Employee),则在Hourly构造函数中调用super(name);

public Hourly(String name, double rate){
    super(name);
}

Since Hourly extends Employee : 由于Hourly extends Employee

    class Hourly extends Employee {

    private int rate;

    public Hourly(String name, int rate) {
      super(name);  //assuming Employee has this constructor
      this.rate = rate;
    }

    //this make sense if name is protected and doesn't have getter in base class
    //ideally you would have this in base class itself
    public String getName() {
       return super.name; //for rate there is no sense in using super as it is not known to super class
    }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM