Java用反射机制在不看类的实现方法下查看类的相关信息

这个个人感觉如果不做安全的话,直接看API就行了,但教程上有,然后就总结一下吧,可能对于深入了解Java有作用,还可能对以后获取别人设计的类信息(不看他的实现部分下)有作用吧

用静态方法查看Java类的名称

直接上代码

import javax.swing.JButton;
import java.lang.ClassNotFoundException;
class Test
{
    public static void main(String args[])
    {
        //方法① Object.getClass()返回一个Class类对象
        Class c1 = new JButton().getClass();
        //不能使用JButton.getClass()方法
        //可先JButton b = new JButton()声明一个JButton对象的,再用getClass()来查看
        System.out.println(c1.getName());

        //方法② .class方法,直接通过类名就能调用
        Class c2 = JButton.class;
        System.out.println(c2.getName());

        //方法③ Class类的静态方法forName(String a),此方法会抛出ClassNotFoundException的异常,所以使用这个方法时要使用try-catch语句
        try{
            Class c3 = Class.forName("javax.swing.JButton");
            System.out.println(c3.getName());
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }
}
查看类声明

具体包括:类的修饰符(如:public class ArrayList 的public),类的名称,类的泛型参数(如ArrayList<这个里面就是泛型参数>),类的继承类(该类父类),类的实现接口,类的注解
代码如下:

import java.util.ArrayList;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.Modifier;
import java.lang.annotation.Annotation;
//之后给出Annotation的链接门,Annotation在开发Java类库的时候很重要
class Test
{
    public static void main(String args[])
    {
        Class<?> c = ArrayList.class;//Class<?> 在本例中相当于ArrayList<int>或者其他的

        System.out.println("类的名称为:" + c.getName());

        System.out.println("类的修饰符为:" + Modifier.toString(c.getModifiers()));

        //类的泛型参数
        TypeVariable<?>[] typeVariable = c.getTypeParameters();
        System.out.print("类的泛型参数为:");
        if(typeVariable.length != 0)
            for(int i = 0; i < typeVariable.length; i++)
                System.out.println(typeVariable[i] + "\t");
        else
            System.out.println("空");

        //类的接口
        Type[] interface_ = c.getGenericInterfaces();
        System.out.print("类的接口为:");
        if(interface_.length != 0)
            for(int i = 0; i < interface_.length; i ++)
                System.out.println(interface_[i] + "\t");
        else
            System.out.println("空");

        //输出父类
        Type superClass = c.getGenericSuperclass();
        System.out.print("类的父类为:");
        if(superClass != null)
            System.out.println(superClass);
        else
            System.out.println("空");

        //输出类的注释信息,但有的注释信息是不能通过这种方式完成
        Annotation[] annotations = c.getAnnotations();
        System.out.print("类的注解为:");
        if(annotations.length != 0)
            for(int i = 0; i < annotations.length; i ++)
                System.out.println(annotations[i]);
        else
            System.out.println("空");

    }
}

输出如下:

类的名称为:java.util.ArrayList
类的修饰符为:public
类的泛型参数为:E
类的接口为:java.util.List<E>
interface java.util.RandomAccess
interface java.lang.Cloneable
interface java.io.Serializable
类的父类为:java.util.AbstractList<E>
类的注解为:空

Annotation的链接门:感谢www.open-open.com网站的资源

查看类的成员

包括:成员变量、构造方法、普通方法、内部类等

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;

class Test
{
    public static void main(String args[])
    {
        Class<?> c = ArrayList.class;
        System.out.println("类的标准名称为:" + c.getCanonicalName());
        System.out.println("类的一般名称为:" + c.getName());

        System.out.println("类的构造方法:");
        Constructor<?>[] constructors = c.getConstructors();
        if(constructors.length != 0)
            for(int i = 0; i < constructors.length; i ++)
                System.out.println("\t" + constructors[i]);
        else
            System.out.println("空");

        System.out.println("类的非继承变量:");//不包含其父类变量
        Field[] fileds = c.getDeclaredFields();
        if(fileds.length != 0)
            for(int i = 0; i < fileds.length; i ++)
                System.out.println("\t" + fileds[i]);
        else
            System.out.println("空");

        System.out.println("类的全部变量:");
        Field[] filedsAll = c.getFields();
        if(filedsAll.length != 0)
            for(int i = 0; i < filedsAll.length; i ++)
                System.out.println("\t" + filedsAll[i]);
        else
            System.out.println("空");

        System.out.println("类的非继承方法:");
        Method[] methods = c.getDeclaredMethods();
        if(methods.length != 0)
            for(int i = 0; i < methods.length; i ++)
                System.out.println("\t" + methods[i]);
        else
            System.out.println("空");

        System.out.println("类的所有方法:");
        Method[] methodsAll = c.getMethods();
        if(methodsAll.length != 0)
            for(int i = 0; i < methodsAll.length; i ++)
                System.out.println("\t" + methodsAll[i]);
        else
            System.out.println("空");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值