我找到了一篇有趣的代码文章:
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);
}
}
我真的很好奇了解创建此类的优势。我知道这里的此类对象是不可变的,因为一旦初始化就无法更改其变量值。我以前从未做过这样的事情,而且我真的不了解它的优势。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句