温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

getClass .class如何在java中使用

发布时间:2021-04-15 17:55:57 来源:亿速云 阅读:164 作者:Leah 栏目:编程语言

本篇文章给大家分享的是有关getClass .class如何在java中使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

java反射机制

怎么从一个实例上的获得类反射

/**
 * obj 实例的对象
 * getClass() 获得该实例类的反射
 * @return 
 * Class<? extends Object>
 */
obj.getClass();
//例子
String str1 = "123";
Class<?> strClass1 = str1.getClass();
String str2 = new String("456");
Class<?> strClass2 = str2.getClass();

怎么从一个类上获得类的反射

/**
 * ClassName 类名称
 * .class 该类的属性
 * @return
 * Class<? extends Object>
 */
ClassName.class
//例子
Class<?> a = String.class
Class<?> b = Integer.class
Class<?> c = Double.class
package Main;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import Demo.Demo1;
public class Index 
{
  public static void main(String[] agrs) throws Exception
  {
    Demo1 obj = new Demo1();
    String str = (String)runTargetMethod(obj,"test1",new Class[]{String.class,String.class},new Object[]{"2","3"});
    System.out.println(str);
    System.out.println();
    System.out.println();
    System.out.println();
    dumpTargetMethod(obj);
  }
  /**
   * 调用指定对象的指定方法。
   * @param Object obj 对象实例
   * @param String methodName 要调用的方法名称
   * @param Class<?>[] classes 方法中的参数对象
   * @param Object[] valObj 需要向参数中传递的值 
   * @return Object 直接结果
   * */
  public static Object runTargetMethod(Object obj,String methodName,Class<?>[] classes,Object[] valObj) throws Exception
  {
    Class<?> classObj = obj.getClass();
    Method method = classObj.getMethod(methodName,classes);
    return method.invoke(obj, valObj);
  }
  /**
   * 打印对象内容
   * @param Object obj 对象实例
   * @throws IllegalAccessException 
   * @throws IllegalArgumentException 
   * */
  public static void dumpTargetMethod(Object obj) throws IllegalArgumentException, IllegalAccessException
  {
    Class<?> classObj = obj.getClass();
    System.out.println("-------------------method dump-------------------------");
    Method[] methods = classObj.getDeclaredMethods();
    for(Method method : methods)
    {
      Class<?>[] parameters = method.getParameterTypes();
      StringBuffer strBuffer = new StringBuffer();
      for(int i=0;i<parameters.length;i++)
      {
        if(i < (parameters.length - 1))
          strBuffer.append(parameters[i].getName() + ",");
        else
          strBuffer.append(parameters[i].getName());
      }
      if(!strBuffer.toString().isEmpty())
      {
        System.out.println("method info (method modifier : " + Modifier.toString(method.getModifiers()) + "  method return type : " + method.getReturnType().getName() + "  method name : " + method.getName() + " parameters : " + strBuffer.toString() + ")");
      }else{
        System.out.println("method info (method modifier : " + Modifier.toString(method.getModifiers()) + "  method return type : " + method.getReturnType().getName() + "  method name : " + method.getName() + " parameters : null)");
      }
    }
    System.out.println("-------------------method dump-------------------------");
    System.out.println("-------------------------------------------------------");
    System.out.println("-------------------fields dump-------------------------");
    Field[] fields = classObj.getDeclaredFields();
    for(Field field : fields)
    {
      //能够访问该字段
      field.setAccessible(true);
      System.out.println("field info (modifier : " + Modifier.toString(field.getModifiers()) + "  type : " + field.getType().getName() + " " + " name : " + field.getName() + " defaultValue : " + field.get(obj).toString() + ")");
    }
  }
}
package Demo;
/**
 * Demo 测试类
 */
public class Demo1 
{
  public String demo1 = "demo1_val";
  protected String demo2 = "demo2_val";
  private String demo3 = "demo3_val";
  public static String demo4 = "demo4_val";
  public String getName()
  {
    return "my name is 勇哥";
  }
  public int getAge()
  {
    return 22;
  }
  public String test1(String str1,String str2)
  {
    StringBuffer buffer = new StringBuffer(str1);
    buffer.append(str2);
    return buffer.toString();
  }
  public String test1()
  {
    return "123";
  }
}

以上就是getClass .class如何在java中使用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI