在Java构造函数中有固定的参数

在Java构造函数中有固定的参数

本文介绍了在Java构造函数中有固定的参数 - 可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Java中定义了一个对象 - 就Java而言,它们是同样的东西,但就填充它们的数据而言,它们可以是三种类型之一(通常命名为1 ,2,3为0,表示根)。

我真正想要做的是定义四个构造函数,因为每个类型需要稍微不同的参数。我可以做战略null,但它似乎是错误的事情做。我想要的是类似:

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

然后调用以下命令:



switch(toQueue.itemType){

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

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

}



我希望这有一定的意义 - 这是一个有点,和谷歌只提出参考public static void等
如果有更多的经验

解决方案

另一种方法是使用Java,避免在这里使用构造函数,并创建一个调用私有构造函数的静态工厂方法:

  class MenuNode {
private MenuNode(){
//没有什么重要的
}
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;
}


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").
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.

解决方案

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

这篇关于在Java构造函数中有固定的参数 - 可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 03:19