类方法学习四:getGenericInterfaces,getInterfaces

一.说明

1.Type[] getGenericInterfaces

以Type的形式返回本类直接实现的接口.这样就包含了泛型参数信息

2.Class[] getInterfaces

返回本类直接实现的接口.不包含泛型参数信息

二.例子

1.接口

[java] view plain copy
  1. public interface MyInterfaceA {  
  2.   
  3.   public void sayHello();  
  4.   
  5. }  
  6.   
  7. public interface MyInterfaceB{  
  8.   
  9.   public String print();  
  10.   
  11. }  
  12.   
  13. public interface MyInterfaceC<A,B> {  
  14.   
  15.   public A getA();  
  16.   
  17.   public B getB();  
  18.   
  19. }  

2.Test.java

[java] view plain copy
  1. import java.lang.reflect.Type;  
  2.   
  3. import java.lang.reflect.ParameterizedType;  
  4.   
  5.   
  6.   
  7. public class Test<A,B> implements MyInterfaceC<A,B>,MyInterfaceA,MyInterfaceB{  
  8.   
  9.   private A a;  
  10.   
  11.   private B b;  
  12.   
  13.   //MyInterfaceC  
  14.   
  15.   public A getA() {  
  16.   
  17.     return a;  
  18.   
  19.   }  
  20.   
  21.   public B getB() {  
  22.   
  23.     return b;  
  24.   
  25.   }  
  26.   
  27.   //MyInterfaceA  
  28.   
  29.   public void sayHello() {  
  30.   
  31.   }  
  32.   
  33.   //MyInterfaceB  
  34.   
  35.   public String print() {  
  36.   
  37.     return "";  
  38.   
  39.   }  
  40.   
  41.   
  42.   
  43.   public static void main(String args[]) throws Exception{  
  44.   
  45.     Type[] ts = Test.class.getGenericInterfaces();  
  46.   
  47.       
  48.   
  49.     System.out.println("======getGenericInterfaces======:");  
  50.   
  51.     for (Type t:ts) {  
  52.   
  53.       System.out.println(t);  
  54.   
  55.         
  56.   
  57.       if (ParameterizedType.class.isAssignableFrom(t.getClass())) {  
  58.   
  59.         System.out.print("----------->getActualTypeArguments:");  
  60.   
  61.         for (Type t1:((ParameterizedType)t).getActualTypeArguments()) {  
  62.   
  63.           System.out.print(t1 + ",");  
  64.   
  65.         }  
  66.   
  67.         System.out.println();  
  68.   
  69.       }  
  70.   
  71.     }  
  72.   
  73.       
  74.   
  75.     System.out.println();  
  76.   
  77.     System.out.println("======getInterfaces======:");  
  78.   
  79.     for (Class c:Test.class.getInterfaces()) {  
  80.   
  81.       System.out.println(c.getName());  
  82.   
  83.     }  
  84.   
  85.   }  
  86.   
  87. }  

输出结果:

======getGenericInterfaces======:

MyInterfaceC<A, B>

----------->getActualTypeArguments:A,B,

interface MyInterfaceA

interface MyInterfaceB

======getInterfaces======:

MyInterfaceC

MyInterfaceA

MyInterfaceB

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值