简体   繁体   中英

In Java, is there any way to call this() and super() in same constructor?

I have two classes as below. I want to access this() and super() constructors in single constructor TestEmployee(). Current method fails to compile. Is there any other way in Java so I can call both this() and super() in same constructor's body.

class Employee{
   double salary;
   Employee(double salary){
      this.salary = salary;
   }
}
class TestEmployee extends Employee{
   TestEmployee(){
        super(1000000);
        this(10000);
   }
   double bonus;
   TestEmployee(double bonus){
       this.bonus = bonus;
   }
}

显然不行,super和this都必须是构造函数体的第一条语句。而且只能有一个第一条语句

It would not make sense because TestEmployee has a salary, which is passed in and sent to the base class Employee because TestEmployee derives from Employee . A bonus is a separate part of compensation and would be stored separately. Ideally, you would have a different constructor in the Employee base class which allowed for the passing of a salary and a bonus , which TestEmployee could access.

As others have said, you cannot do exactly what you want the way you're trying to approach it. What you could do, however, is design your classes a bit differently so that you can achieve the same goal.

class Employee{
   double salary;
   Employee(double salary){
      this.salary = salary;
   }
}
class TestEmployee extends Employee{
   TestEmployee(double salary, double bonus){
        super(salary);
        this.bonus=bonus;
   }

   double bonus;
}

Then if you wanted a default TestEmployee constructor, you could do:

TestEmployee() {
  this(1000000,10000);
}

To prevent code duplication,you can define a non-static method which does all your initlization block for all your constructors.You can include this method below your call to super() to initialize your object.But the drawback is ..it can be called from any other method within the class.So it is not a good design pattern

class TestEmployee extends Employee{

       double bonus;

       TestEmployee(){
            super(1000000);
            intialize(10000);
       }

       TestEmployee(double bonus){
          super(bonus);
           intialize(bonus);
       }
       private void intialize(double bonus)
       {
           this.salary = bonus;
       }
    }

You can't put in the same constructor this two calls because both of them need to be put first in the constructor when calls the parent class. You can use this and super in the same constructor when you have an extra parameter. Like in this exemple:

class TestEmployee extends Employee{
double: age;
   TestEmployee(double salary, double age){
        super(1000000);
        this.age=age;
   }
Or when you are using a Chaining Constructor, for example:
public class Employee{
String name;
double salary;
double age;
 public Employee()
    {
        this("James Bond", 34000);
    }

    public Employee(String n, double s)
    {
       this("Ana", 34000, 23);
    }
public Employee(String n, double s, double age)
    {
        name = n;
        salary = s;
        this.age=age;

    }

As others have said, why would you even want to. It makes no sense.
You can fake it by calling another constructor which in turn calls its superclass constructor. But if the only reason to call another constructor is to relay to another superclass constructor than the one that gets called implicity there's almost certainly something seriously wrong with your design.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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