简体   繁体   English

Java - 具有相同参数的多个构造函数

[英]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我需要创建多个具有相同参数的构造函数,以便我可以从我的 DAO 类中调用这些构造函数来填充不同的下拉值

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?当我从 DAO 类引用时,如何引用这些构造函数?

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.并使用null作为不需要的参数。

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)你可以按照 Baraky 所做的,你也可以使用这个(创建一个布尔标志,或一个 int 值标志)

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.在 Java 中,不能有多个具有相同参数的方法(包括构造函数)。 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.在静态方法ctorApple的代码中, ctorBananactorCarrot内部调用私有构造函数Foo()并充当具有相同参数的多个构造函数的角色。 The prefix ctor is intended to remind the client that these methods serve in the role of constructors.前缀 ctor 旨在提醒客户端这些方法充当构造函数的角色。

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.如果您需要灵活地创建对象,请查看Builder模式。 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.只需从 resultSet 中获取所有属性,如果它们为 null,则不会设置它们。

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 .如果您有 2 个以上相同的构造函数,则使用int typeswitch Just suggestion.只是建议。

EDIT: types in this case should be defined by enum for easier code grasp.编辑:这种情况下的types应该由enum定义,以便于代码掌握。

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.这也将遵守 OPEN/CLOSE 主要。

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

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