简体   繁体   English

Java:填充了接口元素的数据结构 - 如何强制实现接口的类实现compareTo()?

[英]Java: a datastructure filled with interface elements - how to force classes that implement the interface to implement compareTo()?

I got a built in interface called Bicycle (and I am not allowed to add or change anything in it), 我有一个名为Bicycle的内置界面(我不允许在其中添加或更改任何内容),

and I created a data structure filled with Bicycle elements in it. 我创建了一个充满自行车元素的数据结构。

I need that the Bicycle elements will be able to be sorted in different ways, the decision how exactly they will be sorted in the data structure has to be upon the class that implements the interface Bicycle. 我需要自行车元素能够以不同的方式进行排序,决定它们在数据结构中的排序方式必须在实现接口Bicycle的类上。 (or any other way that doesn't have to make any change in Bicycle interface) (或任何其他不必在Bicycle界面进行任何更改的方式)

I read on the internet that I need to write something like this: 我在网上看到我需要写这样的东西:

Public class BicycleDataStructure <Bicycle extends Comparable<Bicycle>>

and then I can use compareTo() function on Bicycle elements in the data structure. 然后我可以在数据结构中的Bicycle元素上使用compareTo()函数。 But, When I create a class like: 但是,当我创建一个类,如:

SportBicycle implements Bicycle

Java doesn't tell me that I need to add implementation of compareTo() function, so how it will know by which parameter to compare it? Java没有告诉我我需要添加compareTo()函数的实现,那么它将如何知道通过哪个参数进行比较呢? I think I do something wrong.. Thank you for your help. 我想我做错了什么..谢谢你的帮助。

Edit: code - 编辑:代码 -

public class BicycleDataStructure<T extends Comparable<Bicycle>> {
protected List<Bicycle> dataStructure;
protected Bicycle max;

public BicycleDataStructure() {
    this.dataStructure= new ArrayList<>();
    this.max = null;

}

public void addBicycle(Bicycle b1) {
    int i;
    for (i = 0; i < dataStructure.size(); i++) {
        Bicycle b = dataStructure.get(i);

        if (b1.compareTo(b) < 0) {
            this.dataStructure.add(i, b1);
            break;
        }

    }

}

and the interface- 和接口 -

public interface Bicycle{

public int getModel();

public String getColor();

public String getBrand();

public int getSize();

} }

Currently, Bicycle is being treated as a type parameter, instead do this: 目前, Bicycle被视为一个类型参数,而不是这样做:

class BicycleDataStructure <T extends Comparable<Bicycle>> {...}

Now T is the type parameter and Bicycle refers to the interface type. 现在T是类型参数, Bicycle指的是接口类型。

Even if you can't modify the Bicycle interface, you can extend it. 即使您无法修改Bicycle接口,也可以对其进行扩展。

public interface ComparableBicycle extends Bicycle, Comparable<ComparableBicycle> {
}

Your SportBicycle class can implement ComparableBicycle and still be an instance of Bicycle. 您的SportBicycle类可以实现ComparableBicycle,仍然是Bicycle的一个实例。 The compiler will require you to implement compareTo() . 编译器将要求您实现compareTo()

This will result in an error: 这将导致错误:

public class SportBicycle implements ComparableBicycle {

    // ... Implementations of Bicycle methods ...
}

Until you add compareTo() : 在添加compareTo()

public class SportBicycle implements ComparableBicycle {
    /* (non-Javadoc)
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    @Override
    public int compareTo(ComparableBicycle o) {
        // TODO: Make the comparison
        return 0;
    } 

    // ... Implementations of Bicycle methods ...
}

First, you should change the generic type of your data structure class as follows: 首先,您应该更改数据结构类的泛型类型,如下所示:

public class BicycleDataStructure<T extends Bicycle & Comparable<T>>

This is called an intersection bound type , and it tells the compiler that the data structure will hold instances of a type that is both a subtype of Bicycle and Comparable<T> . 这称为交集绑定类型 ,它告诉编译器数据结构将保存类型为BicycleComparable<T>的子类型的实例。 This avoids the need to create a subtype of Bicycle just to express that it's also Comparable of that subtype. 这样就无需创建Bicycle的子类型,只是为了表示它也是该子类型的可Comparable

Then, you should make both your dataStructure and max attributes use the generic type T : 然后,您应该使dataStructuremax属性都使用泛型类型T

protected List<T> dataStructure;
protected T max;

Then, in your add method, the b1 argument and b local variable should also be of type T . 然后,在add方法中, b1参数和b局部变量也应该是T类型。 This is the code: 这是代码:

public class BicycleDataStructure<T extends Bycycle & Comparable<T>> {

    protected List<T> dataStructure;

    protected T max;

    public BicycleDataStructure() {
        this.dataStructure = new ArrayList<>();
        this.max = null;    
    }

    public void addBicycle(T b1) {
        for (int i = 0; i < dataStructure.size(); i++) {
            T b = dataStructure.get(i);

            if (b1.compareTo(b) < 0) {
                this.dataStructure.add(i, b1);
                break;
            }   
        }    
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM