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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16076317/
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-31 21:49:27  来源:igfitidea点击:

Java - Multiple constructors with same arguments

javaconstructorarguments

提问by J?cob

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

E.g.

例如

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

回答by BobTheBuilder

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 nullas the unneeded argument.

null用作不需要的参数。

回答by KyelJmD

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

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

回答by Stian Standahl

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 :)

这样做时,性能下降(非常非常小),但如果代码更具可读性,那就值得了:)

回答by Alpesh Gediya

I do not think multiple constructor with same arguments (data type) are allowed by java.

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

回答by NilsH

If you need flexible creation of objects, check out the Builderpattern. 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.

如果您需要灵活地创建对象,请查看Builder模式。但是,在您的情况下,我不明白为什么仅仅拥有一个带有所有参数的构造函数就行不通。只需从 resultSet 中获取所有属性,如果它们为 null,则不会设置它们。

回答by Vinigas

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 typeand switch. Just suggestion.

如果您有 2 个以上相同的构造函数,则使用int typeand switch。只是建议。

EDIT: typesin this case should be defined by enumfor easier code grasp.

编辑:types在这种情况下应该定义enum为更容易的代码掌握。

回答by javakurious

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 主要。

回答by Sarvar Nishonboev

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, ctorBananaand ctorCarrotinternally 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.

在 Java 中,不能有多个具有相同参数的方法(包括构造函数)。要解决此限制,您需要使用具有不同名称的静态方法,将客户端代码用作构造函数。在遵循静态方法的代码中ctorApplectorBanana并在ctorCarrot内部调用私有构造函数 Foo()并充当具有相同参数的多个构造函数的角色。前缀 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;
   }
}