欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

singleton pattern

程序员文章站 2022-03-15 12:35:48
...
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