2012-05-03 71 views
0

对不起,如果这是一个非常基本的,但我找不到任何东西在我的书和搜索。我在创建对象时尝试发送信息,但有很多选项,我只能真正改变它(不幸的是,它接近尾声)。有没有办法将值传递给构造函数中的特定项目?

说我有这样的事情:

class_to_start_(int maxActive, 
    byte whenExhaustedAction, 
    long maxWait, 
    int maxIdle, 
    int minIdle, 
    boolean testOnBorrow, 
    boolean testOnReturn, 
    long timeBetweenEvictionRunsMillis, 
    int numTestsPerEvictionRun, 
    long minEvictableIdleTimeMillis, 
    boolean testWhileIdle, 
    long softMinEvictableIdleTimeMillis, 
    boolean lifo) 

他们都有缺省值,因此我不需要改变任何人,但我想改变的只有最后一个lifo的默认值。我可以做到这一点吗?理想情况下,像class_to_start_('lifo'=True)(这是行不通的,我试了一下)。

这可能吗?

回答

3

Java没有默认参数值,所以简短答案是否定的。

您可以有但是重载的构造函数,让你有一个构造函数,只需要你想改变的说法:

/** Main constructor. */ 
public Foo(int maxActive, 
      byte whenExhaustedAction, 
      int minIdle, 
      boolean testOnBorrow, 
      boolean lifo) 

/** Convenience constructor. */ 
public Foo(boolean lifo) 
{ 
    this(1, 0x01, 3, false, lifo); // call the main constructor will default values 
} 

你也可以看看制作fluent interface生成器对象。然后这个想法是你做的事:

final Foo f = new FooBuilder().withLifo(false).build(); 
+0

是不是这个意思是我还是需要在默认参数中输入,如果我只想改变最后一个? –

+0

你必须提供默认值*某处*,是的。 –

+0

@learningJava:你*写'Foo'类,还是这是你正在使用的现有类? –

0

什么也可以工作是创建所需类的一个实例,并设置所有值所需的默认值:

// in your main method 
Foo myDefaults = new Foo(100, "name", false, true); 
myDefaults.setLifo(false); 
Foo someChangedInstance = myDefaults; 

...然后加构造函数的类,它的类的实例,并设置等于参数化实例的值的所有值:

// in your Foo class 
public Foo(int maxWidth, String standardName, boolean logEverything, boolean lifo) { 
    // ... 
} 

public Foo(Foo otherInstance) { 
    this.maxWidth = otherInstance.maxWidth; 
    this.standardName = otherInstance.standardName; 
    this.logEverything = otherInstance.logEverything; 
    this.lifo = otherInstance.lifo; 
} 

这样,你可以修改实例的唯一的单值的类,并创建只有一些修改的新实例。

不要忘记在更改myDefault后重置属性。

myDefault.setLifo(true); 
0

您可以添加一个构造函数map参数和值对,并使用默认值未指定任何参数。这样你只需要指定你想覆盖的参数。

0

当通常使用默认值并且有很多参数时,使用构造函数或工厂方法变得笨拙。最方便和优雅的方法是使用fluent interface的setter。

像这样:

公共MyClass类{

// Define default values in initializers 
private int maxActive = 5; 
private byte whenExhaustedAction = 'x'; 
private long maxWait = 999; 
private int maxIdle = 3; 
private int minIdle = 1; 
private boolean testOnBorrow = true; 
private boolean testOnReturn = false; 
private long timeBetweenEvictionRunsMillis = 5000; 
private int numTestsPerEvictionRun = 10; 
private long minEvictableIdleTimeMillis = 2000; 
private boolean testWhileIdle = false; 
private long softMinEvictableIdleTimeMillis = 0; 
private boolean lifo = true; 

// Only getter/setters for first two fields shown - others similar 

public int getMaxActive() { 
    return maxActive; 
} 

// Return "this" to create a fluent interface 
public MyClass setMaxActive(int maxActive) { 
    this.maxActive = maxActive; 
    return this; 
} 

public int getMaxIdle() { 
    return maxIdle; 
} 

public MyClass setMaxIdle(int maxIdle) { 
    this.maxIdle = maxIdle; 
    return this; 
} 

// Other getters/setters to follow 

}

使用它这个样子,只是呼吁要将默认值以外的值的字段的setter方法:

MyClass x = new MyClass().setMaxActive(5).setMaxIdle(100); 
相关问题