zoukankan      html  css  js  c++  java
  • 代理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();
    	}
    }
    

  • 相关阅读:
    Groovy 设计模式 -- null对象模式
    Groovy 设计模式 -- 借贷
    Groovy 设计模式 -- 抽象工厂 模式
    Groovy 设计模式 -- Strategy 模式
    Groovy 设计模式 -- proxy & delegate
    Groovy 类名称赋值为变量使用(newInstance & new)
    yaml
    REST POST PUT差别
    Continuous Design
    通配符 Globbing赏析
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/6791642.html
Copyright © 2011-2022 走看看