어떻게 대상의 유일성을 보증합니까

1934 단어 대상
/*
          :
   1.        new       。
   2.           。
   3.                       。
*/
public class Test{
    public static void main(String[] args){
       
	   //Subject sub = Subject.oSub;//       ,       
	   Subject sub = Subject.getInstance();//            ,             ,        !
	   new Student().show(sub);
	   new Teacher().show(sub);
   }
}

class Subject{
    public  String sub;
	public static Subject oSub = new Subject();
	private Subject(){
	    sub = "        ";
	}
	
	public static Subject getInstance(){
	    //oSub = new Subject();
		//if(****)    if               
		return oSub;
	}
	
	public String getSubject(){
		return sub;
	}
	
	public void setSubjcet(String name){
	    sub = name;
	}
}

/*
                         :
         :
                         !(    ,          ,          ?    ?
           ,     !            ,               )             ,     
                          ,                  !
 class Subject{
    public  String sub;
	//public Subject ss = new Subject();
	public static Subject oSub = new Subject();//   
        //public static Subject oSub = null;//   
	private Subject(){
            //oSub = new Subject();//oSub     ,         oSub         !
	    sub = "        ";
	}
	
	public static Subject getSubject(){        
		return oSub;
                //return oSub = new Subject();
	}
}
*/

class Student{
    public void show(Subject sub){
	    System.out.println(sub.sub);
	}
}

class Teacher{
    public void show(Subject sub){
	    System.out.println(sub.sub);
	}
}

좋은 웹페이지 즐겨찾기