假设我有一个课程如下:
private class ComparableElement extends Element implements Comparable<ComparableElement> {
@Override
public int compareTo(ComparableElement o) {
return getName().compareTo(o.getName());
}
}
其中Elements的定义如下:
public class Element {
protected String name;
protected String category;
public Element() {
super();
}
public Element(String _name, String _category) {
super();
this.name = _name;
this.category = _category;
}
public String getName() {
return name;
}
}
然后,如果我有一个Element对象,最好的做法是将该Element对象转换为ComparableElement实例吗?类似的东西:
Element e;
...
ComparableElement ce = (ComparableElement)e;
只是这不起作用......
答案 0 :(得分:1)
您可以制作新约束器ComparableElement
:
public ComparableElement (Element element )
{
//Here you get all the data from the element and set it to this object.
}