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", "行百里者半九十"); } }