1. 程式人生 > >java的多構造函數的處理方式

java的多構造函數的處理方式

public action java ati arraylist arp hash gre date

/**
 * 
 */
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;
	}
}

  

java的多構造函數的處理方式