About defining the interface Comparable (comparator) to compare the sizes of two objects

1. Topic: Comparing the sizes of objects of different reference types:

Requirement description: Although the student category and the news category are completely different categories, they both have the ability to compare. For example, you can compare the size of two students, but you need to specify whether the comparison is based on student number, name, or grades, etc.

Implementation ideas and key code descriptions:
Define the ability to compare as an interface, and let students and news classes implement this interface.

(1) Define the interface Comparable, which contains the only method int compareTo(Object obj); The return value >0 means greater than, the return value =0 means equal to, and the return value <0 means less than.

(2) Define the student class, including student ID, name, age and scores, implement the Comparable interface, and implement the comparison method;

(3) Define the news category, including number (int type), title, content and number of clicks, implement the Comparable interface, and implement the comparison method;

(4) Define the test class, create two student objects and news objects respectively, compare them and output the results of the comparison.

2. Analysis:

(1) An uncommon keyword is used in the compareTo(Object obj) method: instanceof keyword, which is used to determine whether an object is Instance of a class (or interface): Usage: Object + instanceof + class (interface) .

(2) Or in the compareTo(Object obj) method, you can also use a method called getClass() method. Simply put, it returns: which class’s .class file is loaded in the memory at runtime

That is, it becomes like this to judge the parameters passed in:

public int compareTo(Object obj) {
        if (obj.getClass() != getClass()) {
            return -1;
      //Determine whether the .class file of the passed in obj object is the .class file of the current Student class object
                          
        }
   /*
      The same class will not be loaded twice in the memory. If the obj object passed in points to the same class as the class originally loaded in the memory, that is
                                                                 Object of Student class
      (That is, there will not be two Student.classes in the memory, they will only be loaded once)
   
*/
            //The code for judging other conditions will be omitted here...
    }

(3) Find out the member variables of each class and encapsulate them with private.

3. Code implementation

(For the time being, I have only written the comparison of two objects of the Student class, and found some problems in it, which are about the comparison of names and student numbers (that is, when the student number contains numbers and uppercase and lowercase English) Improvements will be made over time, but the general problem can be achieved)

(1)Comparator

//Comparator
public interface Comparable {
    public int compareTo(Object obj);//Comparison method

}

(2) Student

public class Student implements Comparable{
    private String sid; //student number
    private String name; //name
    private int age; //Age
    private int score; //total score
    public Student() {
          this.sid="null";
          this.name="null";
          this.age=0;
          this.score=0; //Automatically initialized when no value is assigned
    }

    public Student(String sid, String name, int age, int score) {
        this.sid = sid;
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public int compareTo(Object obj) {
        /*if (obj.getClass() != getClass()) {
            return -1; //Determine whether the .class file of the passed in obj object is the .class file of the current Student class object
        }
       /*
          The same class will not be loaded twice in the memory. If the obj object passed in and the class originally loaded in the memory point to the same object, it is the object of the Student class.
          (That is, there will not be two Student.classes in the memory, they will only be loaded once)
       */
        if(!(obj instanceof Student)){

            return 2;
        }
        //Type cast
        Student student = (Student) obj;
        if((this.sid.compareTo(student.sid))==1){
            return 1;
        }
        else if((this.sid.compareTo(student.sid))==-1){
            return -1;
        }else if((this.name.compareTo(student.name))==1){
            return 1;
        }else if((this.name.compareTo(student.name))==-1){
            return -1;
        }else if(this.score>student.score){
            return 1;
        }else if(this.score<student.score){
            return -1;
        }
        return 0;
    }

}

(3) Test class Test

public class Test01 {
    public static void main(String[] args) {
         Student student1 =new Student("2200130225","Zhang San",18,82);
         Student student2= new Student("2200130224","王五",18,88);
         int i=student1.compareTo(student2);

         if(i==-1){
             System.out.println("The output is from small to large");
             System.out.println("Student number: " + student1.getSid() + ", Name: " + student1.getName() + ", Age: " + student1.getAge() + " ,The total score is: " + student1.getScore());
             System.out.println("Student number: " + student2.getSid() + ", Name: " + student2.getName() + ", Age: " + student2.getAge() + " ,The total score is: " + student2.getScore());
         }
         if(i==1){
             System.out.println("The output is from small to large");
             System.out.println("Student number: " + student2.getSid() + ", Name: " + student2.getName() + ", Age: " + student2.getAge() + " ,The total score is: " + student2.getScore());
             System.out.println("Student number: " + student1.getSid() + ", Name: " + student1.getName() + ", Age: " + student1.getAge() + " ,The total score is: " + student1.getScore());
         }
         if(i==0){
             System.out.println("Two comparison objects are equal");
         }
         if(i==2) {
             System.out.println("Sorry you entered the wrong comparison object, please re-enter:");
         }
    }
}

(5) Two of the example test output results: (The code first compares the student number, then compares the name, and finally compares the score to output from small to large)