JAVA——接口的基本概念,使用限制,应用(USB接口实例,停车场停车实例)
接口的基本概念,使用限制,应用
(一)接口的概念
- 接口存在的意义:抽象类虽然能约定子类的实现要求,但是具有单继承缺陷,为了解决抽象类的单继承缺陷,定义了接口。
- 在使用时,接口优先于抽象类,能使用接口就不使用抽象类。
- 接口的定义:接口是抽象方法和全局变量的集合。
- 接口使用 interface 关键字定义接口, implements 关键字实现接口。
- 一个子类可以实现多个接口。子类必须覆写接口中的所有抽象方法。
- 接口可以通过子类的实例化来得到实例化对象。和抽象类类似。
- 若一个子类同时实现多个接口,则这多个接口之间可以相互转换。
接口的简单应用:
//1. 定义接口SchoolA,接口内定义一个抽象方法printA()。
interface SchoolA{
public void printA();
}
//2. 定义接口SchoolB,接口内定义一个抽象方法printB()。
interface SchoolB{
public void printB();
}
//3.1定义一个子类Student,让该子类同时实现以上两个接口。
class Student implements SchoolA,SchoolB{
//3.2 在子类中覆写接口SchoolA中的抽象方法。
public void printA(){
System.out.println( "我就读于学校A");
}
//3.3 在子类中覆写接口SchoolB中的抽象方法。
public void printB(){
System.out.println("我就读于学校B");
}
}
//4.1 定义主类
public class Test0003{
public static void main(String[] args){
//4.2 实例化子类Student得到接口SchoolA的实例化对象
SchoolA a = new Student();
a.printA();
//4.3 接口也可以不通过子类实例化对象,接口间的互相转换。
SchoolB b = (SchoolB) a;
b.printB();
}
}
运行结果如下:
(二)接口的使用限制
-
接口中定义的方法默认为public(public可省略)。所以注意在子类覆写接口中方法的时候,使用的权限一定不能比public小,一般使用public。
-
接口中定义的变量,实际就是全局常量,默认为public static final(可省略)。
-
子类可以实现多个接口,子类的实例化对象可以向上转型成任意接口类型。
-
子类可以继承抽象类(类),先继承抽象类再实现接口(先extends再implements)。
-
一个抽象类可以使用implements实现多个接口,
抽象类可以选择实现接口的抽象方法,也可以不实现,交给该抽象类的子类去实现。 -
接口不能继承抽象类(类),但一个接口可以使用extends实现多个接口继承。
(三)接口的应用
- 现在要求描述一个概念-电脑上可以使用任何的 USB 设备(U盘,打印机等等):
分析题意:
电脑上有USB接口,USB接口可以插入相应的设备。具体代码实现如下:
//定义接口
interface Usb{
//安装驱动-设备厂商提供
void setup();
//工作
void work();
}
//使用usb接口
class Computer{
public void plugin(Usb usb){
usb.setup();
usb.work();
}
}
//实现接口
class UDisk implements Usb{
public void setup(){
System.out.println("安装usb驱动");
}
public void work(){
System.out.println("usb工作");
}
}
class KeyDisk implements Usb{
public void setup(){
System.out.println("连接键盘");
}
public void work(){
System.out.println("键盘开始工作");
}
}
//定义测试类
public class InterfaceApplication{
public static void main(String[] args){
Computer computer = new Computer();
Usb udisk = new UDisk();
computer.plugin(udisk);
Usb keydisk = new KeyDisk();
computer.plugin(keydisk);
}
}
运行结果如下:
- 停车场停车实例
停车场里要停放车辆,停放车辆之前要判断能否停进标准车位;
不同类型的车停进停车场之前需要给出车辆的长和宽;
摩托车,大卡车…是对车辆的具体实现,提供具体车辆的长和宽。
具体代码如下:
//定义车位接口
interface Vehicle{
int length();
int width();
}
//判断一辆车是否可以停进停车场
class Parking{
private int length=4;
private int width=2;
public Parking(){
}
public Parking(int length,int width){
this.length =length;
this.width = width;
}
//具体判断方法
public void place(Vehicle vehicle){
if(vehicle.length()<=this.length && vehicle.width()<=this.width){
System.out.println(vehicle+"可以停进停车场");
}else{
System.out.println(vehicle+"不可以停进停车场");
}
}
}
//不同类型的车辆具体实现接口
class Bus implements Vehicle{
private int length=4;
private int width=2;
public Bus(){
}
public Bus(int length,int width){
this.length = length;
this.width = width;
}
public int length(){
return this.length;
}
public int width(){
return this.width;
}
//对象默认调用toString()方法,toString()方法默认为对象的地址,为了得到自己想要的结果,可以重写toString()方法
public String toString(){
return "Bus";
}
}
class Truck implements Vehicle{
private int length=5;
private int width=2;
public Truck(){
}
public Truck(int length,int width){
this.length = length;
this.width = width;
}
public int length(){
return this.length;
}
public int width(){
return this.width;
}
public String toString(){
return "Truck";
}
}
class Motorbike implements Vehicle{
private int length=2;
private int width=1;
public Motorbike(){
}
public Motorbike(int length,int width){
this.length = length;
this.width = width;
}
public int length(){
return this.length;
}
public int width(){
return this.width;
}
public String toString(){
return "Motorbike";
}
}
class Car implements Vehicle{
private int length=3;
private int width=2;
public Car(){
}
public Car(int length,int width){
this.length = length;
this.width = width;
}
public int length(){
return this.length;
}
public int width(){
return this.width;
}
public String toString(){
return "Car";
}
}
//定义测试类
public class Test07{
public static void main(String[] args){
Parking parking = new Parking();
//方法1:分别使用对象调用方法
//适用于车辆类型不多的情况
//parking.place(new Truck());
//parking.place(new Bus());
//parking.place(new Car());
//parking.place(new Motorbike());
//方法2:利用对象数组
//当去要判断的车辆类型比较多的时候,使用对象数组比较方便
Vehicle[] vehicles = new Vehicle[]{
new Truck(4,4),new Bus(4,2),new Car(),new Motorbike()
};
for(int i = 0;i < vehicles.length;i++){
parking.place(vehicles[i]);
}
}
}
运行结果如下: