?Java?SE?面向?qū)ο缶幊痰?個常用接口
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));
? ? }
}此代碼運(yùn)行報錯:

原因: 沒有告訴要如何進(jìn)行排序,是年齡還是姓名還是分?jǐn)?shù).沒有告訴比較的規(guī)則
解決方式:
如果自定義的數(shù)據(jù)類型 進(jìn)行大小比較 一定要實(shí)現(xiàn)可以比較的接口
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 +
? ? ? ? ? ? ? ? '}';
? ? }
?
? ? //誰調(diào)用這個方法 誰就是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);//默認(rèn)從小到大排序
?
? ? ? ? System.out.println("比較后 "+Arrays.toString(students));
? ? }
}如果要 分?jǐn)?shù)比較 和 姓名比較
? //誰調(diào)用這個方法 誰就是this
? ? @Override
? ? public int compareTo(Student o) {
? ? ? ? //return this.age - o.age;//從小到大
? ? ? ? //return o.age - this.age;//從大到小
? ? ? ? return (int) (this.score - o.score);//分?jǐn)?shù)排序
? ? ? ? return this.name.compareTo(o.name);//姓名排序
? ? }缺點(diǎn): 這個接口對類的侵入性非常強(qiáng).一旦寫好了,不敢輕易改動.
如何降低對類的侵入性呢?
使用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("比較后(按分?jǐn)?shù)) "+Arrays.toString(students1));
? ? }
}運(yùn)行結(jié)果:

優(yōu)點(diǎn):對類的侵入性非常弱.
3.Cloneable
面試問題:
你知道Cloneable接口嗎?為啥這個接口是一個空接口?有啥作用?
空接口 -> 標(biāo)志接口 -> 代表當(dāng)前這個類是可以被克隆的.
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);
? ? }
}運(yùn)行結(jié)果:

注意事項:
- 1.引用的對象要想被克隆,必須實(shí)現(xiàn)Cloneable接口.
- 2.必須重寫克隆方法,并且聲明異常.
到此這篇關(guān)于 Java SE 面向?qū)ο缶幊痰?個常用接口的文章就介紹到這了,更多相關(guān) Java SE 面向?qū)ο缶幊探涌趦?nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
對python中GUI,Label和Button的實(shí)例詳解
今天小編就為大家分享一篇對python中GUI,Label和Button的實(shí)例詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
python神經(jīng)網(wǎng)絡(luò)MobileNetV2模型的復(fù)現(xiàn)詳解
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)MobileNetV2模型的復(fù)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Python中創(chuàng)建包和增添包的路徑(sys.path.append())
本文主要介紹了Python中創(chuàng)建包和增添包的路徑(sys.path.append()),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
python3.x上post發(fā)送json數(shù)據(jù)
這篇文章通過代碼示例給大家講述了python3.x上post發(fā)送json數(shù)據(jù)的詳細(xì)方法,一起學(xué)習(xí)下。2018-03-03
Anaconda最新版2023安裝教程Spyder安裝教程圖文詳解
這篇文章主要介紹了Anaconda最新版2023安裝教程Spyder安裝教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05
Python圖像運(yùn)算之頂帽運(yùn)算和底帽運(yùn)算詳解
數(shù)學(xué)形態(tài)學(xué)是應(yīng)用于圖像處理和模式識別領(lǐng)域的新方法。數(shù)學(xué)形態(tài)學(xué)表示以形態(tài)為基礎(chǔ)對圖像進(jìn)行分析的數(shù)學(xué)工具,基本思想是用具有一定形態(tài)的結(jié)構(gòu)元素去量度和提取圖像中對應(yīng)形狀以達(dá)到對圖像分析和識別的目的。本文將為大家介紹頂帽運(yùn)算和底帽運(yùn)算,需要的可以參考一下2022-07-07
Python利用字典將兩個通訊錄文本合并為一個文本實(shí)例
這篇文章主要介紹了Python利用字典將兩個通訊錄文本合并為一個文本實(shí)例,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
用Python進(jìn)行行為驅(qū)動開發(fā)的入門教程
這篇文章主要介紹了用Python進(jìn)行行為驅(qū)動開發(fā)的入門教程,本文也對BDD的概念做了詳細(xì)的解釋,需要的朋友可以參考下2015-04-04
python中np.multiply()、np.dot()和星號(*)三種乘法運(yùn)算的區(qū)別詳解
這篇文章主要介紹了python中np.multiply()、np.dot()和星號(*)三種乘法運(yùn)算的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

