首页 > 其他 > 详细

多例模式

时间:2016-01-14 14:00:42      阅读:126      评论:0      收藏:0      [点我收藏+]

例如:表示一周时间的类要用多例模式

class Sex{
    private String title ;
    private static final Sex MALE = new Sex("man") ;
    private static final Sex FEMALE = new Sex("woman") ;
    private Sex (String title ){
        this.title = title ;
    }
    public String toString(){
       return this.title;
    }
    public static Sex getInstance(String ch){
        switch(ch){
            case "man":
                return MALE ;
            case "woman":
                return FEMALE;
            default :
                return null ;
        }
    }
   
}
public class TestSex{
    public static void main(String arg[]){
        Sex sex = Sex.getInstance("man");
        System.out.println(sex) ;
    }
}

 

多例模式

原文:http://www.cnblogs.com/da-peng/p/5129834.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!