首頁 > 軟體

 Java SE 物件導向程式設計的3個常用介面

2022-01-15 16:00:03

1.Comparable

前言,想要排序Student.有程式碼:

import java.util.Arrays;
 
class Student {
    public int age;
    public String name;
    public double score;
 
    public Student(int age, String name, double score) {
        this.age = age;
        this.name = name;
        this.score = score;
    }
 
    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + ''' +
                ", score=" + score +
                '}';
    }
}
 
public class TestDemo {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student(12,"niubi",99.9);
        students[1] = new Student(20,"liuren",18.9);
        students[2] = new Student(80,"laoren",50.9);
        System.out.println(Arrays.toString(students));
 
        Arrays.sort(students);
 
        System.out.println(Arrays.toString(students));
    }
}

此程式碼執行報錯:

原因: 沒有告訴要如何進行排序,是年齡還是姓名還是分數.沒有告訴比較的規則

解決方式:

如果自定義的資料型別 進行大小比較 一定要實現可以比較的介面

import java.util.Arrays;
 
class Student implements Comparable<Student>{
    public int age;
    public String name;
    public double score;
 
    public Student(int age, String name, double score) {
        this.age = age;
        this.name = name;
        this.score = score;
    }
 
    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + ''' +
                ", score=" + score +
                '}';
    }
 
    //誰呼叫這個方法 誰就是this
    @Override
    public int compareTo(Student o) {
        //return this.age - o.age;//從小到大
        return o.age - this.age;//從大到小
    }
 
}
 
public class TestDemo {
 
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student(12,"niubi",99.9);
        students[1] = new Student(6,"liuren",18.9);
        students[2] = new Student(80,"laoren",50.9);
        System.out.println("比較前 "+Arrays.toString(students));
 
        Arrays.sort(students);//預設從小到大排序
 
        System.out.println("比較後 "+Arrays.toString(students));
    }
}

如果要 分數比較 和 姓名比較

  //誰呼叫這個方法 誰就是this
    @Override
    public int compareTo(Student o) {
        //return this.age - o.age;//從小到大
        //return o.age - this.age;//從大到小
        return (int) (this.score - o.score);//分數排序
        return this.name.compareTo(o.name);//姓名排序
    }

缺點: 這個介面對類的侵入性非常強.一旦寫好了,不敢輕易改動.

如何降低對類的侵入性呢?

 使用Comparator

2.Comparator 比較器

import java.util.Arrays;
import java.util.Comparator;
 
class Student1 {
    public int age;
    public String name;
    public double score;
 
    public Student1(int age, String name, double score) {
        this.age = age;
        this.name = name;
        this.score = score;
    }
 
    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + ''' +
                ", score=" + score +
                '}';
    }
}
 
class AgeComparator implements Comparator<Student1>{
    @Override
    public int compare(Student1 o1, Student1 o2) {
        return o1.age - o2.age;
    }
}
 
class ScoreComparator implements Comparator<Student1>{
    @Override
    public int compare(Student1 o1, Student1 o2) {
        return (int) (o1.score - o2.score);
    }
}
 
class NameComparator implements Comparator<Student1>{
    @Override
    public int compare(Student1 o1, Student1 o2) {
        return o1.name.compareTo(o2.name);
    }
}
 
public class TestDemo1 {
 
    public static void main(String[] args) {
        Student1[] students1 = new Student1[3];
        students1[0] = new Student1(12,"niubi",99.9);
        students1[1] = new Student1(6,"liuren",18.9);
        students1[2] = new Student1(80,"laoren",50.9);
        System.out.println("比較前 "+Arrays.toString(students1));
 
        AgeComparator ageComparator = new AgeComparator();
        Arrays.sort(students1,ageComparator);
        System.out.println("比較後(按年齡) "+Arrays.toString(students1));
 
        ScoreComparator scoreComparator = new ScoreComparator();
        Arrays.sort(students1,scoreComparator);
        System.out.println("比較後(按姓名) "+Arrays.toString(students1));
 
        NameComparator nameComparator = new NameComparator();
        Arrays.sort(students1,nameComparator);
        System.out.println("比較後(按分數) "+Arrays.toString(students1));
    }
}

執行結果:

優點:對類的侵入性非常弱.

3.Cloneable

面試問題:

你知道Cloneable介面嗎?為啥這個介面是一個空介面?有啥作用?

空介面 -> 標誌介面 -> 代表當前這個類是可以被克隆的.

class Person implements Cloneable{
    public int age ;
    public void eat(){
        System.out.println("吃!");
    }
 
    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                '}';
    }
 
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
public class TestDemo2 {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person();
        person.age = 99;
        Person person2 = (Person) person.clone();
        System.out.println(person.age);
        System.out.println(person2.age);
 
        System.out.println("==========");
        person2.age = 199;
        System.out.println(person.age);
        System.out.println(person2.age);
    }
}

執行結果:

注意事項:

  • 1.參照的物件要想被克隆,必須實現Cloneable介面.
  • 2.必須重寫克隆方法,並且宣告異常.

 到此這篇關於 Java SE 物件導向程式設計的3個常用介面的文章就介紹到這了,更多相關 Java SE 物件導向程式設計介面內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com