interfaceAnimal{voidGoes();}classCatimplementsAnimal{publicvoidGoes(){
System.out.println("mew-mew");}}classDogimplementsAnimal{publicvoidGoes(){
System.out.println("wang-wang");}}publicclassTestInterface{publicstaticvoiduse(Animal a)//接口做参数{
a.Goes();//接口变量调用}publicstaticvoidmain(String[] args){
Cat c =newCat();
System.out.println("Cat goes");use(c);
System.out.println();
Dog d =newDog();
System.out.println("Dog goes");use(d);}}
接口多态性
父类引用子类
publicinterfaceAnimal//接口{publicabstractvoideat();//抽象方法eat();}publicclassCatimplementsAnimal//实现接口的Cat类{publicvoideat(){
System.out.println(“Cats eat milk“);}}...//主类省略
Animal a =newCat();
a.cry();...
接口回调
开发中很实用
接口做参数
interfaceShow{voidshow();}classAimplementsShow{publicvoidshow(){
System.out.println("I love This Game");}}classBimplementsShow{publicvoidshow(){
System.out.println("Œ“œ≤ª∂ø¥NBA");}}classC{publicvoidf(Show s)//Ω”ø⁄◊ˆ≤Œ ˝{
s.show();}}publicclassExample13{publicstaticvoidmain(String args[]){
C c=newC();
c.f(newA());
c.f(newB());}}