黑马程序员,黑马论坛--------枚举和单例设计模式的区别

文章来源:黑马程序员,黑马论坛

 

枚举和单例设计模式的区别,黑马面试有的

public class Test11 {

        public static void main(String[] args) {
               
                //调用:字母对象 -- 1
                System.out.println(Letter.A);
                //输出:com.itheima.Letter@3020ad
               
                //调用:字母枚举 -- 2
                System.out.println(Letter2.A);
                //输出:A
               
                //调用:字母单例模式 -- 3
                System.out.println(Letter3.getLetter3());
                //输出:com.itheima.Letter3@60e128
               
                /*
                 * 三者都可以调用,而[字母对象]和[字母枚举]是类似的
                 * 只不过[字母枚举 ]是类似简化版
                 * 如果字[字母枚举 ]里就一个A那么和[字母单例模式 ]就和类似了
                 *
                 * 具体了解可以参考以下几篇信息
                 * 1.http://cardyn.iteye.com/blog/904534
                 * 2.http://developer.51cto.com/art/201107/275031.htm
                 * 3.http://www.iteye.com/topic/1116193
                 */
        }
       
}
/**
* 字母对象 -- 1
*/
final class Letter{
        //A 字母常量
        public static final Letter A = new Letter();
        //B 字母常量
        public static final Letter B = new Letter();
        //C 字母常量
        public static final Letter C = new Letter();
        /**
         * 私有构造函数不可以外部实例化Letter
         */
        private Letter(){}
}
/**
* 字母枚举 -- 2
*/
enum Letter2{
        A,B,C
}
/**
* 字母单例模式 -- 3
*/
class Letter3{
        //1.初始化一个a字母对象
        private static Letter3 a = new Letter3();
        /**
         * 私有构造函数不可以外部实例化Letter
         */
        private Letter3(){}
        /**
         * 调用获得字母对象,实现单例模式
         * @return  字母对象
         */
        public static Letter3 getLetter3(){
                if (a == null){
                        a = new Letter3();
                }
                return a;
        }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值