简体   繁体   English

Java通用继承/通用可比

[英]Java Generic Inheritance / Generic Comparable

Say that I have a function that does things with a list of shapes. 假设我有一个函数可以处理一系列形状。 I can define a few useful classes: 我可以定义一些有用的类:

public abstract class Shape implements Comparable<Shape> { }

public abstract class FourSidedShapes extends Shape { }

public class Rectangle extends FourSidedShapes{
   ...
   @Override
   public int compareTo(Shape o) {
    return 0;
   }
}

public class Square extends FourSidedShapes {... }

I have a helper container: 我有一个辅助容器:

//Implements doubly-linked sorted list
public class SortedLinkedListNode<T extends Shape> {
   public SortedLinkedListNode<T> add(T e) { }
   public <E extends Shape> T get(E target) { }
   ...
}

I would like to do something like this: TreeSet<FourSidedShapes> myshapes; 我想做这样的事情: TreeSet<FourSidedShapes> myshapes;

If I do that, I have two problems. 如果这样做,我将遇到两个问题。 First, I want to keep the SortedLinkedList helper collection as generic as possible. 首先,我想使SortedLinkedList帮助程序集合尽可能通用。 So I would like to write the implementation assuming that it will hold Shape or some subclass of shape (including objects that have not been considered / implemented such as circles / triangles, etc. But if I do that, then I have to implement public int compareTo(Shape o) for both rectangle and square even though I really only want to implement public int compareTo(FourSidedShapes o) because I'm really only storing subclasses of FourSidedShapes in my collection. So my question is, how do I modify my SortedLinkedListNode so that it extends a compareTo that is no lower than the base class that it is storing? For example, if I am storing shapes, then it expects shapes to implement Comparable<Shapes> . If I'm storing FourSidedShapes, then it expects FourSidedShapes to implement Comparable<FourSidedShapes> . I suspect that the right answer involves making the parameter of Comparable generic, but I'm not sure what that look like. For example, something like 因此,我想写一个实现,假设它可以容纳Shape或shape的某些子类(包括尚未考虑/实现的对象,例如圆形/三角形等),但是如果我这样做,则必须实现public int即使我真的只想实现public int compareTo(FourSidedShapes o),也可以同时对矩形和正方形进行compareTo(Shape o),因为我实际上只在集合中存储FourSidedShapes的子类。所以我的问题是,如何修改SortedLinkedListNode因此,它扩展了不低于其存储基类的compareTo?例如,如果我存储形状,则它期望形状实现Comparable<Shapes> 。如果我存储FourSidedShapes,则它期望FourSidedShapes实现Comparable<FourSidedShapes> ,我怀疑正确的答案涉及使Comparable泛型的参数,但我不确定那是什么样子。

public class ShapeComparable<T extends Comparable<T>> {
   public abstract int compareTo(T other);
}

I am not completely clear on your question but I am assuming that you are asking how to make Rectangle or FourSidedShapes implement Comparable<FourSidedShapes> . 我对您的问题尚不完全清楚,但我假设您正在询问如何使RectangleFourSidedShapes实现Comparable<FourSidedShapes> Here is the code showing how to do that: 这是显示如何执行此操作的代码:

public abstract class Shape< T extends Shape<T> > implements Comparable<T> { }

public abstract class FourSidedShapes extends Shape<FourSidedShapes> {

    @Override
    public int compareTo(FourSidedShapes o) {
        return 0;
    }        
}

public class Rectangle extends FourSidedShapes{

    @Override
    public int compareTo(FourSidedShapes o) {
        return 0;
    }

}

public class Square extends FourSidedShapes{

    @Override
    public int compareTo(FourSidedShapes o) {
        return 0;
    }

}

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

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