1. 程式人生 > >Algs4-2.1.21可比較的交易

Algs4-2.1.21可比較的交易

turn cti 模板 實現 date com compareto nts rabl

2.1.21可比較的交易。用我們的Date類(請見2.1.1.4節)作為模板擴展你的Transaction類(請見練習1.2.13),實現Comparable接口,使交易能夠按照金額排序。
解答:
public class Transaction implements Comparable<Transaction>
{
private final double amount;

public int compareTo(Transaction that)
{
if (this.amount>that.amount) return +1;
if (this.amount<that.amount) return -1;
return 0;
}
...
}

Algs4-2.1.21可比較的交易