TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
Common.java:
0 k2 l. Z/ K7 `+ v2 J- ( m" I5 z8 r8 S I2 m
- public interface Common {
; y. k& e& i- ]0 Z
. @8 E& t8 f/ J/ C! C. I6 u$ N: d' z: C- public void action();
4 |8 P* k+ X- l( m6 D3 D - }
4 G# t* S* |. s3 u. P# J' f
复制代码 6 U" q7 |+ i! b; J
MyProxyed.java:
7 b; O$ ?# {" i; T3 ]- /**
- m9 l. K+ c6 Z$ V - * 被代理类* i% M6 A. ]6 X5 M' s) W" V
- * @author Administrator
+ J5 Y. p1 A4 U: b2 _+ v& |' Y y - *0 w+ W- ?$ @" d0 m" S9 J; }: Q
- */
0 t9 H4 W: ?% _. Q1 N- z - public class MyProxyed implements Common{
! `7 B! u0 ^% W6 E' k9 H4 @3 f - ; c1 l& N+ u) M- A
- public MyProxyed(){
7 Z8 U2 b6 u" ], \9 ], C -
1 G) h3 V. Y g2 ? N; h4 D - }
- T B9 Z+ N; p+ S - @Override- ^! u" q5 s; J' H1 i/ \) H+ x
- public void action() {+ H6 f, R; ]# U) c; y6 O2 l
- // TODO Auto-generated method stub9 p7 G7 V4 O& o
- System.out.println("我是被代理类,别忘了我。。。。");
; K+ e, X+ l. v4 Y5 b& r* ` - }
" V* B) h. Y; N' f( I- v0 K9 e E" E - ' {8 s- O: W4 |7 O: c0 E0 T7 \
- }
( a7 T# t- V T
复制代码 ProxyClass.java:. {; B! D) E9 J/ s. [
- import java.lang.reflect.InvocationHandler;
: T0 R$ a, B8 P8 g( l3 W+ h/ b - import java.lang.reflect.Method;. k5 E0 _) f" a' d3 o/ F
- import java.lang.reflect.Proxy;- J8 A; _- E/ ?+ g
- 0 ^" W6 D9 R3 a, l
9 T! T- b$ c6 _( x7 f/ a2 C3 z- public class ProxyClass implements InvocationHandler {9 C, p; j9 a$ F8 p4 w. l
- Object obj;
% [9 _( u6 M: }: ^; H# y( L - /**/ e/ U* h2 m0 C: U C% I
- * 用于生成代理类
# }' F# C, O( U5 \, L" D - * @param obj 被代理类对象
: n5 i- I8 l9 [" _, Q7 `# t - * @return 一个代理类. Y Q% X& p9 q& }
- */% x. w! g8 C* ?9 x% a
- public Object blind(Object obj){$ f# l+ i! n0 V
- this.obj=obj;
* q* I2 y# N* [; Q0 D - return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
m, z+ U. H9 w7 {! p% B - }
1 h* D+ `" j* ^ - /**
" E3 k# D3 S( {. w% B - * 代理类对象调用接口方法时,实质上是返回来调用了该方法
/ U$ h% ~) q( _; E$ \; N5 w) ` - */
4 a6 y* l1 x- }6 S$ d$ l9 j - @Override
4 l: @0 ]0 I: |4 O - public Object invoke(Object arg0, Method arg1, Object[] arg2)$ x% n6 U$ J: O8 q( g: g+ y
- throws Throwable {) w T' d2 ~& j5 O/ a- [
- // TODO Auto-generated method stub
/ x. R1 { H* ^( J. y2 ~1 C2 s - Object returnVal=arg1.invoke(obj, arg2); K' v/ Y, F7 H( m3 q. ~, a g7 J; [
- System.out.println("我是代理类");5 M) Z2 {* H N' T* R. [1 s9 `
- return returnVal;: A$ s: n! G) H/ A/ U1 r* a
- }
1 u+ B. @6 N8 L& U2 t2 `- u0 s+ V1 q - 1 m% I% i0 ^) A
- }$ ~, \5 P& ^' T% d
复制代码 Test.java:
0 z; ^/ B( E% w. }( q- /**" M- A9 e$ s+ \' e( u! c
- * 一个动态代理的例子7 V4 Y! ^$ _5 R! \+ U* n9 D
- * @author Administrator/ r2 ]2 s9 @9 t( @# }/ Y
- *被代理类和代理类有共同的方法,代理类调用方法时,其是实质是调用了InvocationHandler中的invoke方法
8 u: I7 O1 }$ C+ k - */
& R; E8 |" o: X8 K/ G - public class Test {. H( o( V% ]9 b7 ~! d( |7 X
- / h& `% N& v% j+ ` q! Z8 F
- public static void main(String[] args) {2 t2 F4 p' Z2 ]! g
- // TODO Auto-generated method stub) y9 |6 s5 ^. t$ T. v3 X3 X3 J
- MyProxyed proxyed=new MyProxyed();
1 c) @! @, C% f - ProxyClass proxy=new ProxyClass();6 ]# \4 }5 j0 o( T% n4 X; Q
- //这个是真正的代理类& k& |# G7 H. H; \: W# k7 q. A
- Object obj=proxy.blind(proxyed);
* g; t5 l( `; [. k - Common common=(Common)obj;( x6 u4 z6 a; q0 v5 k1 ~! I' R* j
- common.action();
8 Z, O" p) b9 a - }# K6 c6 n% w7 V `' e1 N
- 8 y( i) B/ k$ m( G2 K m
- }0 c; Y; c2 x1 m: z% p
复制代码 9 ]: L( b" `0 \! h. w: U. W
ProxyTest.zip
(5.52 KB, 下载次数: 5)
8 X0 A( P- Z) \$ w3 |4 J' O
/ U1 U" Y8 Z4 b; r9 n) k |
|