如何创建一个具有多个使用相同参数类型的构造函数的类 [英] How to create a Class with multiple constructors that use the same parameter type

查看:153
本文介绍了如何创建一个具有多个使用相同参数类型的构造函数的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这样做:


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?

这篇关于如何创建一个具有多个使用相同参数类型的构造函数的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆