String s="hoho"


s.subString(0,2).subString(0,1)


객체에 . 을  찍어서 계속 계속 메소드를 호출 하는 것을 본적이 있습니다.


이것이 바로 자기 부르기!!


return this; 라는 생소한 문법을 볼 수 있습니다. 


public class test {
public static void main(String args[]){
self s=new self();
s.addOne().addOne().addOne().addOne().print();
}

}
class self{
int num=0;
self addOne(){
num+=1;
return this;
}
void print(){
System.out.println(num);
}
}


틀린점이나 보충할 사항은 항상 수정하겠습니다.


싱글턴 패턴은 언제 사용하는 것일까???

오직 하나의 클래스 인스턴스만 공유되어야 할 때 사용했었다.



static 으로 유일한 변수, 메소드를 공유할 수 있다.


class의 모든 것을 하나만 공유하는 방법은 없을까??  이것이 바로 singleTon이다.

public class test {

    public static void main(String args[]){
Sing s=Sing.getInstance();
System.out.println(s.num);
s.num+=5;
Sing s2=Sing.getInstance();
System.out.println(s2.num);
}

}
class Sing{
int num=99;
private static Sing s;
static Sing getInstance(){
if(s==null)
s=new Sing();
return s;
}
}


+ Recent posts