package com.tanm.singleton;
//单例模式
class Single {
private Single(){
}
//设置private为类部私有,外部不能访问
//single变量设为static,是静态变量,困为static方法要调用single
//single变量设为final,表示single是常量不可修改
private static final Single single = new Single();
//static方法getSingleInstance(),不用实例化对象就可以调用Single.getSingleInstance()
public static Single getSingleInstance(){
return single;
}
public void Say() {
System.out.println("程序调用开始......");
}
}
//测试类
public class SingletonDemo {
public static void main(String[] args) {
Single s = Single.getSingleInstance();
s.Say();
}
}
posted on 2007-10-16 17:07
谭明 阅读(418)
评论(0) 编辑 收藏 所属分类:
Java设计模式