?.?.?. ???????? ?.?. (email: egrinkrug@hse.ru) - PowerPoint PPT Presentation

About This Presentation
Title:

?.?.?. ???????? ?.?. (email: egrinkrug@hse.ru)

Description:

Title: - Author: Administrator Last modified by: EGrinkrug Created Date – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 25
Provided by: hseRudata26
Category:

less

Transcript and Presenter's Notes

Title: ?.?.?. ???????? ?.?. (email: egrinkrug@hse.ru)


1
????????-??????????????? ?????? ?
????????????????
?????? 8. ?????????? ? ?????
?.?.?. ???????? ?.?. (email egrinkrug_at_hse.ru)
2
Runtime Type Information
  • ???????? ????????? ? ????????????? ?????????? ?
    ????? ?? ????? ?????????? ????????? ??????
    ???????? ???.
  • ?????????? ??????
  • ??????????? ????? Shape c ??????? draw()
  • ??? ?????????? ??????????? ?????? (?.?. ? ??????
    ?????????????)

3
?????? ??????????
4
??? ??? ????????? ??? ????? ?????????
  • ??? ????? Shape ?? ????? ?????????????????
  • ????? draw() ???????? ?????? Shape ??????
    ?????????? ????? toString() ??? ?????? ????????
    ?????????? ??????
  • ??? ????? ?????? System.out.println() ??????????
    ?????? this
  • ????? toString() ? Shape ???????????. ??
  • ?????????????? ????? toString() ??
    java.lang.Object
  • ????????? ??????????? ?? Shape ?????? ??????????
    toString().
  • ????? this ??????????? ? ????????? ????????????
    ?????, ????????????? ?????????? ??? toString()
  • ??? ????????? ??????????? ?????????? ???? ?
    ?????? ??????????? ???? ?????????? ??????????
    ?????????????? ???? ?????? ?????? ?????? ???????
    Shape.
  • ????? ???????? ??????????? ??? ?????? ??????
    ?????????? ?? ????? draw() ? ?? ????? toString()
  • ????????? ?????????
  • Circle.draw()
  • Square.draw()
  • Triangle.draw()
  • ??? ????, ???? ???-???? ??????? ??????, ? ?????
    ????? ????? Shape ?? ???????? (????????, ?????
    ????????? ???????????? ? ??????? ????)? ???
    ?????? ??? ???????, ???? ?????? ???????? ?????

5
?????? Class
  • ?????? ?????, ??????????????? ? ?????????,
    ??????????? ????? ???????? Class
  • ??? ????????? ? ?????????? ?????? ?????? ??? ????
    ????????? ?????? Class, ??????? ??????????? ?
    ??????????? ????? ? ??????????? .class
  • ???? ?????? ??????????? ??????????? ???????
    (ClassLoader??)
  • ???????? ??????? ????? ??????????? ???????
    ???????????? ??????????? ???????, ??????? ?????
    ?????????????? ??????????? ?????????
  • ???????? (????????) ????????? ???????? ??????
    ?????????? JVM
  • ?????? ???? ????????? ????????? ??????????
    ??????, ??????? Java API, ? ?????????? ?????
  • ?????????????? ?????????? ??????? ?????
    ?????????? ?????? ? ???-???????, ?? ???? ??? ????
    ???????? ???????? ?? ?? ????...
  • ??? ?????? ??????????? ? JVM ???????????, ???
    ?????? ?????????????
  • ??????????? ????-??? ??????????? JVM ??
    ????????????.

6
?????? Class
  • ?????? Class ??? ???? ??????, ??????????????
    ???????, ? ??????? ????? ?????????????? ?
    ?????????, ?????????? ??????? ?? ????.
  • ?????? ?????? ????? ????? ?? ??????
    java.lang.Object
  • public final Class getClass()
  • ?????????? ????? ?? ????? ??????? (?????,
    ????????? ?????? ??????)
  • ?????? ?????? ???????? ? ?? ????? ????
    ?????????????????
  • ????????
  • void myMethod ( Object parameter )
  • Class parameterClass parameter.getClass()
    //
  • - ????????? getClass() ??? ????????? ??????
    ???????????? ????????? ?? ????? ?????????? (at
    runtime)...

7
????? Class
  • ??? ??????? Class ??????????? public final ??????
    Class
  • Object ? new Object() // ??? new
    Tri?ngle(), ? ?.?. ? ?.?.
  • Class oc o.getClass() // ???
    ?????? ???? ?????????? ?????? Class !!!
  • Class cc oc.getClass() // ? ???
    ????? ?lass ??? ???? ????? !!!
  • (cc.getClass() cc) --gt true // ??? ?????
    ??? ?? ??? !!!
  • ????? Class ??? ?????????, ????????????
    ????????, ??????? ????? ??????????? ? ?????????
    Class (??? ?????? - ?????? ???????? ? C ?)
  • ??? ? ??? ????? ????????, ???? ?????? ?? ??????,
    ????? ????????? ??? ???? ???????? ????????,
    ???????????? ? ?????? ??????? (?.?.
    ???????-?????)
  • ???? ?????? ?? ?????????? ?????, ????? ????????
    (?????????????) ?????? ?????? Class ??? ???????
    ??????????? ??????.
  • ??? ? ????? ?????, ????? Class ????? ???????????
    ??????, ?? ????????? ??????? ?????????? (???????
    Class) ??? ?? ??????.
  • C????? ? ???????? ?????????? ??????? ?????? Class
    ???????? ??????? ?????????? ?????????
    ????????-???????????????? ???????????????? ?
    Java, ?????? ? ???????? ?????? Object.

8
???????? ?????? ?????? Class
  • ????? ?????? Class ???????? immutable-????????
    (??? ??? ?????????) ??? ?????? ?????? Class
    ???????? ?????????? ?? ???????? Class??, ? ??
    ?????? ??.
  • ????????? ????? ??????
  • String getName () // ???? ?????? ??? ?????? (?
    jdk 1.0)
  • ????????, com.grinkrug.rtti.Triangle
  • String getSimpleName () // ???? ???????? ???
    ?????? (c jdk1.5)
  • ????????, Triangle
  • String getCanonicalName () // ???? ????????????
    ??? ?????? (? jdk1.5)
  • ????????, com.grinkrug.rtti.Triangle
  • ? ??????? ?? getName() ?????????? ?????? ?????
    ????????? ??????? ????? . (?????), ? ?? ????
    , null ??? ????????? ? ?????????
    ???????...(???????? ?? ?????????? ??? ???????? ?
    ?????? ?????????? ???????, ??????? ????????? ?
    ?????????!)

9
??? ??? ?? ?????????? ???????
  • ?????? ??? top-level ?????? / ?????????? ???
    ??? ????????????? ??? ?????? ?om.grinkrug.rtti.T
    riangle
  • ?????? ??? ???????????? ???? ???
    ??????????????? ???????? ?????

  • int, byte, float
  • ??????????? ??? ?????? ??????? ?????????????? ???
    ??????? (?????????? ?? ?????? ??????????????
    ??????????? ???????) ??? ???? ?????????,
  • ??? ??????????? ???? ????????? ??????????
    ?????????, ? ????????? ???????? L lt??? ????gt
  • ???????? F ????????? ?????? float ?
    Ljava.lang.Object ?????? Object??.
  • ???????????? ????? ????????? ? jdk1.5(), ???
    ???????? ?????, ?? ?? ???????? ?????? ?????
    (fully qualified names) ?? ??????? jdk.

boolean Z float F byte
B int I char
C long J double
D short S ????? ???
????????? L
10
  • ????????? ?????? ?? ????? (static - ?????)
  • public static Class forName ( String className )

  • throws ?lassNotFoundException
  • ???????? ??? ???? ?????? ??? ?????????? ??????
    (fully qualified name)
  • ?????????? ?????? ?? ?????? Class,
    ??????????????? ?????????? ????? (?????????
    ?????? ????? ??? ?????????????, ?? ????? ???????,
    ??????? ??? ????...).
  • ????????? ?????? ?? ?????-????????
  • ???? ?????? ????? ?????????? ????????, ?????? ??
    ???? ????? ???????? ?? ?????-???????? Class c
    Triangle.class
  • ???????????? ???????????? ??? (? ??????? ?? fully
    qualified name ??? ?????? forName())
    Object.class
  • ????????? ?????? ?????????????? ???????????? (?
    ?? ? ????????, ??? ?????? ??????) ? ?????????????
    ????????????? ?? ??????? ????????????? ?????? ???
    ?????????? ??? ????? ?????.
  • ? Java ??? ??????????? ??????????????? ?????, ??
    ???? ??????????????? ????? ???????????? ??
    ???????, ? ?? ???, ??? ??? ????? ?????????, ?.?.
    ????????? ??? ?????????. ???????, ????? ?????
    ?????.

11
?????? ?????? ?lass ??? ??????? ???????????????
??????? ?????? ??????? Class ????? ??????
??????????? ???, ??? ????? ????????????? ????? ?
????.
  • ???????? ?? ?????? ????? ????????????
  • public boolean isInterface ( )
  • ???????? ?? ?????? ????? ??????? ????????
  • public boolean isArray ( )
  • - ? ???? ????????, ??
  • public Class getComponentType ( )
  • ???????? ?? ????? ????? Enum-???
  • public boolean isEnum ( )
  • ???????????? ?? ?????? ????? ??????????? ????
  • public boolean isPrimitive ( )
  • ???????? ?? ????? ????? ??????????
  • public boolean isAnonimousClass ( )
  • ???????? ?? ????? ????? ??????????
  • public boolean isLocalClass()
  • ???????? ?? ????? ????? ???????-??????
    (?????????)
  • public boolean isMemberClass ( )
  • ????? ????? ???????????? ?????? ??????
  • public Class getDeclaringClass ( )

12
  • ????? ?????????? ??????????? ?????? ??????? ???
    ????????????
  • public Class getInterfaces()
  • ????? ?????????? ? ??????? ???????
  • public Class getSuperclass()
  • ???????????? ?? ?????? ????? ?????????? /
    ?????????????? ????????
  • public boolean isAssignableFrom (Class
    another)
  • ????? ??????????? ??????? ??? ??????? ??????
    ??????
  • public ClassLoader getClassLaoder()
  • ?????? ?????? ??????????? ?????? ??????
  • public Package getPackage()
  • ???????? ?? ?????????? ?????? ????????? ???????
    ???????
  • public boolean isInstance(Object o)
  • ??? ???????????? ?????????? ?????????
    instanceof
  • if(shape instanceof Triangle )
  • Triangle t (Triangle)shape
  • ????????? ??????, ????? ????? ????? ????????????
    ?????????? ? ??? ?? ???? ?????????? ?????
    isInstance() ? ???????? instanceof ?

13
Reflection ???????????? ?????? ? ???????
  • ??? ????, ???? ?? ????? ???????? ? ?????
    ????????
  • ?? ????? ?????????, ??????? ???????? ? ??????????
    ????????
  • ??? ???????? ?????-?? ?????? (??? ?????????
    ????), ? ??????? ??????????? ??.
  • ??? ???????? ???????? ??? ?????????????
    ????????????????
  • ???-?? ?????? ?????????? ??????? ??????????
  • ?? ?????????? ?? ?? ???? ???????????, ???
    ?????? ?????? ???????, ??? ??????????, ??? ????
    (?.?. ??? ?????????????? ??????)
  • ???????? ?????????????? ????? ???? ??????? ? ??
    ?????? ???????????????? (????????)
    ??????????????
  • ??? ????????? ??????? ??????? (Remote Method
    Invocation, RMI), ??? ?? ????? ???????? ?
    ???????? ?? ?????? ??????, ??? ?????????????
    ??????? ?? ????? ??? ?????????? ????? ?????????.
  • ??? ????????????? ???????????? / ??????????????
    ????????, ?. ??.

14
????? Class ? reflection
  • ????? Class ???????????? ????????? Reflection
    (??? ? ?????????)
  • ?????????????? ????????? ?????????????? ???????
    java.lang.reflect.
  • ? ?????????? java.lang.reflect ???? ??????,
    ??????? ??????? ????????? JVM ??? ????????????
    ?????? ?????? ? ???????? ?????????? ?????????.
    ????????? ?? ??? ????????
  • Constructor
  • Method
  • Field
  • ????? Class ????? ??????, ??????????? ????????
    ??? ??????? ??? ????????? ?????????? ?????? ?
    ???????????? ?? ? ????????. ????? ????, ? ?????
    ?????? Class ???? ?????? ??? ??????? ???
    ????????? ???????, ? ?.?.

15
?????????? ?????????? ? ?????????????
??????(??????????? ?????? ??????? ?????? Class)
  • ????????? ???? public ????????????? ??????
  • public Constructor getConstructors ( )
    throws SecurityException
  • ????????? ???? ????????????? ?????? (???????
    ?????????)
  • public Constructor getDeclaredConstructors (
    ) throws SecurityException
  • ????????? ????????? public - ???????????? ??????
  • public Constructor getConstructor (Class
    paramTypes)
  • throws NoSuchMethodException,
    SecurityException
  • ????????? ????????? ????????????,
    ???????????????? ? ??????
  • public Constructor getDeclaredConstructor
    (Class paramTypes)
  • throws NoSuchMethodException,
    SecurityException

16
???????????????? ??????
  • ????????? ????? ??? ????? ????????? ????
    ?????????? (??? ??????? ???????????? ???
    ??????????)
  • public Object newInstance() throws
    InstantiationException,IllegalAccessException
  • ?????? ??????????? (java.lang.reflect.Constructor)
    ?????????
  • ????????? ?????????? ?????? (?????? ??? ???
    ?????? ???????????), ?.?. -???? ??????????,
    ??????????, ...
  • ????????? ???????? ? ????????????? ?????? ???????
    ??????, ???????? ??????????? ??????????? (??????,
    ??? ??????????? ??? ????????????), ? ?????????
    ??????????? ????????

static Object createInstance(Constructor c,
Object params) throws Exception
return c.newInstance(params)
17
?????????? ?????????? ? ???????
??????(??????????? ?????? ??????? ?????? Class)
  • ????????? ???? public ??????? ??????
  • public Method getMethods ( ) throws
    SecurityException
  • ????????? ???? ??????? ?????? (??????? ?????????,
    ?? ???????? ??? ??????????????)
  • public Method getDeclaredMethods ( ) throws
    SecurityException
  • ????????? ????????? public - ?????? ??????
  • public Method getMethod (String name, Class
    paramTypes)
  • throws NoSuchMethodException,
    SecurityException
  • ????????? ????????? ??????, ???????????????? ?
    ??????
  • public Method getDeclaredMethod (String name,
    Class paramTypes)
  • throws NoSuchMethodException,
    SecurityException

18
????????????? ??????? Method
  • ??? ? ?????? Constructor, ?????? Method ?????
    ???????? ?????????? ? ???? (???? ??????????,
    ????????????? ????????, ????????? ?? ??
    ?????????? ????? ???????????, ? ??.)
  • ???????? ?????????? ???????????? ??????? Method
    ???????? ?????? ??????
  • ????????

public Object invoke (Object obj, Object
args) throws IllegalAccessException,
AllegalArgumentException, InvocationTargetExc
eption
static Object callMethod (Object o, Method m,
Objectparams) throws Exception return
m.invoke (o, params)
19
?????? ? ?????? ??????(??????????? ??????
??????? ?????? Class)
  • ????????? ???? public ????? ??????
  • public Field getFields ( ) throws
    SecurityException
  • ????????? ???? ????? ?????? (??????? ?????????,
    ?? ???????? ??? ??????????????)
  • public Field getDeclaredFields ( ) throws
    SecurityException
  • ????????? ????????? public - ???? ?????? ??
    ?????
  • public Field getField (String name)
  • throws NoSuchFieldException,
    SecurityException
  • ????????? ????????? ????, ???????????????? ?
    ?????? ??????
  • public Field getDeclaredField (String name)
  • throws NoSuchFieldException,
    SecurityException

20
?????? ? ?????? ??????
  • ????? Field ???????????? ??? ??????? ? ????????
    ???? ?????? ? ??????????? ???????
  • ???? ???? ? ?????? ???????????, ?? ??????????
    ?????? ? ???????? ??????? ???????????? ? ?????
    ???? null
  • ??? ??????? ? ???????? ???? ? ?????? Field ????
    ????? ???????
  • ??????
  • public Object get (Object concreteObject)
  • throws
    IllegalArgumentException, IllegalAccessExpublictio
    n
  • public int getInt (Object concreteObject)
  • throws IllegalArgumentException,
    IllegalAccessExpubliction
  • // ? ??? ?????? ??????????? ?????
  • ? ??????
  • public void set (Object concreteObject, Object
    newValue)
  • throws IllegalArgumentException,
    IllegalAccessExpubliction
  • public void setInt (Object concreteObject, int
    newValue)
  • throws IllegalArgumentExcep
    tion, IllegalAccessExpubliction
  • // ? ??? ????? ??????????? ?????

21
Reflection ? ???????
  • ? ?????????? reflection ???? ????????? ????? ???
    ????????????? ???????? ???????? ? ??????? ? ???
    java.lang.reflect.Array.
  • ? ???? ??????
  • ????????? ???????? ???????
  • public static Object newInstance(Class
    componentType, int length)
  • throws NegativeArraySizeException
  • public static Object newInstance(Class
    componentType, int dimensions)
  • throws IllegalArgumentException,
    NegativeArraySizeException
  • ???????? ?????? ? ????????? ???????
  • public static Object get (Object array, int
    index)
  • throws IllegalArgumentException,
    ArrayIndexOutOfBoundsException
  • public static void set (Object array, int index,
    Object newValue)
  • throws IllegalArgumentException,
    ArrayIndexOutOfBoundsException
  • ? ?? ??????? ??? ??????????? ????? ?????????
    ???????...
  • ??? ????? ????????? ??????? ???????????? ?????? ?
    ??????????

22
?????????? ???????????? ??????? reflection
  • ??? ?????????? ???????? ??????? ??????????
  • ??? ???????????? ???????, ??? ????????????? ??
    runtime
  • ??? ?????? (??? ??????????? ???????????? ???????)
    ?? ????????? ? ????????? ????? (????????? ???!)
  • ???????????? ?????? ?????????? ????????? (????
    ???? ?????? ?????????? ????????? ??????????????)

23
?????????????????? reflection
  • ? ?????????? ????? ??????? ? JDK 5() ?????????
    ??????????????? ?????????? ? reflection-??????????
    ? ? ????? ?????? Class
  • ??? ???????? ????? ???????? ??????????? ? ??????
    ? ??????, ???????????? ? Java
  • ?? ?????????? ????? ?????????????????? ? ????????
    reflection ??? ??? ? ??????????.

24
QA
Write a Comment
User Comments (0)
About PowerShow.com