Avoiding nulls, immutability, object state

I like the concept of immutability. I also like the concept of no nulls (and if possible also no NullDesignPattern, no NullObjects... as much as possible). But what about the following scenario: I have an object User , which has two fields: birthday and dateInLifeMarried (could be any similar field; the important thing is that at first this field is null and changes at some point in object l

避免空值,不变性,对象状态

我喜欢不变性的概念。 我也喜欢没有空值的概念(如果可能的话,也尽可能地没有NullDesignPattern,没有NullObjects ...)。 但是,以下情况如何: 我有一个对象User ,它有两个字段: birthday和dateInLifeMarried (可以是任何类似的字段;重要的是,起初这个字段为null并且在对象生命中的某个时刻改变)。 由于它是不可变的,我希望这两个字段都在构造函数中: public User(birthday, dateInLifeMarried) 现在: 我

java optional parameter in methods

I want to make a method that takes 1 required parameter and 1 optional parameter, but I found how to make an optional array which is by making in the parameter (int... b) but this is for an array, I want to make it just either this value is null or the user entered it, I can make it by making 2 methods of the same name but one with the single parameter and one with the 2 parameters, but can it be

方法中的java可选参数

我想创建一个需要1个参数和1个可选参数的方法,但是我发现如何通过在参数(int ... b)中创建一个可选数组,但这是针对数组的,我想使它只是这个值为空或用户输入它,我可以通过使两个同名的方法,但一个与单个参数,一个与2个参数,但它可以用只有一个方法? 谢谢 不,Java不支持可选参数。 重载的另一种替代方法(对两个参数没有太大意义,但对于更多的参数确实有意义)是使用表示所有参数的构建器类型 - 您可以为构建器

Using an ArrayList as Parameter of Constructor

I'm trying to write a constructor for a class which accepts an ArrayList (containing integers) as one of it's arguments. When instantiating this class later, I will pass an appropriate, pre-populated list of values, so I don't want to create an empty list inside the constructor. Unfortunately, when I try to compile the below code, Java spits out five errors, all related to line 23

使用ArrayList作为构造函数的参数

我试图为一个接受ArrayList(包含整数)作为其参数之一的类编写构造函数。 稍后再实例化这个类时,我会传递一个适当的,预先填充的值列表,所以我不想在构造函数中创建一个空列表。 不幸的是,当我尝试编译下面的代码时,Java吐出了五个错误,都与第23行有关(我的构造函数的函数定义)。 任何意见,将不胜感激: /* * SumGenerator * * @author James Scholes */ import java.util.ArrayList; import java.util.Random

Android: How to set a default value for an argument variable

Android function PHP example: function HaHa($a = "Test") { print $a; } The question is how to do it in android... public void someFunction(int ttt = 5) { // something } The solution above doesn't work, how can I do it? Thanks! No, Java does not support default values for function parameteres. There's an interesting post about borrowing language features here: htt

Android:如何为参数变量设置默认值

Android功能 PHP示例: function HaHa($a = "Test") { print $a; } 问题是如何在android中执行它... public void someFunction(int ttt = 5) { // something } 上述解决方案无效,我该怎么办? 谢谢! 不,Java不支持函数参数的默认值。 这里有一篇关于借用语言功能的有趣帖子:http://java.dzone.com/news/default-argument-values-java 你可以像这样滥用重载: int someMethod() { return someMetho

Java alternative for C# code

This question already has an answer here: Does Java support default parameter values? 19 answers The structure of Java handles this through function overloading. Basically, create a function that doesn't have expectedDisplayValue as a parameter and that function would call VerifyControlState (identifier, controltype, buttonProperty, ""); See the Does Java support default par

Java替代C#代码

这个问题在这里已经有了答案: Java是否支持默认参数值? 19个答案 Java的结构通过函数重载来处理这个问题。 基本上,创建一个没有expectedDisplayValue作为参数的函数,该函数将调用VerifyControlState (identifier, controltype, buttonProperty, ""); 请参阅“是否支持Java”默认参数值? 问题的更多细节。

Constructors with default values of parameters

This question already has an answer here: Does Java support default parameter values? 19 answers No, Java doesn't support default values for parameters. You can overload constructors instead: public Shape(int v,int e) {vertices =v; edges = e; } public Shape() { this(1, 2); } No it doesn't. Java doesn't support default arguments in any function; constructors included. What

具有参数默认值的构造函数

这个问题在这里已经有了答案: Java是否支持默认参数值? 19个答案 不,Java不支持参数的默认值。 你可以重载构造函数: public Shape(int v,int e) {vertices =v; edges = e; } public Shape() { this(1, 2); } 不,它不。 Java不支持任何函数中的默认参数; 包括建设者。 你可以做的是定义public Shape(int v, int e) ,也是一个默认的构造函数 public Shape() { this(1, 2); } 请注意这里的特殊语法将构造委

Are default parameter values supported by Java?

Possible Duplicate: Does Java support default parameter values? Suppose I want to make default parameter value in C++, then we can express it as below. void functionName(char *param1, int param2=2); But if I want to make this in Java, then is it possible. Currently I am doing as below public functionName(String param1) { this(param1, 2); } public functionName(String param1, int param2)

Java支持默认参数值吗?

可能重复: Java是否支持默认参数值? 假设我想在C ++中创建默认参数值,那么我们可以如下表示它。 void functionName(char *param1, int param2=2); 但是如果我想用Java来做这件事,那么是否有可能。 目前我正在做如下 public functionName(String param1) { this(param1, 2); } public functionName(String param1, int param2) { .......... } 这在Java是不可能的,但是你希望你可以使用Builder模式,这就是Stack

Is it possible to declare default argument in Java in String?

This question already has an answer here: Does Java support default parameter values? 19 answers 不,你通常会这样做的方式是重载方法,如下所示: public void test() { test("exampleText"); } public void test(String name) { } No, it is not. However, the following is possible: public void test() { test("exampleText"); } public void test(String name) { //logic here }

是否有可能在String中声明Java中的默认参数?

这个问题在这里已经有了答案: Java是否支持默认参数值? 19个答案 不,你通常会这样做的方式是重载方法,如下所示: public void test() { test("exampleText"); } public void test(String name) { } 不它不是。 但是,以下是可能的: public void test() { test("exampleText"); } public void test(String name) { //logic here }

how to set default method argument values?

This question already has an answer here: Does Java support default parameter values? 19 answers You can accomplish this via method overloading. public int doSomething(int arg1, int arg2) { return 0; } public int doSomething() { return doSomething(defaultValue0, defaultValue1); } By creating this parameterless method you are allowing the user to call the parameterfull meth

如何设置默认方法参数值?

这个问题在这里已经有了答案: Java是否支持默认参数值? 19个答案 你可以通过重载方法来完成。 public int doSomething(int arg1, int arg2) { return 0; } public int doSomething() { return doSomething(defaultValue0, defaultValue1); } 通过创建这个无参数方法,您允许用户使用在无参数方法的实现中提供的默认参数来调用parameterfull方法。 这被称为重载该方法。 如果你的参数是相同的类型,

Java optional parameters

How do I use optional parameters in Java? What specification supports optional parameters? varargs could do that (in a way). Other than that, all variables in the declaration of the method must be supplied. If you want a variable to be optional, you can overload the method using a signature which doesn't require the parameter. private boolean defaultOptionalFlagValue = true; public voi

Java可选参数

我如何在Java中使用可选参数? 什么规范支持可选参数? 可变参数可以做到这一点(以某种方式)。 除此之外,必须提供方法声明中的所有变量。 如果您希望变量是可选的,则可以使用不需要参数的签名来重载方法。 private boolean defaultOptionalFlagValue = true; public void doSomething(boolean optionalFlag) { ... } public void doSomething() { doSomething(defaultOptionalFlagValue); } 有几种方法可以