java的多构造函数的处理方式

/**
 * 
 */
package P;

import java.awt.List;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction;

import S.Smty;

/**
 * @author Administrator
 *
 */
public class Q {

	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Person p =new Person.Builder("mike").setAge(14).build();
		
		System.out.println(p.getName());
		System.out.println(p.getAge());
	}
	
	
}


class Person {
	private String name;
	private int age;
	
 	public static class Builder{
 		private String name=null;
 		private int age=0;
 		
 		public Builder(String name){
 			this.name=name;
 			
 		}
 		
 		public Builder setAge(int age) {
 			this.age=age;
 			return this;
 		}
 		
 		public Person build() {
 			return new Person(this);
 		}
 	}
 	
 	public Person(Builder builder) {
 		this.name=builder.name;
 		this.age=builder.age;
 	}
 	
 	public String getName() {
		return name;
	}
 	public int getAge() {
		return age;
	}
}

  

posted @ 2017-12-28 14:59  layfork  阅读(1074)  评论(0编辑  收藏  举报