反射: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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值