01/10/2018, 17:26

Giải thích đề bài sử dụng compareTo()

Program FSIS of a flower shop captures and processes data about an entity named Customer and a specific type of Customer named HighEarner. The former represents customers that are of interest to the shop, while the latter represents wealthy customers whose income are above a given threshold. Figure 1 shows the concept class diagram of FSIS. Table 1 lists the attribute design details of Customer and HighEarner. Note that attribute income is specific to HighEarner. To ease listing of customer objects, program FSIS requires that Customer realises an interface named Comparable. This interface is provided by Java. The implementation of the method Comparable.compareTo must compare two Customers by name.
mình không hiểu là cái interface comparable thì sẽ so sánh cái gì, so sánh giữa HighEarn vs nomal customer à??

Trương Tấn Phát viết 19:40 ngày 01/10/2018

must compare two Customers by name.

So sánh giữa 2 khách hàng bằng tên thôi.
Đại loại như này:

// ...
public class Customer implements Comparable<Customer>{
// ...
@Override
public int compareTo(Customer o){
    return this.name.compareTo(o.name); 
}
// ...
}
KieuThinh viết 19:28 ngày 01/10/2018

Nói chung là cái Interface Comparable và Comparator là 2 cái Interface cung cấp cho bạn các method so sánh: compareTo, equals and compare. Để sử dụng các method này bạn cần implements 2 cái interface đó, nó sẽ cung cấp cho bạn các method này theo kiểu mặc định.

Bạn cũng có thể Override lại 2 các phương thức này để so sánh dựa theo ý của bạn.

Son Ha viết 19:35 ngày 01/10/2018

This post was flagged by the community and is temporarily hidden.

Bài liên quan
0