【问题标题】:Implements Comparable Missing One trait实现可比缺失一个特征
【发布时间】:2016-08-21 05:00:21
【问题描述】:

所以我正在编写一个非常基本的代码,它实现Comparable 根据年份、艺术家和标题比较一幅画。

但是我的代码不是按标题比较画作,而是按年份和艺术家比较。

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();
    }

}

【问题讨论】:

标签: java enums comparable implements


【解决方案1】:

您的 if-else 逻辑在几个方面存在缺陷。它应该更像这样:

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);

附带说明,您应该使用泛型并避免强制转换:

public class Painting implements Comparable<Painting> {
    @Override
    public int compareTo(Painting other) {
        // no casting necessary
    }
}

【讨论】:

    【解决方案2】:

    当。慢了几秒钟。哈哈。我想出了与 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);
    }
    

    一个区别。我会考虑更改您的班级标题。具体来说,我会改变:

    public class Painting implements Comparable
    

    到:

    public class Painting implements Comparable<Painting>
    

    这样,您的绘画类的 compareTo() 方法签名将变为:

    public int compareTo(Painting o)
    

    换句话说,您不必强制转换或担心检查参数是否是绘画的实例!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      • 2015-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多