反射:Class、Method

java.lang .Class

Instances of the class Class represent classes and interfaces in a running Java application. An enum is a kind of class and an annotation(注释) is a kind of interface. Every array also belongs(属于) to a class that is reflected as aClass object that is shared(共享) by all arrays with the same element type and number of dimensions. The primitive(原生) Java types (boolean,byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.

Class has no public constructor. Instead Class objects are constructed automatically(自动) by the Java Virtual Machine as classes are loaded and by calls to thedefineClass method in the class loader.

The following example uses a Class object to print the class name of an object:

     void printClassName(Object obj) {
         System.out.println("The class of " + obj +
                            " is " + obj.getClass().getName());
     }
 

It is also possible to get the Class object for a named type (or for void) using a class literal (JLS Section15.8.2). For example:

     System.out.println("The name of class Foo is: "+Foo.class.getName());
 

java.lang.reflect .Class Method

A Method provides information about, and access to, a single method on a class or interface. The reflected method may be a class method or an instance method (including an abstract method).

A Method permits(允许) widening (扩展)conversions to occur when matching(匹配) the actual(实际) parameters to invoke with the underlying(底层) method's formal parameters, but it throws anIllegalArgumentException if a narrowing (收缩)conversion would occur.

posted @ 2012-05-04 19:06  hibernate3例子  阅读(193)  评论(0编辑  收藏  举报