Icomparable

winnyThePoo

New member
Icomparable

איך משתמשים ב- Icomparable למיין class שאני בניתי בלי להשתמש ב-sort של array או arrayList ????????
 

itaym02

New member
קוד לדוגמה

את צריכה לתת תוכן למטודה שIComparable מספקת. המיון עובד על פי המטודה הזאת. מעבר לזה האינטרפייס לא עושה דבר נוסף.
public class Shape: IComparable { // An Example of a class .... //******************************************************* // CompareTo // // This method is inherited from interface IComparable. // This method is used for the Sort method of the ArrayList // object. // The sorting will be in decending order // The method returns 1 if the input is bigger then // the Shape this methode belongs to // The method returns 0 if the input Shape equals the shape // this methode belongs to // The method returns -1 if the input is lower then // the Shape this methode belongs to //******************************************************* public int CompareTo(object I_Shape) { Shape ShapeToCompare =(I_Shape as Shape); if (ShapeToCompare.areaOfShape>this.areaOfShape) return (1); //input area is bigger // then current object if (ShapeToCompare.areaOfShape<this.areaOfShape) return (-1); //input area is smaller // then current object return 0; //input area equals the current object } }​
כל מה שמצפים ממטודה זאת הוא להחזיר 1 או 0 או -1 ולסדר את האובייקטים לפי ההשוואה ביניהם. מקווה שעזר
 
למעלה