• 沒有找到結果。

More on super (1/1)

N/A
N/A
Protected

Academic year: 2022

Share "More on super (1/1)"

Copied!
24
0
0

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

全文

(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)
(13)
(14)
(15)
(16)

More on super (1/1)

1 c l a s s S t u d e n t {

2 i n t ID ; S t r i n g name ;

3 v o i d s t u d y _ f o r _ m i d t e r m ( ) {

4 System . o u t . p r i n t l n ( " I am s t u d y i n g f o r midterm . " ) ;

5 }

6 }

7 c l a s s AwardStudent extends S t u d e n t {

8 Award [ ] p r e s i d e n t _ a w a r d s ;

9 v o i d s t u d y _ f o r _ m i d t e r m ( ) {

10 f o r(i n t i = 0 ; i < 1 0 ; i ++)

11 super. s t u d y _ f o r _ m i d t e r m ( ) ;

12 }

13 }

super: much like (ParentName)this (but NOT the same)

H.-T. Lin (NTU CSIE) Inheritance OOP 04/12-13/2010 27 / 35

(17)

More on super: Key Point

this: a Sub-type reference variable pointing to the object itself super: a Base-type reference variable pointing to the object itself

same reference value, different type super.super: no

H.-T. Lin (NTU CSIE) Inheritance OOP 04/12-13/2010 28 / 35

(18)

Inheritance (1/5)

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

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

3 c l a s s EEProfessor extends P r o f e s s o r { }

4

5 c l a s s Demo{

6 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] argv ) {

7 CSIEProfessor HTLin = new CSIEProfessor ( ) ;

8 System . o u t . p r i n t l n ( HTLin . g e t C l a s s ( ) ) ;

9 System . o u t . p r i n t l n ( HTLin i n s t a n c e o f CSIEProfessor ) ;

10 System . o u t . p r i n t l n ( HTLin i n s t a n c e o f P r o f e s s o r ) ;

11 System . o u t . p r i n t l n ( HTLin i n s t a n c e o f EEProfessor ) ;

12 }

13 }

HTLin is an instance of CSIEProfessor

HTLin is an instance of Professor (because CSIEProfessor is a type of Professor)

HTLin is not an instance of EE Professor [the compiler knows]

H.-T. Lin (NTU CSIE) Inheritance OOP 04/12-13/2010 29 / 35

(19)

Inheritance (2/5)

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

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

3 c l a s s EEProfessor extends P r o f e s s o r { }

4

5 c l a s s Demo{

6 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] argv ) {

7 P r o f e s s o r HTLin = new CSIEProfessor ( ) ;

8 System . o u t . p r i n t l n ( HTLin . g e t C l a s s ( ) ) ;

9 System . o u t . p r i n t l n ( HTLin i n s t a n c e o f CSIEProfessor ) ;

10 System . o u t . p r i n t l n ( HTLin i n s t a n c e o f P r o f e s s o r ) ;

11 System . o u t . p r i n t l n ( HTLin i n s t a n c e o f EEProfessor ) ;

12 }

13 }

HTLin is an instance of CSIEProfessor HTLin is an instance of Professor

HTLin is not an instance of EE Professor [not easily determined at compile-time, but can be checked at run-time]

H.-T. Lin (NTU CSIE) Inheritance OOP 04/12-13/2010 30 / 35

(20)

Inheritance (3/5)

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

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

3 c l a s s EEProfessor extends P r o f e s s o r { }

4

5 c l a s s Demo{

6 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] argv ) {

7 P r o f e s s o r HTLin = new CSIEProfessor ( ) ;

8 i n t s co re = 100;

9 System . o u t . p r i n t l n ( HTLin . g e t C l a s s ( ) ) ;

10 System . o u t . p r i n t l n ( sc or e . g e t C l a s s ( ) ) ;

11 System . o u t . p r i n t l n ( HTLin i n s t a n c e o f j a v a . l a n g . O b j e c t ) ;

12 System . o u t . p r i n t l n ( sc or e i n s t a n c e o f j a v a . l a n g . O b j e c t ) ;

13 }

14 }

every valid object is an instance of java.lang.Object

primitive type is not an instance of anything [easily determined at compile-time]

H.-T. Lin (NTU CSIE) Inheritance OOP 04/12-13/2010 31 / 35

(21)

Inheritance (4/5)

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

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

3 c l a s s EEProfessor extends P r o f e s s o r { }

4

5 c l a s s Demo{

6 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] argv ) {

7 P r o f e s s o r [ ] p a r r = new CSIEProfessor [ 3 ] ;

8 System . o u t . p r i n t l n ( p a r r . g e t C l a s s ( ) ) ;

9 System . o u t . p r i n t l n ( p a r r i n s t a n c e o f O b j e c t ) ;

10 System . o u t . p r i n t l n ( p a r r i n s t a n c e o f O b j e c t [ ] ) ;

11 System . o u t . p r i n t l n ( p a r r i n s t a n c e o f P r o f e s s o r [ ] ) ;

12 System . o u t . p r i n t l n ( p a r r i n s t a n c e o f CSIEProfessor [ ] ) ;

13 System . o u t . p r i n t l n ( p a r r i n s t a n c e o f EEProfessor [ ] ) ;

14 }

15 }

every object array is an instance of Object[]

Object[] is a reference type

every reference type “extends” Object every object array is an instance of Object

H.-T. Lin (NTU CSIE) Inheritance OOP 04/12-13/2010 32 / 35

(22)

Inheritance (5/5)

1 c l a s s Demo{

2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] argv ) {

3 i n t[ ] o a r r = new i n t[ 3 ] ;

4 System . o u t . p r i n t l n ( o a r r . g e t C l a s s ( ) ) ;

5 System . o u t . p r i n t l n ( o a r r i n s t a n c e o f O b j e c t ) ;

6 System . o u t . p r i n t l n ( o a r r i n s t a n c e o f O b j e c t [ ] ) ;

7 System . o u t . p r i n t l n ( o a r r i n s t a n c e o f double[ ] ) ;

8 System . o u t . p r i n t l n ( o a r r i n s t a n c e o f s h o r t[ ] ) ;

9 System . o u t . p r i n t l n ( o a r r i n s t a n c e o f i n t[ ] ) ;

10 }

11 }

a int array is an instance of int[]

int[] is a reference type

every reference type “extends” Object every object array is an instance of Object

H.-T. Lin (NTU CSIE) Inheritance OOP 04/12-13/2010 33 / 35

(23)

Inheritance: Key Point

compile-time detectable: only siblings (int sibling of Object, EEProfessor sibling of CSIEProfessor, etc.)

run-time detectable: other instances (or null)

1 CSIEProfessor SomeOne = new CSIEProfessor ( ) ;

2 P r o f e s s o r p ;

3 p = SomeOne ; / / note : r e f e r e n c e assignment

4 i f ( p i n s t a n c e o f CSIEProfessor && p ! = n u l l) {

5 CSIEProfessor pCSIE = ( CSIEProfessor ) p ;

6 / / . . .

7 }

8 / ∗ i n s t a n c e o f i s n o t r e a l l y used v e r y o f t e n ;

9 b u t u s e f u l i n enhancing u n d e r s t a n d i n g . ∗ /

H.-T. Lin (NTU CSIE) Inheritance OOP 04/12-13/2010 34 / 35

(24)

Java Type Hierarchy

H.-T. Lin (NTU CSIE) Inheritance OOP 04/12-13/2010 35 / 35

參考文獻

相關文件

• To introduce the Learning Progression Framework (LPF) as a reference tool for designing a school- based writing programme to facilitate progressive development

In summary, the main contribution of this paper is to propose a new family of smoothing functions and correct a flaw in an algorithm studied in [13], which is used to guarantee

 High-speed sectioning images (up to 200 Hz) via temporal focusing-based widefield multiphoton microscopy.  To approach super-resolution microscopy

提升 提升 提升 提升表達能力 表達能力 表達能力 表達能力: : : : Super Super Super Super Duper Story Maker Duper Story Maker Duper Story Maker Duper Story

Structured programming 14 , if used properly, results in programs that are easy to write, understand, modify, and debug.... Steps of Developing A

Microphone and 600 ohm line conduits shall be mechanically and electrically connected to receptacle boxes and electrically grounded to the audio system ground point.. Lines in

Teacher then briefly explains the answers on Teachers’ Reference: Appendix 1 [Suggested Answers for Worksheet 1 (Understanding of Happy Life among Different Jewish Sects in

• Copy a value from the right-hand side (value or expression) to the space indicated by the variable in the left-hand side.. • You cannot write codes like 1 = x because 1 cannot