"메소드 오버라이딩"의 두 판 사이의 차이

6번째 줄: 6번째 줄:


==실습 예제==
==실습 예제==
;소스 코드
<source lang='java'>
<source lang='java'>
class Base {
class Base {
30번째 줄: 31번째 줄:
   
   
     Base derivedInBaseVariable = new Derived();
     Base derivedInBaseVariable = new Derived();
     derivedInBaseVariable .print();
     derivedInBaseVariable.print();
   
   
     Base.staticPrint();
     Base.staticPrint();
36번째 줄: 37번째 줄:
   }
   }
}
}
</source>
;실행 결과
<source lang='text'>
Base: Base
Base: print
Base: Base
Derived: Derived
Derived: print
Base: Base
Derived: Derived
Derived: print
Base: staticPrint
Derived: staticPrint
</source>
</source>



2013년 10월 1일 (화) 09:32 판

1 개요

method overriding, override
메소드 오버라이딩; 오버라이딩, 오버라이드

2 실습 예제

소스 코드
class Base {
  Base() { System.out .println ("Base: Base"); }
  void print() { System.out .println ("Base: print"); }
  static void staticPrint() { System.out .println ("Base: staticPrint"); }
}
class Derived extends Base {
  Derived() {System.out .println ("Derived: Derived"); }
  void print() {System.out .println ("Derived: print"); }
  static void staticPrint() {System.out .println ("Derived: staticPrint"); }
}
public class Main {
  public static void main (String[] args) {
    Base base = new Base();
    base.print();
 
    Derived derived = new Derived();
    derived .print();
 
    Base derivedInBaseVariable = new Derived();
    derivedInBaseVariable.print();
 
    Base.staticPrint();
    Derived.staticPrint();
  }
}
실행 결과
Base: Base
Base: print
Base: Base
Derived: Derived
Derived: print
Base: Base
Derived: Derived
Derived: print
Base: staticPrint
Derived: staticPrint

3 같이 보기

4 참고 자료

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}