美文网首页
Item 1: Consider Static Factory

Item 1: Consider Static Factory

作者: Aunki | 来源:发表于2018-10-08 06:12 被阅读0次

    1 Example

    public static Boolean valueOf(boolean b) {
        return b ? Boolean.TRUE : Boolean.FALSE;
    }
    

    2 ?

        Fragment fragment = new MyFragment();
        // or
        Date date = new Date();
    
        Fragment fragment = MyFragment.newIntance();
        // or 
        Calendar calendar = Calendar.getInstance();
        // or 
        Integer number = Integer.valueOf("3");
    

    3 Advantage

    3.1 比构造器多名字

    valueOfnewInstancegetInstance

    3.2 不用被调用时都创建新对象

    3.3 返回原返回类型的子类

    相关文章

      网友评论

          本文标题:Item 1: Consider Static Factory

          本文链接:https://www.haomeiwen.com/subject/emqdaftx.html