反射获取类结构信息

 

代码如下:

 1 package com.sina.demo;
 2 interface IMessageService{
 3     public void send();
 4 }
 5 interface IChannel{
 6     public boolean connect();
 7 }
 8 abstract class AbstractBase{}
 9 class Mail extends AbstractBase implements IMessageService,IChannel{
10     @Override
11     public boolean connect() {
12         return true;
13     }
14     @Override
15     public void send() {
16         if(this.connect()) {
17             System.out.println("【信息发送】www.sina.com.cn");
18         }
19     }
20 }
21 public class JavaReflectDemo {
22     public static void main(String[] args) {
23         Class<?> cls = Mail.class;
24         Package pkg = cls.getPackage();
25         System.out.println("包名称:"+pkg.getName());
26         Class<?> parent = cls.getSuperclass();
27         System.out.println("父类名称:"+parent.getName());
28         Class<?> clazz[] = cls.getInterfaces();
29         for(Class<?> temp:clazz) {
30             System.out.println("父接口名称:"+temp);
31         }
32     }
33 }

运行结果:

包名称:com.sina.demo
父类名称:com.sina.demo.AbstractBase
父接口名称:interface com.sina.demo.IMessageService
父接口名称:interface com.sina.demo.IChannel

 

点赞