简体   繁体   English

在Java构造函数中有固定的参数 - 可能吗?

[英]Have fixed parameters in Java constructor - possible?

I've defined an object in Java - as far as the Java is concerned, they're the same thing, but as far as the data which populates them is concerned, they can be one of three types (wildly named 1,2,3 with 0 for the "root"). 我在Java中定义了一个对象 - 就Java而言,它们是相同的,但就填充它们的数据而言,它们可以是三种类型之一(命名为1,2, 3为0表示“root”)。
What I'd really like to be able to do is have four constructors defined, as they need slightly different params for each type. 我真正希望能够做的是定义了四个构造函数,因为它们需要每种类型略有不同的参数。 I could do it with strategic nulls, but it seems like the wrong thing to do. 我可以用战略空值做到这一点,但这似乎是错误的做法。 What I'd love to have is something like: 我喜欢的是:

public MenuNode(int type = 1, param1, param2, param3) {
    doStuffHere();
} 
public MenuNode(int type = 2, paramX, paramY) {
    doStuffHere();
}

and then call something along the lines of: 然后调用以下内容:

switch (toQueue.itemType) {

    when ITEM_TYPE_STATIC {
        MenuNode mn1 = new MenuNode(ITEM_TYPE_STATIC, param1, param2, param3);
    }

    when ITEM_TYPE_DYNAMIC {
        MenuNode mn2 = new MenuNode(ITEM_TYPE_DYNAMIC, paramX, paramY);
    }

}

etc etc. 等等

I hope this makes some sort of sense - it's a bit out there, and Googling only comes up with references to public static void etc. If someone with a bit more experience/know-how with Java than me can take a look, eternal love and gratitude will be forthcoming. 我希望这有点道理 - 它有点像那里,谷歌搜索只提到公共静态空白等等。如果有人比我更有经验/知识的Java可以看看,永恒的爱并将表达感激之情。

Another approach is to avoid using a constructor here, and create a static factory method which calls a private constructor: 另一种方法是避免在这里使用构造函数,并创建一个调用私有构造函数的静态工厂方法:

class MenuNode {
    private MenuNode() {
        // Does nothing important
    }
    public static MenuNode createStatic(param1, param2, param3) {
         MenuNode result = new MenuNode();
         result.setItemType(ITEM_TYPE_STATIC);
         result.setParam1(param1);
         result.setParam2(param2);
         result.setParam3(param3);
         result.doStuffHere();
         return result;
    }
    public static MenuNode createDynamic(paramX, paramY) {
         MenuNode result = new MenuNode();
         result.setItemType(ITEM_TYPE_DYNAMIC);
         result.setParamX(paramX);
         result.setParamY(paramY);
         result.doStuffHere();
         return result;
    }

If I understood your scenario right, you may want to thing about refactoring this approach. 如果我理解你的场景是正确的,你可能想要重构这种方法。

Make MenuNode a base class, and inherit your subtypes from it ( MenuNodeType1 , MenuNodeType2 etc.). 使MenuNode成为基类,并从中继承您的子类型( MenuNodeType1MenuNodeType2等)。 That way you'll get control over the single types but you can still throw them all into one collection. 这样你就可以控制单个类型,但你仍然可以将它们全部放入一个集合中。

If I understand you correctly, you want the constraint that the value of the first argument to each constructor must match the constructor being called. 如果我理解正确,你需要约束,即每个构造函数的第一个参数的值必须与被调用的构造函数匹配。

Simple: 简单:

public MenuNode(int type, param1, param2, param3) {
    if (type != 1) throw new IllegalArgumentException ("type must be 1")
    doStuffHere();
} 
public MenuNode(int type, paramX, paramY) {
    if (type != 2) throw new IllegalArgumentException ("type must be 2")
    doStuffHere();
}

If you have two or more types that have the same list of parameters associated with them, the validation would look more like this; 如果您有两个或更多类型具有与之关联的相同参数列表,则验证将更像这样;

    if (type != 1 && type != 3) throw new IllegalArgumentException ("type must be 1 or 3");

Consider using factory methods to construct your MenuNodes : 考虑使用工厂方法构建MenuNodes

public MenuNode createTypeOneMenuNode(Object param1, Object param2, Object param3) {
    ...
}

public MenuNode createTypeTwoMenuNode(Object paramx, Object paramy) {
    ...
}

You are probably looking for method overloading 您可能正在寻找方法重载

I don't see why you need to add the type to the call -> if you call it with X parameters, that you know what type it should be., right? 我不明白为什么你需要在调用中添加类型 - >如果用X参数调用它,你知道它应该是什么类型。对吧?

Another solution if your constructors become hard to distinquish is to use static factory methods because these can have descriptive names. 如果您的构造函数难以区分的另一种解决方案是使用静态工厂方法,因为它们可以具有描述性名称。 But as Bobby mentioned this seems to be more a case for refactoring to multiple types. 但正如Bobby所说,这似乎更适用于重构多种类型。

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

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