Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 类设计问题[可选实现]_Java_Oop_Inheritance_Class Design_Subclass - Fatal编程技术网

Java 类设计问题[可选实现]

Java 类设计问题[可选实现],java,oop,inheritance,class-design,subclass,Java,Oop,Inheritance,Class Design,Subclass,我想知道如何设计一个系统,在这个系统中我有一个Super类和几个Super的子类(比如Sub1、Sub2、Sub3),我想要一个很酷的类。现在我想要两样东西: Sub1和Sub2可能很酷,Sub3永远不会很酷。 我必须能够有一个列表,其中可以有Sub1和Sub2的,如果他们很酷。例如,如果我制作了一个Sub1的对象,它很酷,我可以把它放在列表中,如果它不是,它就不能在列表中 有什么建议吗?提示?在我看来,您需要有一个覆盖列表(比如MyList,它覆盖add() 在add()中,检查要添加的对象是

我想知道如何设计一个系统,在这个系统中我有一个Super类和几个Super的子类(比如Sub1、Sub2、Sub3),我想要一个很酷的类。现在我想要两样东西:
  • Sub1和Sub2可能很酷,Sub3永远不会很酷。
  • 我必须能够有一个列表,其中可以有Sub1和Sub2的,如果他们很酷。例如,如果我制作了一个Sub1的对象,它很酷,我可以把它放在列表中,如果它不是,它就不能在列表中


    有什么建议吗?提示?

    在我看来,您需要有一个覆盖列表(比如MyList,它覆盖add()

    在add()中,检查要添加的对象是否很酷,如果是,则将其添加到列表的一部分。如果不是,那么就优雅地忽略它


    这有帮助吗?

    在我看来,您需要有一个覆盖列表(比如MyList,它覆盖add()

    在add()中,检查要添加的对象是否很酷,如果是,则将其添加到列表的一部分。如果不是,那么就优雅地忽略它


    这有帮助吗?

    您可以创建一个标记界面,比如cool。 让类Sub1和Sub2实现这个接口 在添加到列表之前,请检查cool的示例


    这可能会有所帮助。

    您可以创建一个标记界面,说“酷”。 让类Sub1和Sub2实现这个接口 在添加到列表之前,请检查cool的示例


    也许这会有帮助。

    也许是这样的:

    class Super {}
    
    interface Cool { boolean isCool(); }
    
    class IsCool implements Cool {
        public boolean isCool() { return true; }
    }
    
    class NotCool impolements Cool {
        public boolean isCool() { return false; }
    }
    
    interface CoolSupporter {
        boolean isCool();
        Cool getCool();
    }
    
    class Sub1 extends Super implements CoolSupporter {
        private Cool cool;
        public Sub1() { this(new NotCool()); }
        public Sub1(Cool cool) { this.cool = cool; }
        public boolean isCool() { this.cool.isCool(); }
        public Cool getCool() { return this.cool; }
    }
    
    class Sub2 extends Super implements CoolSupporter {
        private Cool cool;
        public Sub1() { this(new NotCool()); }
        public Sub1(Cool cool) { this.cool = cool; }
        public boolean isCool() { this.cool.isCool(); }
        public Cool getCool() { return this.cool; }
    }
    
    class Sub3 extends Super {}
    
    class CoolList {
        private List<CoolSupporter> list = new ArrayList<CoolSupporter>();
        public void add(CoolSupporter coolSupporter) {
            if (coolSupporter.isCool()) {
                list.add(coolSupporter);
            } else {
                throw new UncoolException();
            }
        }
    }
    
    class Super{}
    接口冷却{boolean isCool();}
    类IsCool实现了Cool{
    公共布尔值isCool(){return true;}
    }
    类不冷蓄水冷{
    公共布尔值isCool(){return false;}
    }
    接口冷却支架{
    布尔isCool();
    Cool getCool();
    }
    Sub1类扩展了超级工具CoolSupporter{
    私人冷淡;
    public Sub1(){this(new NotCool());}
    公共Sub1(Cool Cool){this.Cool=Cool;}
    公共布尔值isCool(){this.cool.isCool();}
    public Cool getCool(){返回this.Cool;}
    }
    Sub2类扩展了超级工具CoolSupporter{
    私人冷淡;
    public Sub1(){this(new NotCool());}
    公共Sub1(Cool Cool){this.Cool=Cool;}
    公共布尔值isCool(){this.cool.isCool();}
    public Cool getCool(){返回this.Cool;}
    }
    类Sub3扩展了Super{}
    类冷却列表{
    私有列表=新的ArrayList();
    公共无效添加(CoolSupporter CoolSupporter){
    if(coolSupporter.isCool()){
    列表。添加(coolSupporter);
    }否则{
    抛出新的uncoleException();
    }
    }
    }
    
    可能是这样的:

    class Super {}
    
    interface Cool { boolean isCool(); }
    
    class IsCool implements Cool {
        public boolean isCool() { return true; }
    }
    
    class NotCool impolements Cool {
        public boolean isCool() { return false; }
    }
    
    interface CoolSupporter {
        boolean isCool();
        Cool getCool();
    }
    
    class Sub1 extends Super implements CoolSupporter {
        private Cool cool;
        public Sub1() { this(new NotCool()); }
        public Sub1(Cool cool) { this.cool = cool; }
        public boolean isCool() { this.cool.isCool(); }
        public Cool getCool() { return this.cool; }
    }
    
    class Sub2 extends Super implements CoolSupporter {
        private Cool cool;
        public Sub1() { this(new NotCool()); }
        public Sub1(Cool cool) { this.cool = cool; }
        public boolean isCool() { this.cool.isCool(); }
        public Cool getCool() { return this.cool; }
    }
    
    class Sub3 extends Super {}
    
    class CoolList {
        private List<CoolSupporter> list = new ArrayList<CoolSupporter>();
        public void add(CoolSupporter coolSupporter) {
            if (coolSupporter.isCool()) {
                list.add(coolSupporter);
            } else {
                throw new UncoolException();
            }
        }
    }
    
    class Super{}
    接口冷却{boolean isCool();}
    类IsCool实现了Cool{
    公共布尔值isCool(){return true;}
    }
    类不冷蓄水冷{
    公共布尔值isCool(){return false;}
    }
    接口冷却支架{
    布尔isCool();
    Cool getCool();
    }
    Sub1类扩展了超级工具CoolSupporter{
    私人冷淡;
    public Sub1(){this(new NotCool());}
    公共Sub1(Cool Cool){this.Cool=Cool;}
    公共布尔值isCool(){this.cool.isCool();}
    public Cool getCool(){返回this.Cool;}
    }
    Sub2类扩展了超级工具CoolSupporter{
    私人冷淡;
    public Sub1(){this(new NotCool());}
    公共Sub1(Cool Cool){this.Cool=Cool;}
    公共布尔值isCool(){this.cool.isCool();}
    public Cool getCool(){返回this.Cool;}
    }
    类Sub3扩展了Super{}
    类冷却列表{
    私有列表=新的ArrayList();
    公共无效添加(CoolSupporter CoolSupporter){
    if(coolSupporter.isCool()){
    列表。添加(coolSupporter);
    }否则{
    抛出新的uncoleException();
    }
    }
    }
    
    管理此问题的最简单方法是进一步细分Sub1(CoolSub1和NotCoolSub1)和Sub2(CoolSub2和NotCoolSub2)

    然后,CoolSub1和CoolSub2可以实现Cool(Cool应该是一个接口,而不是一个类)

    然后您可以定义

    List<Cool>
    
    列表
    

    它将接受Sub1和Sub2的实现,但仅当它们实现了Cool时才接受。

    管理这一点的最简单方法是进一步细分Sub1(CoolSub1和NotCoolSub1)和Sub2(CoolSub2和NotCoolSub2)

    然后,CoolSub1和CoolSub2可以实现Cool(Cool应该是一个接口,而不是一个类)

    然后您可以定义

    List<Cool>
    
    列表
    

    这将接受Sub1和Sub2的实现,但前提是它们实现得很酷。

    Arne的回答有点像你想要的,但我觉得它太复杂了。也许我错过了什么?为什么不只是:

    class Super { }
    
    interface Cool { boolean isCool(); }
    
    class CoolImpl extends Super implements Cool {
        private boolean cool;
        public CoolImpl(boolean cool) { this.cool = cool; }
        public boolean isCool() { return this.cool; }
    }
    
    class Sub1 extends CoolImpl { }
    class Sub2 extends CoolImpl { }    
    class Sub3 extends Super { }
    
    class CoolList extends ArrayList<Cool> {
        public boolean add(Cool cool) {
            if (!cool.isCool()) {
                return false;
            }
            return super.add(cool);
        }
    }
    
    class Super{}
    接口冷却{boolean isCool();}
    类CoolImpl扩展了超级实现Cool{
    私人布尔酷;
    public CoolImpl(boolean cool){this.cool=cool;}
    公共布尔值isCool(){返回this.cool;}
    }
    Sub1类扩展了CoolImpl{}
    Sub2类扩展了CoolImpl{}
    类Sub3扩展了Super{}
    类CoolList扩展了ArrayList{
    公共布尔添加(酷){
    如果(!cool.isCool()){
    返回false;
    }
    返回super.add(cool);
    }
    }
    
    阿恩的答案有点像你想要的,但我觉得太复杂了。也许我错过了什么?为什么不只是:

    class Super { }
    
    interface Cool { boolean isCool(); }
    
    class CoolImpl extends Super implements Cool {
        private boolean cool;
        public CoolImpl(boolean cool) { this.cool = cool; }
        public boolean isCool() { return this.cool; }
    }
    
    class Sub1 extends CoolImpl { }
    class Sub2 extends CoolImpl { }    
    class Sub3 extends Super { }
    
    class CoolList extends ArrayList<Cool> {
        public boolean add(Cool cool) {
            if (!cool.isCool()) {
                return false;
            }
            return super.add(cool);
        }
    }
    
    class Super{}
    接口冷却{boolean isCool();}
    类CoolImpl扩展了超级实现Cool{
    私人布尔酷;
    public CoolImpl(boolean cool){this.cool=cool;}
    公共布尔值isCool(){返回this.cool;}
    }
    Sub1类扩展了CoolImpl{}
    Sub2类扩展了CoolImpl{}
    类Sub3扩展了Super{}
    类CoolList扩展了ArrayList{
    公共布尔添加(酷){
    如果(!cool.isCool()){
    返回false;
    }
    返回super.add(cool);
    }
    }
    
    在Java中,类不能选择性地属于某个类型。尽管您可能会将子类Sub1作为子类,但其中一个子类实现了接口Cool,而另一个子类则没有:

    class Super { }
    
    interface Cool { }
    
    class Sub1 extends Super { }
    class Sub1Cool extends Sub1 implements Cool { }
    
    class Sub2 extends Super { }
    class Sub2Cool extends Sub2 implements Cool { }
    
    class Sub3 extends Super { }
    
    class CoolList extends ArrayList<Super> {
        public boolean add(Super sup) {
            if (!(sup instanceof Cool)) {
                return false;
            }
            return super.add(cool);
        }
    }
    
    class Super{}
    接口