• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

cynchanpin

  • 博客园
  • 联系
  • 订阅
  • 管理

View Post

代理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  阅读(182)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3