singleton pattern

package com.potevio.zjx.study;

/**
 * 单例模式
 * @author Xian-JZ
 *
 */
public class Car {

	public static Car car = new Car();
	
	private Car(){}
	
	public static Car getIntance(){
		return car;
	}
	
	public void run(){
		System.out.println("This car is runing...");
	}
}
 
package com.potevio.zjx.study;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Car car = Car.getIntance();
		Car car2 = Car.getIntance();
		
		if(car == car2)
			System.out.println("They are the same car.");
	}

}
 

你可能感兴趣的:(demo)