• 代理模式


    public interface Italk {
        public void talk(String msg);
    }
    
    
    
    
    public class Person implements Italk {
    
        public String username;
        public String age;
    
        public String getName() {
            return username;
        }
    
        public void setName(String name) {
            this.username = name;
        }
    
        public String getAge() {
            return age;
        }
    
        public void setAge(String age) {
            this.age = age;
        }
    
        public Person(String name1, String age1) {
            this.username = name1;
            this.age = age1;
        }
    
        public void talk(String msg) {
            System.out.println(msg + "!你好,我是" + username + ",我年龄是" + age);
        }
    
    }
    
    
    public class TalkProxy implements Italk {
    
        private Italk talker;
    
        public TalkProxy(Italk talker) {
            // super();
            this.talker = talker;
        }
    
        public void talk(String msg) {
            talker.talk(msg);
        }
    
        public void talk(String msg, String singname) {
            talker.talk(msg);
            sing(singname);
        }
    
        private void sing(String singname) {
            System.out.println("唱歌:" + singname);
        }
    }
    
    
    
    public class MyProxyTest {
    
        public static void main(String[] args) {
            // 不需要执行额外方法的
            Italk person = new Person("王云浩", "20");
            person.talk("No ProXY Test");
            System.out.println("lxit-----------------------------");
            // 需要执行额外方法的
            TalkProxy talker = new TalkProxy(person);
            talker.talk("ProXY Test", "行百里者半九十");
        }
    }
  • 相关阅读:
    电力三维基础信息平台
    基于三维GIS技术的输电线路地理信息系统的设计与实现
    linux学习笔记(1)
    linux学习笔记(4)
    linux学习笔记(8)
    linux学习笔记(6)
    linux学习笔记(3)
    linux 学习笔记(2)
    linux学习笔记(7)
    ASCII码表完整版
  • 原文地址:https://www.cnblogs.com/Akishimo/p/2964582.html
Copyright © 2020-2023  润新知