package test;
 class 多例设计模式测试 {
    //构造方法私有化
	private 多例设计模式测试(String title) {
		this.title=title;
	}
	//实例化对象
	private static final 多例设计模式测试  MALE=new 多例设计模式测试("男");
	private static final 多例设计模式测试 FEMALE=new 多例设计模式测试("女");
	//属性私有化
	private String title;
	//取得外部对象的方法
	public static 多例设计模式测试 getInstance(String msg) {
		switch(msg) {
		case "male":
			return MALE;
		case "female":
			return FEMALE;
		default:
			return null;
		}
	}
	//getter方法
	public String getTitile() {
		return title;
	}
}
public class 多例设计模式{
	public static void main(String[] args) {
		多例设计模式测试 多例=多例设计模式测试.getInstance("male");
		System.out.println(多例.getTitile());
	}
}

  

相关文章:

  • 2022-12-23
  • 2021-09-29
  • 2022-12-23
  • 2021-04-26
  • 2021-11-22
  • 2021-07-22
  • 2021-09-13
猜你喜欢
  • 2021-07-09
  • 2021-09-08
  • 2022-12-23
  • 2021-08-19
  • 2021-07-24
  • 2021-06-11
  • 2022-12-23
相关资源
相似解决方案