java—接口及其应用,接口定义加强

本文介绍了Java接口的基本概念,强调接口在多继承中的重要性。内容包括接口的定义、使用限制,如方法默认为public,变量为全局常量。同时,通过USB设备和停车场实例说明接口的应用。在JDK1.8后,接口新增default和static方法,允许定义带有方法体的接口成员,使得接口功能更加强大。
摘要由CSDN通过智能技术生成

接口的基本概念,使用限制,应用

(一)接口的概念

  • 接口存在的意义:抽象类虽然能约定子类的实现要求,但是具有单继承缺陷,为了解决抽象类的单继承缺陷,定义了接口。
  • 在使用时,接口优先于抽象类,能使用接口就不使用抽象类。
  • 接口的定义:接口是抽象方法和全局变量的集合。
  • 接口使用 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实现多个接口继承。

(三)接口的应用

  1. 现在要求描述一个概念-电脑上可以使用任何的 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);
}
}

运行结果如下:
在这里插入图片描述

  1. 停车场停车实例
    在这里插入图片描述
    停车场里要停放车辆,停放车辆之前要判断能否停进标准车位;
    不同类型的车停进停车场之前需要给出车辆的长和宽;
    摩托车,大卡车…是对车辆的具体实现,提供具体车辆的长和宽。
    具体代码如下:

//定义车位接口
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]);
		}
	}
}

运行结果如下:
在这里插入图片描述
(四)接口定义加强

  1. 可以使用default来定义普通方法,需要通过对象调用;
  2. 可以使用static来定义静态方法,通过接口名就可以调用。
    接口中定义的方法本来不允许有方法体,但是在JDK1.8之后,增加了许多新特性,接口定义加强,可以使用default 和 static 关键字定义方法,定义的方法可以有方法体,在子类实现接口的时候不用再覆写这些方法,只需要覆写抽象方法就可以。使用这些方法的时候直接调用即可。
    举例:
package www.fanfan.com;

public interface InterfaceTest {
    public void print();

    public default void defaultFun(){
        System.out.println("这是一个default修饰的方法");
    }

    public static void staticFun(){
        System.out.println("这是一个static修饰的方法");
    }
}

public class MyInterface implements InterfaceTest{
    //在子类中只需要实现没有方法体的普通方法就可以
    @Override
    public void print() {
        System.out.println("这是接口中一个普通方法");
    }

    public static void main(String[] args) {
        MyInterface myInterface = new MyInterface();
        //接口中的抽象方法使用时使用对象调用
        myInterface.print();
        //接口中的default修饰的方法调用时使用对象调用
        myInterface.defaultFun();
        //接口中的static修饰的方法调用时使用类名.方法名 调用
        InterfaceTest.staticFun();
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值