package com.ht.staticKeyword;
public class PreateConstruTest{
public static void main(String[] args) {
// TODO Auto-generated method stub
//这样写编译会报错,因为构造函数被私有化;
// PreateConstru a=new PreateConstru("cccc");
//PreateConstru b=new PreateConstru("dddd");
PreateConstru a=PreateConstru.getobject();
System.out.println(a.a);
}
}
class PreateConstru {
//调用私有的构造方法赋值给newPreateConstu
String a="单例模式";
private static PreateConstru newPreateConstu=new PreateConstru("初始化单例");
private PreateConstru(String b){
a=b;
}
//外面的类可以通过调用这个方法来得到这个实例化对象,但无论外部
//类中有多少个调用这个getobject来获取对象,本质还是一个对象
public static PreateConstru getobject() {
return newPreateConstu;
}
}