/*
* 语法1:使用interface关键字定义
* 语法2:接口中,允许定义“抽象方法”,public 和 abstract关键字可以省略
* 语法3:接口中,允许定义“默认方法”
* 语法4:实现类通过implements关键字实现接口,可以同时实现多个接口
*/
package com.ztt.Demo01MyInterface;/** 语法1:使用interface关键字定义* 语法2:接口中,允许定义“抽象方法”,public 和 abstract关键字可以省略* 语法3:接口中,允许定义“默认方法”* 语法4:实现类通过implements关键字实现接口,可以同时实现多个接口*/public interface MyInterface {//允许定义“抽象方法”void dosth();public abstract void absTDowork();//允许定义“默认方法”public default void dolist() {}//不允许定义“构造方法”
// public MyInterface() {
//
// }//不允许定义“普通方法”
// public void dolist() {
//
// }}
class Ext implements MyInterface,AnotherInterface{@Overridepublic void dosth() {// TODO Auto-generated method stub}@Overridepublic void absTDowork() {// TODO Auto-generated method stub}@Overridepublic void todo() {// TODO Auto-generated method stub}}
package com.ztt.Demo01MyInterface;public interface AnotherInterface {public default void todo() {}
}
package com.ztt.Demo01MyInterface;import java.util.Arrays;
import java.util.Comparator;//interface 接口的使用场景:Arrays.sort()方法自定义排序
public class test1 {public static void main(String[] args) {String[] arr= {"def","abcd","abb","aaa","hij","aaaa","bnn","cdd"};//默认按照字符串的ASCII码值进行比较//Arrays.sort(arr);//按照字符串的长度进行比较,如果长度一致,再按照ASCII码值进行比较//默认升序StringLengthAndAsciiComparator comp1=new StringLengthAndAsciiComparator();//升序Arrays.sort(arr,comp1);//降序Comparator<String> comp2=comp1.reversed();Arrays.sort(arr,comp2);for(String s:arr) {System.out.println(s);}}}//Comparator接口实现类:封装“排序是的比较”逻辑
class StringLengthAndAsciiComparator implements Comparator<String>{//正数代表o1>o2//负数代表o1>o2//零代表o1=o2public int compare(String o1,String o2) {//按照字符串的长度进行比较,如果长度一致,再比较Ascii码值进行比较if(o1.length()==o2.length()) {//按照字符串Ascii码值进行比较return o1.compareTo(o2);}return o1.length()-o2.length();}
}
运行结果:
def
cdd
bnn
abb
aaa
package com.ztt.Demo01MyInterface;import java.util.Arrays;
import java.util.Comparator;public class BookTest {public static void main(String[] args) {//对象数组Book[] books = {new Book("E",1247,17.85,10005),new Book("A",8547,33.58,10005),new Book("H",7413,18.88,10007),new Book("K",9631,19.85,10001),new Book("B",7581,22.51,10006),new Book("M",1574,16.89,10002)};//排序//按照图书名称进行比较排序BookNameComparator comp1=new BookNameComparator();
// Arrays.sort(books,comp1);//按照图书页数进行比较排序BookPageSizeComparator comp2=new BookPageSizeComparator();//传入不同的Comparator比较器对象//比较规则不同,则排序结果不同//Arrays.sort( books, comp2);//按照默认比较规则Arrays.sort(books);//输出for(Book b: books) {System.out.println(b);}}}//按照图书名称进行"自定义比较规则"
class BookNameComparator implements Comparator<Book>{@Overridepublic int compare(Book b1, Book b2) {// 按照书名进行比较return b1.getBookName().compareTo(b2.getBookName());}}
//按照图书页数进行"自定义比较规则"
class BookPageSizeComparator implements Comparator<Book>{@Overridepublic int compare(Book o1, Book o2) {// 按照页数进行比较return o1.getPageSize()-o2.getPageSize();}}
//图书类
class Book implements Comparable<Book>{private String bookName;//图书名称private int pageSize;//总页数private double price;//销售价格private int sale;//销量public Book(String bookName,int pageSize,double price,int sale) {this.bookName=bookName;this.pageSize=pageSize;this.price=price;this.sale=sale;}@Overridepublic boolean equals(Object obj) {if(this==obj) {return true;}if(obj instanceof Book) {//向下转型Book anotherBook=(Book)obj;return this.bookName.equals(anotherBook.bookName) &&this.pageSize == anotherBook.pageSize &&this.price == anotherBook.price &&this.sale == anotherBook.sale;}return false;}//实现Comparable接口时,重写compareTo@Overridepublic int compareTo(Book anotherBook) {// this和anotherBook进行比较//默认按照销量进行比较return this.getSale()-anotherBook.getSale();}//重写Object类的toString()方法@Overridepublic String toString() {return String.format("<<%s>>,共计%d页,销售价格$%.2f, 总销量%d本",bookName,pageSize,price,sale);}public String getBookName() {return bookName;}public void setBookName(String bookName) {this.bookName = bookName;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public int getSale() {return sale;}public void setSale(int sale) {this.sale = sale;}}
运行结果:
<<K>>,共计9631页,销售价格$19.85, 总销量10001本
<<M>>,共计1574页,销售价格$16.89, 总销量10002本
<<E>>,共计1247页,销售价格$17.85, 总销量10005本
<<A>>,共计8547页,销售价格$33.58, 总销量10005本
<<B>>,共计7581页,销售价格$22.51, 总销量10006本
<<H>>,共计7413页,销售价格$18.88, 总销量10007本
package com.ztt.Demo01MyInterface;public class testBook {public static void main(String[] args) {Book b1 = new Book("E",1247,17.85,10005);Book b2 = new Book("A",8547,33.58,10005);Book b3 = new Book("E",1247,17.85,10005);Book b4 = new Book("H",7413,18.88,10007);//比大小// b1.compareTo(b2);//比内存地址是否相同// b1 == b2System.out.println(b1 == b3);//比"值"(内容数据)是否相同//b1.equals(b2);System.out.println(b1.equals(b3));}}
false
true
package com.ztt.Demo03Document;//新闻文档
public class Document implements Comparable<Document>{private int star; // 点赞数private String title; // 文档标题private int visit; // 访问量//重写Comparable接口中的抽象方法:提供比较大小的逻辑@Overridepublic int compareTo(Document another) {// 默认使用点赞数return this.star - another.star;}//重写Object类的equals():比较内容是否一致(等值比较)@Overridepublic boolean equals(Object another) {// 先比较内存地址if(this==another) {return true;}if(another instanceof Document) {//向下转型Document anotherDoc=((Document) another);//同时比较“点赞数+访问量+标题”return this.star==anotherDoc.star && this.visit==anotherDoc.visit && this.title.equals(anotherDoc.title);}//默认返回falsereturn false;}// 构造方法public Document(int star,String title,int vist) {this.setStar(star);this.setTitle(title);this.setVisit(vist);}public int getVisit() {return visit;}public void setVisit(int visit) {this.visit = visit;}@Overridepublic String toString() {return "Document [点赞数=" + star + ", 标题=" + title + ", 访问量=" + visit + "]";}public int getStar() {return star;}public void setStar(int star) {this.star = star;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}}
测试类:
package com.ztt.Demo03Document;import java.util.Arrays;
import java.util.Comparator;public class test1 {public static void main(String[] args) {Document[] documentArray = {new Document(324, "05你觉得新冠疫情会持续多久才能彻底结束?",567),new Document(632, "03女朋友动不动就生气怎么办?",123),new Document(135, "02央行大放水,但是房价和股市都没有大涨,那么,钱去哪里了?",345),new Document(246, "04庄家能把散户看透到什么地步?",456),new Document(975, "06事业单位,上班如上坟,已经有些抑郁了,我该考虑辞职吗?",321),new Document(235, "01微信全文搜索技术优化",789),};//实现对象数组documentArray的排序//Arrays.sort(documentArray);//仅传入了数组//按照标题名称//方式1//DocumentTitleComparator comp = new DocumentTitleComparator();//Arrays.sort(documentArray,comp);//方式2//Arrays.sort(documentArray,new DocumentTitleComparator());//按照访问量//匿名实现类Arrays.sort(documentArray,new Comparator<Document>() {@Overridepublic int compare(Document o1, Document o2) {return o1.getVisit() - o2.getVisit();} });for(Document doc : documentArray) {System.out.println(doc.toString());}}
}
//按照标题比较
class DocumentTitleComparator implements Comparator<Document>{@Overridepublic int compare(Document o1, Document o2) {return o1.getTitle().compareTo(o2.getTitle());}}
运行结果:
Document [点赞数=632, 标题=03女朋友动不动就生气怎么办?, 访问量=123]
Document [点赞数=975, 标题=06事业单位,上班如上坟,已经有些抑郁了,我该考虑辞职吗?, 访问量=321]
Document [点赞数=135, 标题=02央行大放水,但是房价和股市都没有大涨,那么,钱去哪里了?, 访问量=345]
Document [点赞数=246, 标题=04庄家能把散户看透到什么地步?, 访问量=456]
Document [点赞数=324, 标题=05你觉得新冠疫情会持续多久才能彻底结束?, 访问量=567]
Document [点赞数=235, 标题=01微信全文搜索技术优化, 访问量=789]
package com.ztt.Demo03Document;public class test3 {public static void main(String[] args) { Document doc1 = new Document(235,"02.微信全文搜索技术优化",746);Document doc2 = new Document(235,"03.女朋友动不动就生气怎么办?",4757);Document doc3 = new Document(246,"04.庄家能把散户看透到什么地步?",7547);Document doc4 = new Document(235,"02.微信全文搜索技术优化",746);//通过compareTo()方法比较大小(按照点赞数)//system.out.println( doc1.compareTo( doc2));l //System.out.println( doc1.compareTo( doc3) );//通过compareTo()方法比较内容是否相等//存在的问题:点赞数一致但其余数据不相同//System.out.println(doc1.compareTo(doc2));//存在问题//引用数据类型:==比较内存地址System.out.println(doc1 == doc4);//引用数据类型:重写object类的equals()方法System.out.println(doc1.equals(doc4));}
}
运行结果:
false
true
小结
1.Java的接口( interface )用于定义纯粹的抽象行为(方法)规范。
2.一个类只能extends继承自另一个类(单继承)。但是,一个类可以implements实现多个interface接口(多重实现)。
3.一个interface可以继承自多个interface(接口之间允许多重继承)。。一个类实现interface接口时,要求必须实现该接口中所有的抽象方法。
4.接口也是一种“高层”的数据类型,可以用于定义“引用”的类型,也适用于"向上转型"和"向下转型"。·接口不允许实例化。
5.接口的所有方法都是抽象方法,接口不能定义实例字段(成员变量)。
6.接口可以定义default方法([ JDK版本>= 1.8 ) , default方法可以包含方法体。7.实现 Comparable接口的类,该类的对象之间可以进行“比较”,用于“排序”。