如何调用被私有化的构造方法
(2012-04-13 22:18:21)
标签:
it |
分类: IT |
一个构造方法如果被私有化了,那么只能从该私有化类的内部进行实例化进行调用
class Con1{
}
public class ConstructDemo {
私有化构造方法是一种单态设计方法,可以实现避免多次实例化;如:
public class ConstructDemo {
public static void main(String args[]){
Con1 con1= con1 = Con1.getIns();
Con1 con2= con1 = Con1.getIns();
Con1 con3= con1 = Con1.getIns();
Con1 con4= con1 = Con1.getIns();
Con1 con5= con1 = Con1.getIns();
con1.print();
con2.print();
con3.print();
con4.print();
con5.print();
}
该功能的实现实际上只实例化了一次而已:private static Con1 ins = new Con1();