将超类的属性传递给子类

时间:2011-07-29 14:15:32

标签: java

我有员工,经理班。

员工拥有私人String名称,int age,double salary。他们必须放在一个方法中。我把它们放在字符串细节

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

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

class Manager extends Employee{
    private string department;
}

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


更新#1

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

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

但它给出了错误,即对super的调用必须是构造函数中的第一个语句。

这就是我的经理如何扩展员工类。

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

更新#2

属性名称,年龄和工资必须是私有的。我已经尝试将其更改为公开并使用super.details(name age salary)并且它有效。我尝试了相同的代码,但有私有属性,但它没有用。

4 个答案:

答案 0 :(得分:2)

私有意味着私有,因为没有其他类可以访问它们。您的两个选项是:在父类中创建getter / setter方法或使变量受到保护,以便“Manager”可以看到它们。我会对哪个更好做出自己的看法,但你没有告诉我们你问题的“原因”...

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

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

在这种情况下,Manager方法public void details(String name, int age, double salary, String department)是公共的,返回void,它的名称是详细信息,并且有四个参数,String,int,double和String,但Employee方法public void details(String name, int age, double salary, String department)是不同的在参数中。这个方法只有三个参数,一个是String,int和double。

在这种情况下,您可以直接调用详细信息或覆盖方法(第二个解决方案无法将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
    }
    ...
}

答案 3 :(得分:-1)

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

经理类

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