• 沒有找到結果。

OOPClass,April14,2009 Hsuan-TienLin Inheritance

N/A
N/A
Protected

Academic year: 2022

Share "OOPClass,April14,2009 Hsuan-TienLin Inheritance"

Copied!
9
0
0

加載中.... (立即查看全文)

全文

(1)

Hsuan-Tien Lin

Deptartment of CSIE, NTU

OOP Class, April 14, 2009

(2)

Instance Variables and Inheritance (1/3)

1 c l a s s P r o f e s s o r {

2 S t r i n g t i t l e ;

3 P r o f e s s o r ( ) { t i t l e = "NTU" ; }

4 p u b l i c S t r i n g g e t _ t i t l e ( ) { r e t u r n t i t l e ; }

5 }

6 c l a s s CSIEProfessor extends P r o f e s s o r {

7 S t r i n g t i t l e ; / / what?

8 CSIEProfessor ( ) { t i t l e = " CSIE " ; / / which t i t l e ? }

9 p u b l i c S t r i n g g e t _ c s i e _ t i t l e ( ) { r e t u r n t i t l e ; / / which t i t l e ? }

10 }

11

12 CSIEProfessor HTLin1 = new CSIEProfessor ( ) ;

13 System . o u t . p r i n t l n ( HTLin1 . g e t _ t i t l e ( ) ) ; / / NTU

14 System . o u t . p r i n t l n ( HTLin1 . g e t _ c s i e _ t i t l e ( ) ) ; / / CSIE

15 P r o f e s s o r HTLin2 = new CSIEProfessor ( ) ;

16 System . o u t . p r i n t l n ( HTLin2 . g e t _ t i t l e ( ) ) ; / / NTU

17 System . o u t . p r i n t l n ( HTLin2 . g e t _ c s i e _ t i t l e ( ) ) ; / / hahaha

18 P r o f e s s o r HTLin3 = new P r o f e s s o r ( ) ;

19 System . o u t . p r i n t l n ( HTLin3 . g e t _ t i t l e ( ) ) ; / / NTU

20 System . o u t . p r i n t l n ( HTLin3 . g e t _ c s i e _ t i t l e ( ) ) ; / / hahaha

(3)

What TYPE-of-reference wants to access its title?

1 c l a s s P r o f e s s o r {

2 S t r i n g t i t l e ; / ∗ P r o f e s s o r ( t h i s ) ∗ /

3 P r o f e s s o r ( ) { t i t l e = "NTU" ; } / ∗ P r o f e s s o r ( t h i s ) ∗ /

4 p u b l i c S t r i n g g e t _ t i t l e ( ) { r e t u r n t i t l e ; }

5 / ∗ P r o f e s s o r ( t h i s ) ∗ /

6 }

7 c l a s s CSIEProfessor extends P r o f e s s o r {

8 S t r i n g t i t l e ; / ∗ CSIEProfessor ( t h i s ) ∗ /

9 CSIEProfessor ( ) { t i t l e = " CSIE " ; } / ∗ CSIEProfessor ( t h i s ) ∗ /

10 p u b l i c S t r i n g g e t _ c s i e _ t i t l e ( ) { r e t u r n t i t l e ; }

11 / ∗ CSIEProfessor ( t h i s ) ∗ /

12 p u b l i c S t r i n g g e t _ s u p e r _ t i t l e ( ) { r e t u r n super. t i t l e ; }

13 / ∗ P r o f e s s o r ( super ) ∗ /

14 p u b l i c S t r i n g g e t _ s u p e r _ t i t l e _ 2 ( ) {

15 r e t u r n ( ( P r o f e s s o r )t h i s) . t i t l e ;

16 }

17 / ∗ P r o f e s s o r ( t h i s as P r o f e s s o r ) ∗ /

18 }

(4)

Instance Variables and Inheritance (3/3)

What TYPE-of-reference wants to access its title?

1 CSIEProfessor HTLin1 = new CSIEProfessor ( ) ;

2 System . o u t . p r i n t l n ( HTLin1 . t i t l e ) ; / ∗ CSIEProfessor ( HTLin1 ) ∗ /

3 P r o f e s s o r HTLin2 = new CSIEProfessor ( ) ;

4 System . o u t . p r i n t l n ( HTLin2 . t i t l e ) ; / ∗ P r o f e s s o r ( HTLin2 ) ∗ /

5 P r o f e s s o r HTLin3 = new P r o f e s s o r ( ) ;

6 System . o u t . p r i n t l n ( HTLin3 . t i t l e ) ; / ∗ P r o f e s s o r ( HTLin3 ) ∗ /

7 CSIEProfessor HTLin4 = new CSIEProfessor ( ) ;

8 System . o u t . p r i n t l n ( ( ( P r o f e s s o r ) HTLin4 ) . t i t l e ) ;

9 / ∗ P r o f e s s o r ( HTLin4 as P r o f e s s o r ) ∗ /

compile-time (static) binding: compiler easily decide intention by declared type

instance variables: static binding, new variable w/ same name

“hides” old one (when using new type)

(5)

1 c l a s s P r o f e s s o r {

2 s t a t i c i n t cou nt ;

3 P r o f e s s o r ( ) { c oun t ++; } / / i . e . , P r o f e s s o r . cou nt ++;

4 }

5 c l a s s CSIEProfessor extends P r o f e s s o r {

6 s t a t i c i n t cou nt ;

7 CSIEProfessor ( ) {

8 super( ) ; c oun t ++; / / i . e . , CSIEProfessor . c oun t ++;

9 }

10 }

11 System . o u t . p r i n t l n ( CSIEProfessor . cou nt ) ; / / c l e a r i n t e n t i o n

12 System . o u t . p r i n t l n ( P r o f e s s o r . c oun t ) ; / / c l e a r i n t e n t i o n

13 / / i n t e n t i o n determined by r e f e r e n c e TYPE

14 CSIEProfessor HTLin1 = new CSIEProfessor ( ) ;

15 System . o u t . p r i n t l n ( HTLin1 . c oun t ) ; / / CSIEProfessor . cou nt

16 P r o f e s s o r HTLin2 = new CSIEProfessor ( ) ;

17 System . o u t . p r i n t l n ( HTLin2 . c oun t ) ; / / P r o f e s s o r . cou nt

18 P r o f e s s o r HTLin3 = n u l l;

19 System . o u t . p r i n t l n ( HTLin3 . c oun t ) ; / / P r o f e s s o r . cou nt

class variables: static binding (as said before)

(6)

Class Methods and Inheritance (0/0)

class methods: exactly the same as class variables

intention determined by directly using class names, or by

reference TYPE

(7)

1 c l a s s P r o f e s s o r {

2 S t r i n g s k i l l = " t e l l j o k e s " ;

3 p u b l i c S t r i n g g e t _ s k i l l ( ) { r e t u r n s k i l l ; }

4 }

5 c l a s s J a i J a i P r o f e s s o r extends P r o f e s s o r {

6 p u b l i c S t r i n g g e t _ s k i l l ( ) { r e t u r n s k i l l + " , p l a y on BBS" ; }

7 }

8

9 J a i J a i P r o f e s s o r HTLin1 = new J a i J a i P r o f e s s o r ( ) ;

10 System . o u t . p r i n t l n ( HTLin1 . g e t _ s k i l l ( ) ) ;

11 System . o u t . p r i n t l n ( ( ( P r o f e s s o r ) HTLin1 ) . g e t _ s k i l l ( ) ) ;

12 P r o f e s s o r HTLin2 = new J a i J a i P r o f e s s o r ( ) ;

13 System . o u t . p r i n t l n ( HTLin2 . g e t _ s k i l l ( ) ) ;

14 System . o u t . p r i n t l n ( ( ( J a i J a i P r o f e s s o r ) HTLin2 ) . g e t _ s k i l l ( ) ) ;

15 P r o f e s s o r HTLin3 = new P r o f e s s o r ( ) ;

16 System . o u t . p r i n t l n ( HTLin3 . g e t _ s k i l l ( ) ) ;

17 System . o u t . p r i n t l n (

18 ( ( J a i J a i P r o f e s s o r ) HTLin3 ) . g e t _ s k i l l ( )

19 ) ;/ / ohohoh , why do you want t o be J a i J a i ?

(8)

Instance Methods and Inheritance (2/2)

1 c l a s s P r o f e s s o r {

2 p r i v a t e S t r i n g s k i l l = " t e l l j o k e s " ;

3 p u b l i c S t r i n g g e t _ s k i l l ( ) { r e t u r n s k i l l ; }

4 p u b l i c S t r i n g g e t _ s k i l l _ o h o h o h ( ) { r e t u r n s k i l l ; }

5 }

6 c l a s s J a i J a i P r o f e s s o r extends P r o f e s s o r {

7 p u b l i c S t r i n g g e t _ s k i l l _ h a h a h a ( ) { r e t u r n s k i l l + " , p l a y on BBS" ; }

8 p u b l i c S t r i n g g e t _ s k i l l _ o h o h o h ( ) {

9 r e t u r n g e t _ s k i l l _ o h o h o h ( ) + " , p l a y on BBS" ;

10 }

11 p u b l i c S t r i n g g e t _ s k i l l ( ) {

12 r e t u r n super. g e t _ s k i l l ( ) + " , p l a y on BBS" ;

13 }

14 }

the hahaha version: won’t work because skill is private

the ohohoh version: stack overflow (a.k.a. run out of paper)

(9)

static (compile-time) binding: determined by type instance variables

class variables class methods

—inherited class “hides” things from base class

—hidden stuff can be revealed by casting

dynamic (run-time) binding: determined by content all (non-private) instance methods in Java

“virtual” instance methods in C++

—inherited class “overrides” things from base class

—overridden stuff cannot be revealed by casting

參考文獻

相關文件

– at a premium (above its par value) when its coupon rate c is above the market interest rate r;. – at par (at its par value) when its coupon rate is equal to the market

– at a premium (above its par value) when its coupon rate c is above the market interest rate r;. – at par (at its par value) when its coupon rate is equal to the market

check type information very strictly by compiler (as opposed to single-object polymorphism): ensure type safety in JVM. Note: type information erased

argument ⇒ parameter: by reference value copying –same as reference argument/parameter.. Static Variables Revisited:

•Q :依據討論出的檢核標 準,評核這些組的內容.. •小組討論 (

find a bosom friend (Analects: Selected Reading (II): Methods of choosing friends”) and guides students to carry out a class discussion to understand the methods

# The respective numbers of Senior Teachers (excluding the Deputy Head) and Class Teachers by rank by PSM, AM, APSM and CM entitled by a school shall be determined in accordance

 The class of languages decided by polynomi al-time algorithms 是 the class of languages accepted by polynomial-time algorithms 的 su bset.. G=(V,E) is a simple cycle that contains