無聊程式碼一段

longerandlonger發表於2013-01-09
class Person implements Serializable {
	private static final long serialVersionUID = -9016986248073447608L;
	
	public static enum Gender {
		MALE, FEMALE
	}

	private static class InstanceHolder {
		private static class InstanceHolder2 {
			private static final Person instatnce = new Person("John", 31, Gender.MALE);
		}
	}

	public static Person getInstance() {
		return InstanceHolder.InstanceHolder2.instatnce; // 這裡很奇怪的有木有
	}

	String name = null;
	Integer age = null;
	Gender gender = null;

	private Person() {
		System.out.println("none-arg constructor");
	}

	private Person(String name, Integer age, Gender gender) {
		System.out.println("arg constructor");
		this.name = name;
		this.age = age;
		this.gender = gender;
	}
}

相關文章