菜鸟Java大面经----------------关于单例模式(懒汉式)

关于单例模式

package com.ahuiby.demo1;

class Dog{
	//字段
	private static Dog dog=new Dog();
	
	//构造方法
	private Dog(){}
	
	//方法
	public static Dog getDog(){
		return dog;
	}
}
public class Singleton {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Dog d1=Dog.getDog();
		Dog d2=Dog.getDog();
		
		System.out.println(d1);
		System.out.println(d2);
	}

}

猜你喜欢

转载自ye-wolf.iteye.com/blog/2317053