Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.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_Generics_Bounding - Fatal编程技术网

在Java中绑定多个类

在Java中绑定多个类,java,generics,bounding,Java,Generics,Bounding,我正在完成一项任务,要求我创建两个通用集合(称为ComputerOrder和PartyTrayOrder),分为几个类:ComputerPart、外围设备和服务,以及奶酪、水果和服务。所有这些类(计算机零件、水果、服务等)都扩展了一个称为产品的类 我遇到的问题是,在Java中似乎不可能绑定多个类,因为您可能只绑定多个接口,并且我不允许修改现有的类结构 我读过很多书,看到有人建议我创建一个接口来“编码”所有相关的类,但我不明白如果没有ComputerPart、Peripheral等作为接口,然后使

我正在完成一项任务,要求我创建两个通用集合(称为ComputerOrder和PartyTrayOrder),分为几个类:ComputerPart、外围设备和服务,以及奶酪、水果和服务。所有这些类(计算机零件、水果、服务等)都扩展了一个称为产品的类

我遇到的问题是,在Java中似乎不可能绑定多个类,因为您可能只绑定多个接口,并且我不允许修改现有的类结构

我读过很多书,看到有人建议我创建一个接口来“编码”所有相关的类,但我不明白如果没有ComputerPart、Peripheral等作为接口,然后使用extend关键字,这是怎么可能的。我看到有人进一步建议我创建名为ComputerProduct和PartyTrayProduct的抽象类,但我看不出这会以任何方式限制类型

我不允许发布图片,但这里有一个

这是我当前的ComputerOrder代码,尽管它只限制父类产品,这意味着它实际上并不像赋值指定的那样

public class ComputerOrder<T extends Product> extends GenericOrder<T> {
    public ComputerOrder() {
        super();
    }
}
公共类ComputerOrder扩展了GenericOrder{
公共秩序{
超级();
}
}
以下是作业规范:

  • 设计并实现一个名为GenericOrder的通用容器,该容器充当Products.java中任意数量对象的集合。设计一种机制,为容器的每个实例提供一个唯一标识符。您必须使用Java泛型特性

  • 设计并实现名为ComputerOrder的GenericOrder子类,该子类接受任意数量的不同类别的ComputerPart对象、外围对象和服务对象。根据需要实施尽可能多的方法

  • 设计并实现一个名为PartyTrayOrder的GenericOrder子类,它接受任意数量的不同类别的Cheese对象、水果对象和服务对象。根据需要实施尽可能多的方法。 (……)


  • 该图精确地显示了类层次结构的相似性。每个胖箭头都是一个类扩展。当箭头从A指向B时,意味着A扩展了B。您只需将其关闭到java代码中即可


    当A扩展B和B扩展C时,A就是C。我认为这里不需要多基继承。

    这是我提出的唯一解决方案,它遵循字母赋值,仍然有一定的意义(尽管我认为这不是一个“好”的解决方案-请参阅下面的注释):ComputerOrder和PartyTrayOrder可以提供只接受特殊类型的
    产品的方法:

    abstract class Product {}
    class ComputerPart extends Product {}
    class Peripheral extends Product { }
    class Cheese extends Product {} 
    class Fruit extends Product {}
    class Service extends Product {} 
    
    abstract class GenericOrder<T extends Product> {
        protected final void addImpl(T t) {
        }
    }
    
    class ComputerOrder extends GenericOrder<Product> {
        void add(ComputerPart t) {
            addImpl(t);
        }
        void add(Peripheral t) {
            addImpl(t);
        }
        void add(Service t) {
            addImpl(t);
        }
    }
    
    class PartyTrayOrder  extends GenericOrder<Product> {
        void add(Cheese t) {
            addImpl(t);
        }
        void add(Fruit t) {
            addImpl(t);
        }
        void add(Service t) {
            addImpl(t);
        }
    }
    
    我假设这是预期的解决方案,因为分配包含了广泛的提示:

    根据需要实施尽可能多的方法

    因此,最有可能的目标是而不是实现一个神奇地为多个类绑定的方法。相反,必须为每个“类别”添加一种方法


    旁白:我的直觉是这种设计可以改进。想象一下,您必须为新的订单类型创建类
    CarOrder
    FahsionOrder
    等。或者想象一下,
    PartyTrayOrder
    必须进行扩展,以处理像
    Meat
    Dip
    salar
    这样的类:你最终会得到几十个类和几十个专门方法

    这一切都可以通过引入一个专门的“产品类型”来避免,该“产品类型”与特定“订单类型”可接受的类型完全匹配。所以我认为(
    Product
    应该是一个接口,应该有
    ComputerProduct
    PartyTrayProduct
    这样的接口,如

    interface Product {}
    
    interface ComputerProduct extends Product {}
    class ComputerPart implements ComputerProduct  {}
    class Peripheral implements ComputerProduct  {}
    
    interface PartyTrayProduct extends Product {}
    class Cheese implements PartyTrayProduct{} 
    class Fruit implements PartyTrayProduct{}
    
    class Service implements Product {} 
    class DeliveryService implements PartyTrayProduct{} 
    class AssemblyService implements ComputerProduct  {}
    
    这样,特定的
    ComputerOrder
    PartyTrayOrder
    所需的边界已经用类层次结构建模。这样做的好处是:您不再需要
    ComputerOrder
    PartyTrayOrder
    类了
    然后,
    GenericOrder
    可以是非抽象的,为了创建特定类型的订单,您只需正确使用generic类型绑定

    这里是一个完整的示例,我刚刚将
    sala
    作为一种新的
    PartyTrayProduct
    ,以及
    CarPart
    作为一种新的产品类型,而不必扩展或修改任何“基础结构类”:

    接口产品{}
    接口计算机产品扩展产品{}
    类ComputerPart实现ComputerProduct{}
    类外围设备实现ComputerProduct{}
    接口PartyTrayProduct扩展了产品{}
    类实现PartyTrayProduct{}
    类水果实现PartyTrayProduct{}
    类服务实现产品{}
    类DeliveryService实现PartyTrayProduct{}
    类AssemblyService实现ComputerProduct{}
    类Salad实现PartyTrayProduct{}//一个新的PartyTrayProduct
    //全新的产品类型:
    接口CarProduct扩展了产品{}
    类CarPart实现CarProduct{}
    类CarInterior实现CarProduct{}
    类GenericOrder{
    公共void add(T){}
    }
    公共类产品示例2{
    公共静态void main(字符串[]args){
    GenericOrder c=新的GenericOrder();
    c、 添加(新的ComputerPart());
    c、 添加(新外围设备());
    //c、 添加(新奶酪());//不允许
    //c、 添加(新水果());//不允许
    c、 添加(新的AssemblyService());
    GenericOrder p=新的GenericOrder();
    //p、 添加(新的ComputerPart());//不允许
    //p、 添加(新外围设备());//不允许
    p、 添加(新奶酪());
    p、 添加(新水果());
    p、 添加(新沙拉());//只需添加它。。。
    p、 添加(新的DeliveryService());
    //易于扩展:
    一般顺序
    
    interface Product {}
    
    interface ComputerProduct extends Product {}
    class ComputerPart implements ComputerProduct  {}
    class Peripheral implements ComputerProduct  {}
    
    interface PartyTrayProduct extends Product {}
    class Cheese implements PartyTrayProduct{} 
    class Fruit implements PartyTrayProduct{}
    
    class Service implements Product {} 
    class DeliveryService implements PartyTrayProduct{} 
    class AssemblyService implements ComputerProduct  {}
    
    interface Product {}
    
    interface ComputerProduct extends Product {}
    class ComputerPart implements ComputerProduct  {}
    class Peripheral implements ComputerProduct  {}
    
    interface PartyTrayProduct extends Product {}
    class Cheese implements PartyTrayProduct{} 
    class Fruit implements PartyTrayProduct{}
    
    class Service implements Product {} 
    class DeliveryService implements PartyTrayProduct{} 
    class AssemblyService implements ComputerProduct  {}
    
    class Salad implements PartyTrayProduct{} // A new PartyTrayProduct
    
    // Entirely new product type:
    interface CarProduct extends Product {}
    class CarPart implements CarProduct  {}
    class CarInterior implements CarProduct  {}
    
    class GenericOrder<T extends Product> {
        public void add(T t) { }
    }
    
    public class ProductExample2 {
        public static void main(String[] args) {
    
            GenericOrder<ComputerProduct> c = new GenericOrder<ComputerProduct>();
            c.add(new ComputerPart());
            c.add(new Peripheral());
            //c.add(new Cheese()); // Not allowed
            //c.add(new Fruit()); // Not allowed
            c.add(new AssemblyService());
    
            GenericOrder<PartyTrayProduct> p = new GenericOrder<PartyTrayProduct>();
            //p.add(new ComputerPart());  // Not allowed
            //p.add(new Peripheral());  // Not allowed
            p.add(new Cheese());
            p.add(new Fruit());
            p.add(new Salad()); // Just add it...
            p.add(new DeliveryService());
    
            // Easy to extend:
            GenericOrder<CarProduct> e = new GenericOrder<CarProduct>();
            e.add(new CarPart());
            e.add(new CarInterior());
        }
    }