1、接口由全局常量和公共的抽象方法所组成。在JAVA编程语言中是一个抽象类型,是抽象方法的集合。
定义接口:
interface A1{//定义了接口 public static final String MSG = "Hello";//全局常量 public abstract void print();//抽象方法 }
●接口必须要有子类,但是此时一个子类可以使用implements关键字实现多个接口。没有单继承的强制性要求。
●接口的子类(如果不是抽象类),必须要覆写接口中的全部抽象方法;。
●接口的对象可以利用子类对象的向上转型进行实例化操作。。
实现接口:
interface A1{//定义了接口 public static final String MSG = "Hello";//全局常量 public abstract void print();//抽象方法 } interface B1{ public abstract void get(); } class Y implements A1,B1{ public void print(){ System.out.println("A1接口的抽象方法"); } public void get(){ System.out.println("B1接口的抽象方法"); } } public class InterfaceDemo1 { public static void main(String[] args) { Y y = new Y(); A1 a = y; //向上转型 B1 b = y; //向上转型 a.print(); b.get(); } }
运行结果:
A1接口的抽象方法
B1接口的抽象方法
2、既要继承抽象类,又要实现接口:
interface A1{//定义了接口 public static final String MSG = "Hello";//全局常量 public abstract void print();//抽象方法 } interface B1{ public abstract void get(); } abstract class C1{ public abstract void say(); } class Y extends C1 implements A1,B1{ // public void print(){ System.out.println("A1接口的抽象方法"); } public void get(){ System.out.println("B1接口的抽象方法"); } public void say(){ System.out.println("C1抽象类的方法"); } } public class InterfaceDemo1 { public static void main(String[] args) { Y y = new Y(); A1 a = y; //向上转型 B1 b = y; //向上转型 C1 c = y; a.print(); b.get(); c.say(); } }
运行结果:
A1接口的抽象方法
B1接口的抽象方法
C1抽象类的方法
3、在接口中如果不写public,则也是public访问权限。
允许一个抽象类实现多个接口。
interface A2{ public void funA(); } interface B2{ public void funB(); } //C2接口同时继承了A2和B2两个接口 interface C2 extends A2,B2{ public void funC(); } class X2 implements C2{ public void funA(){} public void funB(){} public void funC(){} }
4、接口的应用——标准
interface USB{ public abstract void start(); public abstract void stop(); } class Computer { public static void plugin(USB usb){ usb.start(); usb.stop(); } } class Flash implements USB{ public void start(){ System.out.println("U盘开始工作"); } public void stop(){ System.out.println("U盘停止工作"); } } class Print implements USB{ public void start(){ System.out.println("打印机开始工作"); } public void stop(){ System.out.println("打印机停止工作"); } } public class InterfaceDemo3 { public static void main(String[] args) { Computer.plugin(new Flash()); Computer.plugin(new Print()); } }
运行结果:
U盘开始工作
U盘停止工作
打印机开始工作
打印机停止工作
5、接口的应用——工厂设计模式
interface Fruit{ public void eat(); } class Apple implements Fruit{ public void eat(){ System.out.println("***吃苹果"); } } class Orange implements Fruit{ public void eat(){ System.out.println("***吃橘子"); } } class Factory{ public static Fruit getInstance(String furitname){ if ("apple".equals(furitname)){ return new Apple(); }else if ("orange".equals(furitname)){ return new Orange(); }else { return null; } } } public class InterfaceDemo4 { public static void main(String[] args) { Fruit fruit = Factory.getInstance("apple"); fruit.eat(); } }
运行结果:
***吃苹果
6、设计模式——代理设计
interface Network{ public void browse(); } class Real implements Network{ public void browse(){ System.out.println("正在上网"); } } class Proxy implements Network{ private Network network; public Proxy(Network network){ this.network = network; } public void check(){ System.out.println("检查用户是否合法"); } public void browse(){ this.check(); this.network.browse(); } } public class InterfaceDemoo5 { public static void main(String[] args) { Network network = new Proxy(new Real()); network.browse(); } } 运行结果: 检查用户是否合法 正在上网