Golang Option模式
class Student {
private long id;
private String name;
private String addr = "unknown";
private int age = 18;
// 一般的Student对象
public Student(long id, String name) {
this.id = id;
this.name = name;
}
// 指定addr的Student对象
public Student(long id, String name, String addr) {
this.id = id;
this.name = name;
this.addr = addr;
}
// 指定addr和age的Student对象
public Student(long id, String name, String addr, int age) {
this.id = id;
this.name = name;
this.addr = addr;
this.age = age;
}
public static void main (String[] args) {
Student st1 = new Student((long)1, "zhang san");
System.out.println(st1.id + "," + st1.name);
Student st2 = new Student((long)2, "li si", "中国");
System.out.println(st2.id