public class Employee {
private String firstName;
private String lastName;
//private default constructor
private Employee(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public static Employee valueOf (String firstName, String lastName) {
return new Employee(firstName, lastName);
}
}
Run Code Online (Sandbox Code Playgroud)
我非常好奇理解创建这种类的优势.我知道这个类的对象是不可变的,因为初始化后无法更改其变量值.我之前从未做过这样的事情,我真的不明白它的优点.
java ×1