java 将超类的属性传递给子类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6874500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:41:21  来源:igfitidea点击:

passing attributes of superclass to subclass

java

提问by Gostores

I have Employee, manager class.

我有员工,经理类。

employee has private String name, int age, double salary. they have to be placed in one method. i placed them in String details

员工有私人字符串名称,整数年龄,双倍工资。它们必须放在一种方法中。我把它们放在字符串细节中

public void details(String name,int age, double salary)
{
    this.name=name;
    this.age=age;
    this.salary=salary;
  }

then they have to be passed down to class manager but theres a new attribute to be added, which is department.

然后它们必须传递给班级经理,但要添加一个新属性,即部门。

class Manager extends Employee{
    private string department;
}

thats my problem, i cant output the attributes together with the attribute department.

那就是我的问题,我无法将属性与属性部门一起输出。



UPDATE #1

更新 #1

I've tried using this code before:

我之前尝试过使用此代码:

public void details(String name, int age, double salary, String dept) 
{
   super(name, age, salary);
   this.dept = dept;
}

But it gives me error that the call to super must be first statement in constructor.

但是它给我的错误是对 super 的调用必须是构造函数中的第一条语句。

this is how my manager extends employee class looks like.

这就是我的经理扩展员工类的方式。

class Manager extends Employee 
{
    private String department;
    public Manager(String name,int age,double salary,String department)
    {
        super(name,age,salary);
        this.department=department;
     }

     public String getDetails()
     {
         return super.details;
     }

     public void details(String name, int age, double salary, String department) 
     {
         super(name, age, salary);
         this.department = department;
     }
}


UPDATE #2

更新 #2

The attributes name, age and salary has to be private. I've tried changing it to public and use super.details(name age salary)and it worked. I've tried the same codes but with private attribute but it didn't work .

属性名称、年龄和薪水必须是私有的。我试过把它改成 public 并使用super.details(name age salary),它奏效了。我尝试了相同的代码,但具有私有属性,但没有用。

回答by Jon7

Private means private, as in, no other class can access them. Your two options are: make getter/setter methods in the parent class or make the variables protected so "Manager" can see them. I'd give my opinion on which is better, but you haven't told us the "why" piece of your question...

Private 意味着私有,因为没有其他类可以访问它们。您的两个选择是:在父类中创建 getter/setter 方法或将变量设置为 protected 以便“Manager”可以看到它们。我会就哪个更好发表我的意见,但你还没有告诉我们你的问题的“为什么”部分......

回答by Codeman

You need to include the values your subclass inherits as properties.

您需要包含子类作为属性继承的值。

class Employee
{
    private String name;

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

    public String getName() 
    {
        return name;
    }

    public void setName(String name)
    {
         this.name = name;
    }
}

回答by Teocci

Your error is that you are trying to call details(String name, int age, double salary)from details(String name, int age, double salary, String dept)using the keyword super. These methods have different signature. So, you cannot call superin the subclass method details.

你的错误是,你试图调用details(String name, int age, double salary)details(String name, int age, double salary, String dept)使用关键字super。这些方法有不同的签名。因此,您不能调用super子类方法的详细信息。

The signature of a method (with exception of the constructors) are composed of three elements. The method's visibility, name, value that returns and parameters.

方法的签名(构造函数除外)由三个元素组成。方法的可见性、名称、返回值和参数。

In this case, The Manager method public void details(String name, int age, double salary, String department)is public, returns void, its name is details and has four parameters a String, int, double and String again, but the Employee method public void details(String name, int age, double salary, String department)is different in the parameters. This method has only three parameters a String, int and double.

在这种情况下,Manager方法public void details(String name, int age, double salary, String department)是public,返回void,它的名字是details,又是String,int,double和String四个参数,但是Employee方法public void details(String name, int age, double salary, String department)的参数不同。这个方法只有三个参数,String、int 和 double。

In this situation you can call details directly or override the method (the second solution can not assign departmentto the details. So, I recommend you the direct call).

在这种情况下,您可以直接调用详细信息或覆盖方法(第二种解决方案不能分配department给详细信息。因此,我建议您直接调用)。

class Employee
{
    private String name;
    private int age;
    private double salary;

    public Employee (String name, int age, double salary, String department)
    {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public void details(String name, int age, double salary)
    {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName() 
    {
        return name;
    }

    public void setName(String name)
    {
         this.name = name;
    }
    ...
}

class Manager extends Employee 
{
    private String department;

    public Manager(String name, int age, double salary, String department)
    {
        // Here is correct to call super() because the constructors have the same signature.
        // This is, the method visibility and name. The parameters are optional.
        super(name, age, salary);
        this.department = department;
    }

    // Direct call.
    public void details(String name, int age, double salary, String department) 
    {
        // Here you CANNOT use super because in Employee there no method with the same signature.
        // So, just call the method details(name, age, salary) that is public.
        details(name, age, salary);
        this.department = department;
    }

    // Overriding the method
    @Override
    public void details(String name,int age, double salary)
    {
        // Here is correct to call super() because you are overriding the super class method.
        // The problem is that this method doesn't have the parameter deparment.
        super(name, age, salary);
        // Do something else
    }
    ...
}

回答by Mark

Probably the best way to handle this is for the Manager class to override the details() method, adding the new parameter:

处理这个问题的最好方法可能是让 Manager 类覆盖 details() 方法,添加新参数:

Manager class

经理类

public void details(String name, int age, double salary, String dept) {
   super.details(name, age, salary);
   this.dept = dept;
}