[英]Implements Comparable Missing One trait
So I'm working on a very basic code which implements Comparable
comparing a painting based on year, artist and title. 因此,我正在研究一个非常基本的代码,该代码实现了Comparable
根据年份,艺术家和标题比较一幅画。
However my code isn't comparing the paintings by title, just year and artist. 但是我的代码没有按名称,年份和艺术家比较绘画。
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Painting p1 = new Painting(Year.NINETYEIGHT, artist.ART1, title.TIT1);
Painting p2 = new Painting(Year.NINETYEIGHT, artist.ART1, title.TIT2);
System.out.println("p1: " + p1.toString());
System.out.println("p2: " + p2.toString());
if(p1.compareTo(p2)> 0){
System.out.println(p1.toString() + " beats " + p2.toString());
} else if(p1.compareTo(p2) < 0){
System.out.println(p2.toString() + " beats " + p1.toString());
} else{
System.out.println("Same Everything");
}
}
}
public enum Year {
NINETYSEVEN, NINETYEIGHT, NINETYNINE, TWOTHOUSAND
}
public enum artist {
ART1, ART2, ART3,
}
public enum title {
TIT1, TIT2,TIT3,
}
public class Painting implements Comparable {
private title title;
private Year year;
private artist artist;
public Painting(Year y, artist a, title t) {
title = t;
year = y;
artist = a;
}
@Override
public int compareTo(Object o) {
//compare values
Painting other = (Painting) o;
int yearCompare = this.year.compareTo(other.year);
int artistCompare = this.artist.compareTo(other.artist);
if (yearCompare == 0) {
//same year, compare artist
return this.artist.compareTo(other.artist);
} else if (artistCompare == 0) {
return this.title.compareTo(other.title);
} else {
return yearCompare;
}
}
@Override
public String toString() {
return title.name() + " by " + artist.name() + " produced " + year.name();
}
}
Dang. ang Slow by just a few seconds. 减慢几秒钟。 Haha. 哈哈。 I came up with the same solution as shmosel. 我想出了与shmosel相同的解决方案。
public int compareTo(Painting other) {
int yearCompare = year.compareTo(other.year);
if (yearCompare != 0)
return yearCompare;
int artistCompare = artist.compareTo(other.artist);
if (artistCompare != 0)
return artistCompare;
return title.compareTo(other.title);
}
One difference. 一个区别。 I would consider changing your class header. 我会考虑更改您的类标题。 Specifically, I would change: 具体来说,我将更改:
public class Painting implements Comparable
to: 至:
public class Painting implements Comparable<Painting>
This way, instead of using the "raw" Object type, you're Painting class's compareTo() method signature will become: 这样,您将不使用“原始”对象类型,而使用Painting类的compareTo()方法签名将变为:
public int compareTo(Painting o)
In other words, you don't have to cast or worry about checking if an argument is an instance of Painting! 换句话说,您不必强制转换或担心检查参数是否是Painting的实例!
Your if-else logic is flawed in several ways. 您的if-else逻辑在几种方面存在缺陷。 It should more look like this: 它应该看起来像这样:
int yearCompare = this.year.compareTo(other.year);
if (yearCompare != 0) {
return yearCompare;
}
int artistCompare = this.artist.compareTo(other.artist);
if (artistCompare != 0) {
return artistCompare;
}
return this.title.compareTo(other.title);
On a side note, you should generics and avoid casting: 附带说明一下,您应该泛型并且避免强制转换:
public class Painting implements Comparable<Painting> {
@Override
public int compareTo(Painting other) {
// no casting necessary
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.