特别的抽象:接口

接口

  • 接口是一种引用数据类型,编译之后,生成.class字节码文件

  • 接口是完全抽象类【抽象类是半抽象类,抽象类当中可以有非抽象方法,接口中都是抽象方法】

  • 接口的语法

    • [修饰符列表] interface 接口名{}
    • 接口支持继承,接口支持多继承 interface C extends A,B{}
    • 接口内容,接口只包括两部分内容,一部分是常量,一部分是抽象方法。没有其他内容!!!——见例一
      • 接口当中的方法都是抽象方法,所以接口中不能有方法体,不能有{}!!
      • 接口中所有的元素都是public修饰的
      • 接口当中都是抽象方法,那么在编写代码的时候,public abstract是可以省略的!!
      • 接口中的常量:public static final可以省略!!
  • 类和类之间是继承关系,类和接口之间是实现关系。继承:extends;实现:implements——见例二

    • 与抽象类被非抽象类继承一样,一个非抽象的类实现接口的时候,需要重写接口当中所有的抽象方法!!!
    • 非抽象类实现接口时,可以同时实现多个接口,弥补了java中的单继承带来的缺陷。非抽象类主机,接口鼠标、接口显示器、接口键盘…,主机需要重写所有接口的所有抽象方法。
  • 接口和接口之间,进行强制转换的时候,没有继承关系,也可以强制转换【多态要求必须有继承关系】,但是运行的时候,可能会有ClassCastException异常,进行instanceof运算符判断 ——见例三
    -继承和实现都存在的情况: extends 在前;implements在后 ——见例四

例题一

package day2.interfacetest;

public class InterfaceTest01 {
	public static void main(String[] args) {
		System.out.println(MyMath.pi);
	}
}

//定义接口
interface A{
	
}

interface MyMath{
	//抽象方法
	//public abstract int sum(int a,int b);
	int sum(int a,int b); 	//接口当中都是抽象方法,那么在编写代码的时候,public abstract是可以省略的!!!
	//常量
	//public static final double pi =3.1415926;
	double pi =3.1415926;		//接口中的public static final可以省略
}

例题二

package day2.interfacetest;

/**
 * @author 衣鱼
 *	类和类之间叫继承,类和接口之间叫做实现
 *		继承:extends
 *		实现:implements
 */
public class IterfaceTest02 {
	public static void main(String[] args) {
		//使用多态
		//接口不能new对象,接口是抽象的,不能实例化
		MyMath m = new NotAbstract();
		m.sum(2, 5);
	}
}

//特殊的抽象类:接口
interface MyMath{
	double PI = 3.1415926;
	int sum(int a,int b);
}
//非抽象类  实现接口
//非抽象类实现抽象接口:抽象方法需要重写
class NotAbstract implements MyMath{
	//实现接口中的方法
	public int sum(int a,int b) {
		System.out.println(a+b);
		return a+b;
	}
}

例题三

package day2.interfacetest;
/**
 * @author 衣鱼
 * 	类:无论是向上转型,还是向下转型,两种类型之间必须有继承关系,没有继承关系,编译器报错【不适用接口,接口编程成功,运行报错】
 *	向下转型的时候,接口和类一样,进行instanceof运算符判断
 */
public class InterfaceTest03 {
	public static void main(String[] args) {
		M m = new Q();
		if(m instanceof K) {
			K k = (K)m;
		}
		//K k = (K)m;		直接运行报错
		
		//多实现测试
		X x = new P();
		x.m1();  //想让x调用m2方法呢?
		Y y =(Y)x;	//接口转型
		y.m2();
	}
}

interface K{}
interface M{}
class Q implements M{}

//-----------------------------------------------------------------
//接口支持多继承
//interface X{}
//interface Y{}
//interface Z extends X,Y{}

//------------------------------------------------------------------
//接口的多实现
interface X{
	void m1();
}
interface Y{
	void m2();
}
interface Z{
	void m3();
}
class P implements X,Y,Z{
	public void m1() {
		System.out.println("m1执行!");
	}
	public void m2() {
		System.out.println("m2执行!");
	}
	public void m3() {}
}

例题四

package day2.interfacetest;

/**
 * @author 衣鱼
 *	继承和实现都存在的情况
 *	extends 在前;implements在后
 */
public class InterfaceTest04 {
	public static void main(String[] args) {
		//多态
		Animal a = new Cat();
		//a.fly();	//编译报错
		Flyable f = new Cat();		//父接口  子类
		f.fly();
	}
}

//父类:动物
class Animal{
	
}
//接口:飞翔
interface Flyable{
	void fly();
}
//子类 猫,继承父类,通过飞翔接口,可以飞翔
class Cat extends Animal implements Flyable{
	public void fly() {
		System.out.println("飞猫");
	}
}

接口在开发中的作用

  • 接口在开发中的作用,类似于多态在开发中的作用。多态:面向抽象编程,不面向具体编程,降低程序的耦合度,提高程序的扩展力。【多态文章】OCP原则:对扩展开发,对修改关闭。
  • 面向抽象编程;面向接口编程。接口可插拔。
  • 接口的使用离不开多态的思想,因为接口是全抽象的,不能实例化,new 子类

类型之间的关系

  • is a ; has a ;like a
    • is a 凡是满足is a的表示“继承关系” 猫是动物
    • has a 凡是满足has a 的关系表示“关联关系”属性 她有名字
    • like a 凡是满足like a的关系表示“实现关系” 厨师像菜单一样

抽象类和接口的语法区别

  • 抽象类是半抽象的,接口是完全抽象的
  • 抽象类当中有构造方法,接口中没有构造方法
  • 接口之间支持多继承,类之间只能单继承
  • 一个类可以同时实现多个接口,一个抽象类只能继承一个类(单继承)

作业

package homework;

/**
 * @author 衣鱼
 *	设计一个笔记本电脑类,属性随意,并进行属性私有化,对外提供公开的set和get方法
 *	设计一个可插拔的接口 InsertDrawable 自行定义方法
 *	设计一个鼠标类实现InsertDrawable接口
 *	设计一个键盘类实现InsertDrawable接口
 *	设计一个显示器类实现InsertDrawable接口
 *	设计一个打印类实现InsertDrawable接口
 *
 *在电脑中有一个接口属性InsertDrawable,可以让笔记本插拔配件
 */
public class ComputerTest {
	public static void main(String[] args) {
		//测试
		 InsertDrawable a =new shubiao();
		 InsertDrawable b =new jianpan();
		 InsertDrawable c =new xianshiqi();
		 InsertDrawable d =new dayinji();
		 Computer u = new Computer(a);
		 u.insertDrawable =b;
		 u. chaba() ;	 
	}
}

class Computer{
	 InsertDrawable  insertDrawable;
	public void chaba() {
		insertDrawable.zhixing();
	}
	public  Computer( InsertDrawable insertDrawable) {
		this.insertDrawable = insertDrawable;
	}
}
interface InsertDrawable{
	void zhixing();
}
class shubiao implements InsertDrawable{
	public void zhixing() {
		System.out.println("鼠标执行");
	}
}
class jianpan implements InsertDrawable{
	public void zhixing() {
		System.out.println("键盘执行");
	}
}
class xianshiqi implements InsertDrawable{
	public void zhixing() {
		System.out.println("显示器执行");
	}
}
class dayinji implements InsertDrawable{
	public void zhixing() {
		System.out.println("打印机执行");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值