Best way to handle multiple constructors in Java

后端 未结 9 1449
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 07:00

I\'ve been wondering what the best (i.e. cleanest/safest/most efficient) way of handling multiple constructors in Java is? Especially when in one or more constructors not al

9条回答
  •  一向
    一向 (楼主)
    2020-12-02 07:42

    A slightly simplified answer:

    public class Book
    {
        private final String title;
    
        public Book(String title)
        {
          this.title = title;
        }
    
        public Book()
        {
          this("Default Title");
        }
    
        ...
    }
    

提交回复
热议问题