美文网首页
ArrayList既然继承自AbstractList抽象类,而A

ArrayList既然继承自AbstractList抽象类,而A

作者: 阿南的生活记录 | 来源:发表于2018-09-07 16:57 被阅读0次
    package example;
    
    import java.io.Serializable;
    import java.util.Arrays;
    
    public class Test {
    
     public static interface MyInterface {
      void foo();
     }
    
     public static class BaseClass implements MyInterface, Cloneable, Serializable {
    
      @Override
      public void foo() {
       System.out.println("BaseClass.foo");
      }
     }
    
     public static class Class1 extends BaseClass {
    
      @Override
      public void foo() {
       super.foo();
       System.out.println("Class1.foo");
      }
     }
    
     static class Class2 extends BaseClass implements MyInterface, Cloneable,
       Serializable {
    
      @Override
      public void foo() {
       super.foo();
       System.out.println("Class2.foo");
      }
     }
    
     public static void main(String[] args) {
    
      showInterfacesFor(BaseClass.class);
      showInterfacesFor(Class1.class);
      showInterfacesFor(Class2.class);
     }
    
     private static void showInterfacesFor(Class<?> clazz) {
      System.out.printf("%s --> %s\n", clazz, Arrays.toString(clazz
        .getInterfaces()));
     }
    }
    

    输出:

    class example.Test$BaseClass --> [interface example.Test$MyInterface, interface java.lang.Cloneable, interface java.io.Serializable]
    class example.Test$Class1 --> []
    class example.Test$Class2 --> [interface example.Test$MyInterface, interface java.lang.Cloneable, interface java.io.Serializable]
    

    Class1没有定义明确的接口,因此Class#getInterfaces()不包括那些接口,而Class2直接实现了该接口。

    package example;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    
    import example.Test.BaseClass;
    import example.Test.Class1;
    import example.Test.Class2;
    
    public class Test2 extends Test {
    
     public static void main(String[] args) {
    
      MyInterface c1 = new Class1();
      MyInterface c2 = new Class2();
    
      // Note the order...
      MyInterface proxy2 = createProxy(c2);
      proxy2.foo();
    
      // This fails with an unchecked exception
      MyInterface proxy1 = createProxy(c1);
      proxy1.foo();
     }
    
     private static <T> T createProxy(final T obj) {
    
      final InvocationHandler handler = new InvocationHandler() {
    
       @Override
       public Object invoke(Object proxy, Method method, Object[] args)
         throws Throwable {
        System.out.printf("About to call %s() on %s\n", method
          .getName(), obj);
        return method.invoke(obj, args);
       }
      };
    
      return (T) Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj
        .getClass().getInterfaces(), handler);
     }
    }
    

    虽然Class1确实隐式实现了MyInterface,但是创建的代理没有实现,所以报错。
    输出:

    About to call foo() on example.Test$Class2@578ceb
    BaseClass.foo
    Class2.foo
    Exception in thread "main" java.lang.ClassCastException: $Proxy1 cannot be cast to example.Test$MyInterface
     at example.Test2.main(Test2.java:23)
    

    相关文章

      网友评论

          本文标题:ArrayList既然继承自AbstractList抽象类,而A

          本文链接:https://www.haomeiwen.com/subject/ylgcgftx.html