Coding/Java 삽질기2011. 8. 24. 23:58

자바는 Static block -> Instance block -> 생성자 순으로 초기화가 된다.

먼저 아래와 같은 코드를 실행해 보자.

public class TestClass {
    public static void main (String[] args) {
       System.out.println("---------------------------");
       
       Parent father = new Parent("father");
       
       System.out.println("---------------------------");
    }
}

class Parent {
    public String myName = "Parent";

    static {
        System.out.println ("This is first message of Parent class!");
    }

    public Parent() {
       System.out.println (myName + ": This is third message! I'm a default constructor of Parent class!");
    }

    public Parent(String name) {
        myName = name;
        System.out.println (myName + ": This is third message! I'm a constructor of Parent class!");
    }
    
    {
      System.out.println (myName + ": This is second message!");
    }
}


그럼 하기와 같은 결과를 얻을 수 있다.

---------------------------
This is first message of Parent class!
Parent: This is second message!
father: This is third message! I'm a constructor of Parent class!
---------------------------



 public class TestClass {

    public static void main (String[] args) {
       System.out.println("---------------------------");
       
       Parent father = new Parent("father");
       Parent mother = new Parent("mother");
       
       System.out.println("---------------------------");
    }
}

class Parent {
    public String myName = "Parent";

    static {
        System.out.println ("This is first message of Parent class!");
    }

    public Parent() {
       System.out.println (myName + ": This is third message! I'm a default constructor of Parent class!");
    }

    public Parent(String name) {
        myName = name;
        System.out.println (myName + ": This is third message! I'm a constructor of Parent class!");
    }
    
    {
      System.out.println (myName + ": This is second message!");
    }
}


 위 와 같이 한 줄을 추가하여 실행하면 아래와 같은 결과를 얻을 수 있다.

---------------------------
This is first message of Parent class!
Parent: This is second message!
father: This is third message! I'm a constructor of Parent class!
Parent: This is second message!
mother: This is third message! I'm a constructor of Parent class!
---------------------------


여기서 보면 static { }  으로 둘러 싸인 구문은 객체 생성시 최초 1회만 수행이 되고, 그 뒤에는 수행이 되지 않는 걸 알 수 있다. 그러나 { } 로 둘러싸인 instance block은 객체가 생성 될 때마다 실행 되며 생성자 보다 먼저 생성되는 걸 알 수 있다.

class Child extends Parent {
    public String myName = "Child";

    static {
        System.out.println ("This is first message of Child class!");
    }

    public Child(String name) {
        myName = name;
        System.out.println (myName + ": This is third message! I'm a constructor of Child class!");
    }
    
    {
      System.out.println (this.myName + ": This is second message of Child class!");
    }
  
}



위 와 같이 Parent class를 상속받는 Child 클래스를 만들고, 아래와 같이 객체를 생성해 보자

public class TestClass {
    public static void main (String[] args) {
       System.out.println("---------------------------");
       
         Child son     = new Child("son");
       
       System.out.println("---------------------------");
    }



그럼 하기와 같은 결과가 나온다.

---------------------------
This is first message of Parent class!
This is first message of Child class!
Parent: This is second message!
Parent: This is third message! I'm a default constructor of Parent class!
Child: This is second message of Child class!
son: This is third message! I'm a constructor of Child class!
---------------------------


위 결과를 보면 알 수 있듯이,
부모 클래스의 static block -> 자식 클래스의 static block -> 부모 클래스의 instance block
-> 부모 클래스의 생성자 -> 자식 클래스의 instance block -> 자식클래스의 생성자 순으로 실행 되는 것을 알 수 있다.

마지막으로 아래와 같이 실행을 하면,

public class TestClass {
    public static void main (String[] args) {
       System.out.println("---------------------------");
       
       Parent father = new Parent("father");
      Parent mother = new Parent("mother");
       Child son     = new Child("son");
       
       System.out.println("---------------------------");
    }
}



하기와 같이 실행 된다. 천천히 살펴보면 앞에서 설명한 내용에서 벗어나지 않는다.

---------------------------
This is first message of Parent class!
Parent: This is second message!
father: This is third message! I'm a constructor of Parent class!
Parent: This is second message!
mother: This is third message! I'm a constructor of Parent class!
This is first message of Child class!
Parent: This is second message!
Parent: This is third message! I'm a default constructor of Parent class!
Child: This is second message of Child class!
son: This is third message! I'm a constructor of Child class!
---------------------------





 

'Coding > Java 삽질기' 카테고리의 다른 글

Java 실행 시간 측정 하기  (0) 2011.09.01
[Android] Image match game  (0) 2011.06.04
Android Tetris  (0) 2011.02.02
Posted by chobocho
Coding/Python 삽질기2011. 6. 16. 01:17
#-*- coding: cp949 -*-
#
# undo/redo 구현 방법
# 1) undo stack를 만든다.
# 2) redo stack를 만든다.
# 3) 새로운 액션을 undo에 넣는다.
# 4) 사용자가 undo를 선택하면 redo.append ( undo.pop() ) 를 수행한다.
# 5) 사용자가 redo를 선택하면 undo.append ( redo.pop() ) 를 수행한다.

undo = []
redo = []

undo.append (1)
undo.append (2)
undo.append (3)
redo.append ( undo.pop() )


print undo
print redo
Posted by chobocho
Coding/Java 삽질기2011. 6. 4. 19:56


즐거운 연휴~ 심심해서(?) 만들어 본 간단한 안드로이드용 퍼즐 게임



게임 방법

1. 빈 곳을 터치하면 상하좌우에서 동일한 이미지가 2개 있으면 삭제한다.
2. 빈 곳을 터치했으나 삭제할 수 있는 이미지가 없으면 타이머가 준다.
 
 
 

'Coding > Java 삽질기' 카테고리의 다른 글

Java에서 객체의 초기화 순서  (0) 2011.08.24
Android Tetris  (0) 2011.02.02
Bubble sort  (0) 2010.07.11
Posted by chobocho
Coding/CPP 삽질기2011. 4. 4. 17:37


static int box[1000];

static int box[1000] = {0, };

static int box[1000] = {1, };


의 차이는?

 
static int box[1000] = {1, }; 의 경우

컴파일시 바이너리의 크기가 sizeof(int) * 1000 만큼 커진다. 

'Coding > CPP 삽질기' 카테고리의 다른 글

Unity3D (1)  (0) 2011.09.08
간단한(?) 퀴즈  (2) 2011.02.06
[CPP] 파일 분할 프로그램 v0.01  (0) 2010.01.28
Posted by chobocho
Coding/Tip2011. 3. 1. 19:22

3시 15분 0초일 때 두 바늘이 만드는 작은 각은 7.5 도 이다.
 
시침은 1분에 0.5도 씩 움직인다. 

( 시침은 12시간에 한 바퀴를 돈다. 그러므로 한 시간에는 360 / 12 = 30 도를 돌고, 1분에는 30/60 = 0.5 도 이다.)
 
분침은 1분에 6도씩 움직인다.
( 분 침은 한시간에 1바퀴를 돌기 때문에 360/60 = 6 도 이다.)
 
3시 15분인 경우, 분침은 15분동안 움직였으므로 90도를 움직였고,
 
시침은 3에 위치 했으니, 30 x 3 + 0.5 x 15 = 97.5 도 이다.
 
그러므로 시침과 분침이 이루는 작은 각은 97.5 - 90 = 7.5 도 이다.

'Coding > Tip' 카테고리의 다른 글

SVN Local 저장소 사용법 간단 정리  (0) 2011.09.03
[TIP] Private IP  (0) 2011.01.19
보고있는 사이트...  (0) 2005.08.21
Posted by chobocho
Coding/CPP 삽질기2011. 2. 6. 01:09
1. Thread와 Process의 차이는?

2. Compiler와 Interpreter의 차이는?

3. C로 binary tree를 짜보시오.

4. C에서 Stack과 Heap의 구조에 대해서 설명하시오.

5. 자바에서 Garbage collection에 대해서 설명 하시오.

6. BNF란?

7. C로 Quick sort를 구현해 보시오.

8. Quick sort의 Big(O)는 얼마인가?

9. Unix에서 IPC의 방법을 설명하시오.

  


'Coding > CPP 삽질기' 카테고리의 다른 글

static int box[1000] = {1, };  (0) 2011.04.04
[CPP] 파일 분할 프로그램 v0.01  (0) 2010.01.28
[CPP] Lotto 생성기  (0) 2010.01.22
Posted by chobocho
Coding/Java 삽질기2011. 2. 2. 02:41


Galaxy Tab 전용으로 만든 테트리스.

아직 갈 길이 멀다.
 

'Coding > Java 삽질기' 카테고리의 다른 글

[Android] Image match game  (0) 2011.06.04
Bubble sort  (0) 2010.07.11
Selection sort  (0) 2010.07.11
Posted by chobocho
Coding/Tip2011. 1. 19. 23:55


Private IP

·class A : 10.0.0.0 - 10.255.255.255
·class B : 172.16.0.0 - 172.31.255.255
·class C : 192.168.0.0 - 192.168.255.255

'Coding > Tip' 카테고리의 다른 글

3시 15분 0초의 각도  (0) 2011.03.01
보고있는 사이트...  (0) 2005.08.21
빈칸을 체크해서 포커스를 맞춰주는 스크립트  (0) 2005.06.08
Posted by chobocho
Coding/Script2011. 1. 19. 23:50
<?
        echo "Your IP is [ ".$_SERVER['REMOTE_ADDR']." ]";
?>

'Coding > Script' 카테고리의 다른 글

[Excel VBA] File open / 파일열기  (0) 2016.12.20
BASIC의 추억  (0) 2007.04.04
홈페이지에 명언을 뿌려주는 코드  (6) 2006.09.18
Posted by chobocho
Coding/QR Code2011. 1. 12. 23:15

* QR code encoder 소스를 구할 수 있는 사이트
  ActionScript3 / Java / JavaScript / PHP 로 된 소스를 볼 수 있다.
  
   http://www.d-project.com/qrcode/index.html


* QR Code의 에러 정정 코드에 관한 설명이 나온 사이트 (일본어)
  
   http://www.swetake.com/qr/


* Galois fields 에 대한 참고 문서

   http://designtheory.org/library/encyc/topics/gf.pdf


* Reed-Solomon code 대해 참고 할 만한 문서
  
  http://web.eecs.utk.edu/~plank/plank/papers/CS-96-332.pdf


이전에 만들었던 QR code 소스 코드를  다시 만들면서, 이해가 가지 않는 이산수학책을 부여 잡고 국영수의 중요성에 대해서 다시 한 번 깨닫고 있다.  

'Coding > QR Code' 카테고리의 다른 글

QR Code 발표자료  (0) 2005.08.20
QR_CODE에 관하여 1  (0) 2005.08.18
갈로아 필드에서 a 구하기  (0) 2005.08.16
Posted by chobocho