如何创建一个具有多个使用相同参数类型的构造函数的类
问题描述:
我想这样做:
public class Arquivo {
private File diretorio = null ;
public Arquivo(File dir){
this.diretorio = dir;
}
public Arquivo(String dir){
this( new File(dir) );
}
public Arquivo(String fileName){
this( new File("./src/Data/"+fileName) );
}
}
答
与构造函数的关系,这是构造函数的限制之一
You can't with constructor, that is one of the limitation of constructors
时间开始使用静态工厂模式
另请参阅
- What are static factory methods in Java?