简单的代理,留作笔记

xiaoxiao2021-02-28  109

接口

/** * 定义了Animal接口; * 使用多态;让代理类能够代理子类 * @author Andy * */ public interface Animal { //定义了规范 void eat(); void run(); }

/**接口的代理 * 定义了Animal接口; * 使用多态;让代理类能够代理子类 * @author Andy * */ public interface Animal { //定义了规范 void eat(); void run(); }

具体的实现类

import java.util.Random; public class Cat implements Animal{ @Override public void eat() { System.out.println("cat eat...."); try { Thread.sleep(new Random().nextInt(10000)); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void run() { System.out.println("cat run...."); try { Thread.sleep(new Random().nextInt(10000)); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

测试类

public class TestProxy { public static void main(String[] args) { Animal animal = new Cat(); //多态 AnimalProxy ap = new AnimalProxy(animal); ap.eat(); } }

转载请注明原文地址: https://www.6miu.com/read-54755.html

最新回复(0)