日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

java 常用接口的实现--compareable

發(fā)布時(shí)間:2025/7/25 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 常用接口的实现--compareable 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

//主要用于實(shí)現(xiàn)比較的接口 用于對(duì)象的比較大小 排序等操作

?//interface declaration:

?

/**
?* This interface should be implemented by all classes that wish to define a
?* <em>natural order</em> of their instances.
?* mailto:%7B@link java.util.Collections#sort} and mailto:%7B@code java.util.Arrays#sort} can then
?* be used to automatically sort lists of classes that implement this interface.
?* <p>
?* The order rule must be both transitive (if mailto:%7B@code x.compareTo(y) < 0} and
?* mailto:%7B@code y.compareTo(z) < 0}, then mailto:%7B@code x.compareTo(z) < 0} must hold) and
?* invertible (the sign of the result of x.compareTo(y) must be equal to the
?* negation of the sign of the result of y.compareTo(x) for all combinations of
?* x and y).
?* <p>
?* In addition, it is recommended (but not required) that if and only if the
?* result of x.compareTo(y) is zero, then the result of x.equals(y) should be
?* mailto:%7B@code true}.
?*/
public interface Comparable<T> {

??? /**
???? * Compares this object to the specified object to determine their relative
???? * order.
???? *
???? * @param another
???? *??????????? the object to compare to this instance.
???? * @return a negative integer if this instance is less than mailto:%7B@code another};
???? *???????? a positive integer if this instance is greater than
???? *???????? mailto:%7B@code another}; 0 if this instance has the same order as
???? *???????? mailto:%7B@code another}.
???? * @throws ClassCastException
???? *???????????? if mailto:%7B@code another} cannot be converted into something
???? *???????????? comparable to mailto:%7B@code this} instance.
???? */
??? int compareTo(T another);
}

?

?

//for example:

public class HighScore implements Comparable<HighScore>
{
?private String myDate;
?private int myGameType;
?private int myLevel;
?
?public HighScore(String date, int gameType, int level)
?{
??myDate = date;
??myGameType = gameType;
??myLevel = level;
?}
?
?@Override
?public int compareTo(HighScore other)
?{
??if(this.getGameType()==other.getGameType())
??{
???if(this.getLevel()==other.getLevel())
???{
????return 0;
???}
???else if(this.getLevel()>other.getLevel())
???{
????return -1;
???}
???else
???{
????return 1;
???}
??}
??else
??{
???if(this.getGameType()>other.getGameType())
???{
????return -1;
???}
???else
???{
????return 1;
???}
??}
?}
?
?@Override
?public String toString()
?{
??return myDate+";"+myGameType+";"+myLevel;
?}

?//*********GETTERS*************/
?public String getDate()
?{
??return myDate;
?}

?public int getGameType()
?{
??return myGameType;
?}
?
?public int getLevel()
?{
??return myLevel;
?}
}


//上面便是實(shí)現(xiàn)的例子?

?

轉(zhuǎn)載于:https://www.cnblogs.com/fwycmengsoft/archive/2011/11/14/2248002.html

總結(jié)

以上是生活随笔為你收集整理的java 常用接口的实现--compareable的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。