Tải bản đầy đủ (.pptx) (33 trang)

Session6 module8 9 annotations reflection

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (305.01 KB, 33 trang )






 !
Module 8 – Annotations
""#$%&'()*+(,-")(,
+./

)012

(3.232

*12

"32

"3"4
516

.7411provide additional
information about existing pieces of data$

1413344$*1891
83343.8.331139
3344$

.32343737$"443
3.848$


1133443323..8
.313.23431
3$


annotation4.81334431$
3317218$

Annotation typ17$*1.81334431
43$

*104.74:
// Definition of an Annotation type
public @interface TestAnnotation {
String value;
}
// Usage of Annotation
@TestAnnotation (value="name")
public void display() {}
Uses of Annotations

14.32:

Information for the compiler;..8143
333333$

Compiler-time and deployment-time processing;23
32343<=>7<231$

Runtime processing;43..04

34$

.334?32<
7<41<133344$
(+(+-@*
DO
DO
if the metadata changes the
design of the class.
if the metadata changes the
design of code dealing with
class.
if the metadata changes the
design of the class.
if the metadata changes the
design of code dealing with
class.
DON’T
DON’T

2883
.3..
33
.

51873
384.$

2883
.3..

33
.

51873
384.$
"32

3A3

"241

B3<1@TestAnno



"2<4343A3$

".33:C3

)04:@TestAnno(“Testing”)

D

"2444.3$

)144.333.8C3

@TestAnno(owner=“Stephen”, value=“class scope”)
E


F(3

F+33

F353

F*3

F

F13
GGuse a deprecated method and tell
GGcompiler not to generate a warning
@SuppressWarnings("deprecation")
(31HIJ ./+$31HIK
GG33suppressed
L
Annotations Used by the Compiler
// Javadoc comment follows
/**
* @deprecated
* explanation of why it was deprecated
*/
@Deprecated static void deprecatedMethod() { }
}
GGmark method as a superclass method
GGthat has been overridden
@Override 331HIJL
F(3


518<3283113
3$

2<8133H32
1I431<131
$,33:

3<.8<3118M

8233

3.3
F353

*234143
3331
41
131.
3$
%N
F353

*114331.
1142.
4$

*123341
32132.
4$


04<283
32413133<
.13.31418$
F353)04
class Thu
{
@Deprecated
public void chao() {System.out.println("\n Hello");}
}
class ThuTest
{
@SuppressWarnings({"deprecation"})
public void test()
{ Thu t = new Thu();
t.chao();
}
}
public class SuppressWarning
{
public static void main(String[] args)
{
ThuTest h = new ThuTest();
h.test();
}
}
F(4

*1F(4711.
14<4.81/$


*121F(41.
A/318
234134$
F

*181F34
13113$

1N:

8$+'")

8$">

8$'-*)
)=>)
@Retention(RetentionPolicy.SOURCE)
@interface Test_Retention
{
String v1();
}
class RetentionTest
{
@Test_Retention(v1="Hello")
public void thu() {System.out.println("Test annotations");}
}

class RetentionAnno
{public static void main(String args[])
{ new RetentionTest().thu();


}
}
"423"

*3823<1OFO84.
2.81A83<3214
$

343.1(3$

3431

34312

34333441
"4231

5132341<1
31F$

*1332144.321
243$

*13281.14
A11
4438
Module 9
Re2ection API
""#$%&'()*+(,-")(,

+.
PN
3

  1 .8 2 < ./  32  7
234.2$

*13334<1.3<
.33&'.3

23123:

8Q

"3./

"31338
PR
8Q

34:

2   2 1   .< 1 .8 A getName() 41< 1
4.33$

37:

E8AgetField() 41./$

333:


E8AgetConstructors() 41./$

1234:

E8AgetMethods() 41./$
P

×