Home >Java >javaTutorial >Detailed analysis of JAVA interfaces and abstract classes
This article brings you relevant knowledge about java, which mainly sorts out issues related to interfaces and abstract classes. When you need to derive a class from several classes, inherit all of them The properties and methods of JAVA do not have multiple inheritance and need to be implemented with interfaces. Let’s take a look at them together. I hope it will be helpful to everyone.
Recommended study: "java video tutorial"
public class UsbTest { public static void main(String[] args) { Computer com =new Computer(); Flash flash=new Flash(); com.transferData(flash); // USB usb =new Flash() com.transferData(new Printer()); /* * U盘开始工作 * 传输数据 * U盘结束工作 * 打印机开始工作 * 传输数据 * 打印机结束工作 * * */ }}class Computer{ public void transferData(USB usb){ usb.start(); System.out.println("传输数据"); usb.stop(); }}interface USB{ void start(); void stop();}class Flash implements USB{ @Override public void start() { System.out.println("U盘开始工作"); } @Override public void stop() { System.out.println("U盘结束工作"); }}class Printer implements USB{ @Override public void start() { System.out.println("打印机开始工作"); } @Override public void stop() { System.out.println("打印机结束工作"); }}
public class JDK8Test { public static void main(String[] args) { SubClass sub =new SubClass(); CompareA.methods1(); sub.methods2(); /* * Compare 北京 * Compare 上海 */ }}class SubClass implements CompareA{}interface CompareA{ public static void methods1() { System.out.println("Compare 北京"); } public default void methods2() { System.out.println("Compare 上海"); }}
public class NetWorkTest { public static void main(String[] args) { Server server=new Server(); ProxyServer proxyServer=new ProxyServer(server); // 放的是被代理对象进去 proxyServer.browse(); // 调用的是代理对象,但结果是真实对象的方法被调用 /* * 检查工作 * 真实服务器访问网络 */ }}interface NetWork{ void browse();}// 被代理类class Server implements NetWork{ @Override public void browse() { System.out.println("真实服务器访问网络"); }}// 代理类class ProxyServer implements NetWork{ private NetWork work; public ProxyServer(NetWork work){ this.work=work; } public void check(){ System.out.println("检查工作"); } @Override public void browse() { check(); work.browse(); }}
public class InnerClassTest { public static void main(String[] args) { // 实例化Dog,静态类 Pseson.Dog dog=new Pseson.Dog(); dog.eat(); // 吃骨头 // 实例化Cat,非静态类 Pseson p1 =new Pseson(); Pseson.Cat cat=p1.new Cat(); cat.eat(); // 猫吃鱼 }}class Pseson{ String name; int age; public void eat(){ System.out.println("吃饭"); } static class Dog{ String name; int age; public void eat(){ System.out.println("吃骨头"); } } class Cat{ public void eat(){ System.out.println("猫吃鱼"); } }}
public class InnerClassTest { public static void main(String[] args) { // 实例化Cat,非静态类 Pseson p1 =new Pseson(); Pseson.Cat cat=p1.new Cat(); cat.eat(); cat.display("小花"); /* * 小花 * 波斯猫 * 人 */ }}class Pseson{ String name="人"; int age; public void eat(){ System.out.println("吃饭"); } static class Dog{ String name; int age; public void eat(){ System.out.println("吃骨头"); } } class Cat{ String name="波斯猫"; public void eat(){ System.out.println("猫吃鱼"); } public void display(String name){ System.out.println(name); // 方法中name System.out.println(this.name); // 内部类中name System.out.println(Pseson.this.name); // 外部类中name } }}
Recommended learning: "java video tutorial"
The above is the detailed content of Detailed analysis of JAVA interfaces and abstract classes. For more information, please follow other related articles on the PHP Chinese website!