怎么获取PropertyUtils的实例,想让他走这个私有构造器,外面怎么调用
public class PropertyUtils extends PropertyUtilsBase {
private PropertyUtils(){
this.init();
}
private static final PropertyUtilsBase propertyUtils = new PropertyUtils();
@Override
protected String getPropertiesFilePath() {
return "common.hadoop.properties";
}
}
public class PropertyUtils extends PropertyUtilsBase{
private PropertyUtils(){
this.init();
}
private static final PropertyUtilsBase propertyUtils = new PropertyUtils();
@Override
protected String getPropertiesFilePath() {
return "common.hadoop.properties";
}
/**
* 获取propertyUtils
* @return
*/
public static PropertyUtilsBase getInstance(){
return propertyUtils;
}
}
外部获取,直接调用getInstance()方法
单例模式了解下:
public class PropertyUtils extends PropertyUtilsBase{
private static final PropertyUtils propertyUtils = new PropertyUtils(); //恶汉模式
private PropertyUtils(){}
public static PropertyUtils getPropertyUtils(){
return propertyUtils;
}
}
public class PropertyUtils extends PropertyUtilsBase{
private static PropertyUtils propertyUtils = null; //懒汉模式
private PropertyUtils(){}
public static PropertyUtils getPropertyUtils(){
if (propertyUtils==null){
propertyUtils = new PropertyUtils();
return propertyUtils;
}else {
return propertyUtils;
}
}
}
public static void main(String[] args) {
PropertyUtils propertyUtils = PropertyUtils.getPropertyUtils();
}
class A extends B{
private A(){}
private static final A a = new A();
public static A getA(){
return a
}
}
获取: A.getA();
java 你都设置私有了 还调用这个构造干嘛 这就是java的规则
如果你想给其他类用这个构造函数
你就公开 或者像你封装字段一样封装这个构造函数