一个Java的接口实例
package interface_demo; public class Demo4 { public static void main(String[] args) { Computer c = new Computer(); Mouse m = new Mouse(); c.run(m); } } package interface_demo; public class Mouse implements USB { @Override public void run() { System.out.println("这是一个鼠标"); } } package interface_demo; public class Computer { // @Override public void open() { System.out.println("开机"); } public void close() { System.out.println("关机"); } public void run(USB u) { u.run(); } } package interface_demo; public interface USB { void run(); }
此部分代码来自黑马程序员的B站视频。