java 如何在同一行实例化对象并调用setter?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6335130/
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
How to instantiante object & call setter on same line?
提问by Tigmal
If I have an Employee
class with a default constructor:
如果我有一个Employee
带有默认构造函数的类:
private String firstName;
public Employee(){}
and a setter:
和一个二传手:
public void setFirstName(String firstName){
this.firstName = firstName;
}
Why does this attempt fail to instantiate and call the setter in the same line?
为什么此尝试无法实例化并在同一行中调用 setter?
Employee employee = new Employee().setFirstName("John");
回答by stevevls
You can also use this syntax:
您还可以使用以下语法:
Employee employee = new Employee() {{
setFirstName("John");
}};
Though keep in mind that it's going to create an anonymous inner class and probably isn't what you want.
但请记住,它会创建一个匿名内部类,可能不是您想要的。
回答by Steve Kuo
Because setFirstName
doesn't return anything. If you want to chain methods then setFirstName
would have to return Employee
.
因为setFirstName
不返回任何东西。如果要链接方法,则setFirstName
必须返回Employee
.
Another approach is to have a constructor that takes firstName
as an argument.
另一种方法是使用一个构造函数firstName
作为参数。
回答by Sorrow
The method serFirstName
is of return type void
(nothing). Try:
该方法serFirstName
是返回类型void
(无)。尝试:
public Employee setFirstName(String fname) {
this.firstName = fname;
return this;
}
回答by T-Bull
(employee = new Employee()).setFirstName("John");
performs instantiation and calling the setter, as you requested in the headline, but does not declare the variable as suggested in your code example.
按照标题中的要求执行实例化和调用 setter,但没有按照代码示例中的建议声明变量。
(Employee employee = new Employee()).setFirstName("John");
will probably not work, I assume. But you can try.
我想可能不会工作。不过你可以试试。
Of course, you can always stuff multiple statements in one line.
当然,您始终可以在一行中填充多个语句。
Employee employee; (employee = new Employee()).setFirstName("John");
or
或者
Employee employee = new Employee(); employee.setFirstName("John");
If I were you, I would settle for a parameterized constructor, though.
不过,如果我是你,我会满足于参数化构造函数。
回答by Eng.Fouad
It should be like this:
应该是这样的:
Employee employee = new Employee();
employee.setFirstName("John");
回答by LouwHopley
Because the you want to set employee
to the value of .setFirstName("John");
which does not return anything because it is a void
因为你想设置employee
为它的值.setFirstName("John");
不会返回任何东西,因为它是一个void
So you could either change your setter to:
因此,您可以将二传手更改为:
public Employee setFirstName(String fname) {
this.firstName = fname;
return this;
}
OR Create a second constructor for Employee
或创建第二个构造函数 Employee
public Employee(String fname){this.firstName = fname;}
Which would set firstname
on init.
这将firstname
在 init 上设置。
回答by mre
Although this is a bit overkill, you could try using the builder pattern
虽然这有点矫枉过正,但您可以尝试使用构建器模式
public class Employee{
private String firstName;
public static class Builder{
private String firstName;
public Builder firstName(String firstName){
this.firstName = firstName;
return this;
}
public Employee build(){
return new Employee(this);
}
}
private Employee(Builder builder){
firstName = builder.firstName;
}
}
Then you can do the following
然后你可以执行以下操作
Employee e = new Employee.Builder().firstName("John").build();
回答by Jean-Fran?ois Beauchef
In order for your code to work, you would have to return the Employee
(meaning "this") in the setter method setFirstName
.
为了让您的代码正常工作,您必须Employee
在 setter 方法中返回(意思是“this”)setFirstName
。
If you don't own the Employee class (I know this is just a simple example - but for the sake of argument) and cannot modify it, one way to solve that is using functional programming. You could declare yourself a function like this:
如果您不拥有 Employee 类(我知道这只是一个简单的例子 - 但为了论证)并且不能修改它,解决这个问题的一种方法是使用函数式编程。您可以像这样声明自己的函数:
static final Function<String, Employee> EMPLOYEE = firstName -> {
Employee employee = new Employee();
employee.setFirstName(firstName);
return employee;
};
And then you can create your employee in one line like this:
然后你可以像这样在一行中创建你的员工:
Employee jake = EMPLOYEE.apply("Jake");
Maybe not exactly what you want, but still useful.
也许不完全是你想要的,但仍然有用。