类 Proxy中的方法:
newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) 
的第二个参数,在JDK中这样描述:interfaces - the list of interfaces for the proxy class to implement1、我如果 我有这么个接口:
public interface Subject {
public void requst();
}
2、有这么个类去实现这个接口:
public class RealSubject implements Subject {
public void requst() {
System.out.println("From real subject!");
}可以这样去 获得:
realSubject.getClass().getInterfaces()
而不能这样去获得:
Subject.class.getInterfaces()  ?
我想问的是,好像没有听说过“用接口”去取得 Class 对象的想法过哦···

解决方案 »

  1.   

    确定此对象所表示的类或接口实现的接口..还有接口怎么就不可以Subject.class.getInterfaces()这样调用???难道接口不生成.class字节文件??
      

  2.   

    打开Class的源文件:
       *
         * @return an array of interfaces implemented by this class.
         */
        public native Class<?>[] getInterfaces();
    这个方法返回的是该类实现的接口。  Subject是个接口,它没有实现任何接口,所以Subject.class.getInterfaces()返回size为0的数组。
      

  3.   

    Subject是一个接口,没有类的实现,RealSubject.getClass()实际上是先得到一个RealSubject的类对象(当然得到的是Class类型的),然后去用这个Class类对象去调用getInterface()方法,来得到RealSubject的实例。这是用反射机制来实例化对象的另一种方法,但是前提是,比如RealSubject必须是一个类,不能是抽象类或接口,这个你自己也应该想得通,因为接口和抽象类都是不能实例化的,当然用反射机制来实例化也是行不通的。显然Subject是个接口,不管是.class还是.getClass(),都是错的。java中和反射机制用的时候不多,但是不说明不重要,因为学了这个,java中的一些以前不明白的东西会突然理解了如果你想更好的理解一下java的反射,去学一下动态代理模式吧
      

  4.   

    不好意思哥们儿,看错了看成getInstance了。If this object represents a class or interface that implements no interfaces, the method returns an array of length 0. 看一下这段话就明白了:如果这个对象表示的是一个没有继承任何接口的类或者接口,那么该方法就返回一个长度为0的Classi数组。