代理Delegation
package com.ctl.test; class Person { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Person(int id, String name) { super(); this.id = id; this.name = name; } public Person() { super(); } public void say(String name) { System.out.println("my name is" + name); } public void run() { System.out.println("-----run start-----"); for (int i = 0; i < 5; i++) { try { Thread.sleep(200); System.out.println("i=" + i); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("-----run start-----"); } public void study() { System.out.println("study"); } public void work() { System.out.println("work"); } } class PersonDelegation { private String name; public PersonDelegation(String name) { super(); this.name = name; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } private Person person = new Person(1, name); public String getName() { return name; } public void setName(String name) { this.name = name; } public void work() { person.work(); } public void run(){ person.run(); } } public class PersonDelegationTest { public static void main(String[] args) { PersonDelegation per = new PersonDelegation("lin"); per.work(); per.run(); } }
posted on 2017-05-01 12:18 cynchanpin 阅读(178) 评论(0) 编辑 收藏 举报