本文主要讲述java的接口,以其相关细节。
老韩接口的介绍:
老韩接口的细节:
老韩继承类vs接口实现
老韩接口的多态性
1)接口的多态性示意图,如下图所示:
示例代码如下:
USB接口:
1 interface USBInterface { 2 public void start(); 3 public void end(); 4 }
Phone类:
1 public class Phone implements USBInterface{ 2 @Override 3 public void start() { 4 System.out.println("phone开始工作"); 5 } 6 7 @Override 8 public void end() { 9 System.out.println("phone停止工作"); 10 } 11 }
Camera类:
1 public class Camera implements USBInterface { 2 @Override 3 public void start() { 4 System.out.println("camera开始工作"); 5 } 6 7 @Override 8 public void end() { 9 System.out.println("camera停止工作"); 10 } 11 }
Computer类:
1 public class Computer { 2 public void work(USBInterface usbInterface){ 3 usbInterface.start(); 4 usbInterface.end(); 5 } 6 }
Test类:
1 public class InterfaceTest { 2 public static void main(String[] args) { 3 Computer computer = new Computer(); 4 Camera camera = new Camera(); 5 Phone phone = new Phone(); 6 7 // 体现了接口的多态性 8 computer.work(camera); 9 computer.work(phone); 10 } 11 }
运行结果如下:
标签:java,void,System,接口,Computer,Override,public From: https://www.cnblogs.com/zwgitOne123/p/16995097.html