🐷1.接口的概念
🐱🚀2.接口的语法规则
💚3.接口的使用
🚀4.接口的特性
🎆5.实现多个接口
🐶6.接口间的继承
🎊7.接口使用的实例
1.什么是接口呢???
接口放在现实生活中来说呢,就是手机啊。电脑的插座之类的,我们会发现那些插座几乎大家的充电口都能用,那么这就是一种标准
接口不能直接实例化,会报错
interface IShape{public abstract void draw();public static final String name="一宝";default public void func(){System.out.println("默认的");}}
class Rect implements IShape{@Overridepublic void draw() {System.out.println("🟥");}
}
class Circle implements IShape{@Overridepublic void draw() {System.out.println("○");}
}
class Heart implements IShape{@Overridepublic void draw() {System.out.println(";❤");}
}public class Test {public static void main(String[] args) {IShape shape = new Rect();IShape shape2 = new Circle();IShape shape3 = new Heart();shape.draw();shape.func();shape2.draw();shape2.func();shape3.draw();shape3.func();}}
对于shape.func()可以这样理解,调用shape.draw()发生动态绑定,就到那个Rect重写的draw方法去了,但是调用shape.fun()就到接口中去了,因为func本身也是一个抽象方法,因为有了default的修饰,所以可以不在子类中重写,通过接口的引用,就直接到IShape当中去调了
下面实现电脑USB接口
package demo2;/*** Created with IntelliJ IDEA.* Description:* User: WHY* Date: 2022-11-16* Time: 10:52*/
interface IUSB{void openDevice();void closeDevice();
}
class KeyBoard implements IUSB {@Overridepublic void openDevice() {System.out.println("打开键盘");}@Overridepublic void closeDevice() {System.out.println("关闭键盘");}public void intput(){System.out.println("敲击键盘");}
}
class Mouse implements IUSB{@Overridepublic void openDevice() {System.out.println("打开鼠标");}@Overridepublic void closeDevice() {System.out.println("关闭鼠标");}public void click(){System.out.println("点击鼠标");}
}
class Computer{public void open(){System.out.println("开机");}public void close(){System.out.println("关机");}public void useDevice(IUSB usb){//判断一下是不是所有的USB接口都可以被使用usb.openDevice();if(usb instanceof Mouse){Mouse mouse=(Mouse)usb;//向下转型mouse.click();//执行子类中特有的方法}else if(usb instanceof KeyBoard){KeyBoard keyBoard=(KeyBoard)usb;keyBoard.intput();}usb.closeDevice();}
}public class Test {public static void main(String[] args) {Computer computer=new Computer();IUSB iusb=new KeyBoard();//向上转型computer.useDevice(iusb);IUSB iusb1=new Mouse();computer.useDevice(iusb1);}}
package demo3;/*** Created with IntelliJ IDEA.* Description:* User: WHY* Date: 2022-11-16* Time: 13:48*/
abstract class Animal{public String name;public Animal(String name) {this.name = name;}
}
interface IRunning{void run();
}
interface ISwimming{void swim();
}
interface IFly{void fly();
}
class Dog extends Animal implements IRunning{public Dog(String name) {super(name);}@Overridepublic void run() {System.out.println(name+"正在跑");}}
class Duck extends Animal implements IFly,IRunning,ISwimming{public Duck(String name) {super(name);}@Overridepublic void run() {System.out.println(name+"正在跑");}@Overridepublic void fly() {System.out.println(name+"正在飞");}@Overridepublic void swim() {System.out.println(name+"正在游泳");}
}
class robot implements ISwimming{public String name;public robot(String name) {this.name = name;}@Overridepublic void swim() {System.out.println(name+"机器人在游泳");}
}public class TestDemo {public static void walk(IRunning iRunning){iRunning.run();}public static void swim(ISwimming iSwimming){iSwimming.swim();}public static void main(String[] args) {walk(new Dog("贝贝"));walk(new Duck("丫丫"));swim(new Duck("丫丫1"));swim(new robot("小冰"));}}
从这两段代码可以看出,只要这个类继承了这个接口,就可以实现
package demo4;/*** Created with IntelliJ IDEA.* Description:* User: WHY* Date: 2022-11-16* Time: 14:14*/
class Student implements Comparable<Student>{public String name;public int age;public int score;public Student(String name, int age, int score) {this.name = name;this.age = age;this.score = score;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", score=" + score +'}';}public int compareTo(Student o){//根据年龄比较排序if(this.age>o.age){return 1;}else if(this.age<o.age){return -1;}else{return 0;}}
}
//根据姓名字母排序/* if(this.name.compareTo(o.name) > 0) {return 1;}else if(this.name.compareTo(o.name) < 0) {return -1;}else {return 0;}*/
//使用冒泡排序实现依据年龄排序
public class Test {public static void sort(Comparable[] array) {for (int i = 0; i < array.length-1; i++) {for (int j = 0; j < array.length-1-i; j++) {/*if(array[j] > array[j+1]) {交换;}*/if(array[j].compareTo(array[j+1]) > 0) {Comparable tmp = array[j];array[j] = array[j+1];array[j+1] = tmp;}}}}public static void main(String[] args) {Student[] students=new Student[3];students[0]=new Student("wangyibo",25,100);students[1]=new Student("weihongyan",19,99);students[2]=new Student("gaoyan",19,95);System.out.println(students[0].compareTo(students[1]));}}
这个是调用sort函数实现排序
这个是调用compareTo方法实现的
当比较字符串是否相等用equals方法,当比较大小也要用compareT方法,要是想从大到小排,换个return 就好
下面来总结一下接口的知识点
使用interface 接口来定义接口
接口不能被实例化
接口中的方法默认是public abstract 修饰的
接口中的成员默认是public static final 修饰的
接口当中的方法,不能有具体实现,但是从jdk8开始,可以写一个default来修饰,我已经子啊前面的代码展示并解释了
接口当中不能有构造方法
接口需要被类实现,使用关键字implements
接口中可以有static修饰的方法
接口和接口之间使用关键字extends继承,与类的单继承不一样啊,类可以同时继承多个接口,接口和接口之间也可以多继承
interface A {void funcA();}
interface B {void funcB();
}
//CC这个接口 不仅仅具备func这个功能,还具备了A和B接口的功能
interface CC extends A,B {void funcC();
}class C implements CC {public void funcA() {}public void funcB() {}public void funcC() {}}
public class Test2 {
}
就像这个,一个接口也可以同时继承多个接口,并拥有被继承接口的功能
类继承另一个类同时实现多个接口,代码也有体现到
今天的内容就到此结束,我们下期再见,886!!!
💚💚💚💚💚💚💚💚💚💚