简体   繁体   中英

Java - Multiple constructors with same arguments

I need to create multiple constructors with same arguments so that I can call these from my DAO class for populating different drop down values

public static Employee empType(String empCode, String empType) {

    Employee emp = new Employee();
    emp .empCode= empCode;
    emp .empType= empType;
    return emp ;
}

 public static Employee empDept(String deptCode, String deptName) {

    Employee emp = new Employee();
    emp .deptCode= deptCode;
    emp .deptName= deptName;
    return emp ;
}

When I am referencing from DAO class, how can I refer to these constructors?

Eg

private static Employee myList(ResultSet resultSet) throws SQLException {
    return new <what should be here>((resultSet.getString("DB_NAME1")), 
                      (resultSet.getString("DB_NAME2")));
}

You cant. Also, these functions aren't constructors. And how do you want to decide which "constructor" to call???

You can merge both functions:

public static Employee createEmp(String empCode, String empType, String deptName) {
    Employee emp = new Employee();
    emp .empCode= empCode;
    emp .empType= empType;
    emp .deptName= deptName;
    return emp ;
}

And use null as the unneeded argument.

You cannot Create multiple constructors/methods with the same name and same arguments

What you can do is change your implementation, and those are not Contructors.

You can follow what Baraky did, you can also use this(create a boolean flag,or an int value flag)

public Employee empType(String val1, String val2, int type) {

     Employee emp = new Employee();

    if(type == 1){
          emp .empCode= val1;
          emp .empType= val2;
    }else if(type ==2){
          emp.deptCode= val1;
          emp .deptName= val2;
     }
    return emp ;
}

If you must have multiple constructors you could add a dummy parameter like this.

public static Employee empType(String empCode, String empType) {

    Employee emp = new Employee();
    emp .empCode= empCode;
    emp .empType= empType;
    return emp ;
}

 public static Employee empDept(String deptCode, String deptName, bool dummy) {

    Employee emp = new Employee();
    emp .deptCode= deptCode;
    emp .deptName= deptName;
    return emp ;
}

When doing this it is a minimal performance(veryveryvery small) drop, but if the code is more readable it is worth it :)

From the article :

In Java you cannot have multiple methods (including constructors) with the same arguments. To get around this limitation you need to use static methods with different names that serve the client code as constructors. In the code that follows the static methods ctorApple , ctorBanana and ctorCarrot internally call the private constructor Foo() and serve in the role of multiple constructors with the same arguments. The prefix ctor is intended to remind the client that these methods serve in the role of constructors.

class Foo
{
   private Foo()
   {
     // ...
   }
   public static Foo ctorApple(/* parameters */)
   {
       Foo f = new Foo();
       // set properties here using parameters
       return f;
   }
   public static Foo ctorBanana(/* parameters */)
   {
       Foo f = new Foo();
       // set properties here using parameters
       return f;
   }
   public static Foo ctorCarrot(/* parameters */)
   {
       Foo f = new Foo();
       // set properties here using parameters
       return f;
   }
}

我认为 java 不允许具有相同参数(数据类型)的多个构造函数。

If you need flexible creation of objects, check out the Builder pattern. In your case however, I don't see why it shouldn't work just to have one constructor with all your parameters. Just fetch all the properties from the resultSet, and if they're null, they'll just not be set.

If you have TWO same constructor with same parameter meaning but different effect, you can try to clutch them into one:

public Name(Type parameter, boolean type) {
    if(type) {

    }else {

    }
}

If you have 2+ same constructor then use int type and switch . Just suggestion.

EDIT: types in this case should be defined by enum for easier code grasp.

If using a different set of parameters for the constructor changes the behavior how it acts, I would suggest extending or using composition to create a new class with the new set of parameters.

That will also adhere to OPEN/CLOSE pricipal.

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