package com.at221;

import java.io.Serializable;
import java.lang.reflect.*; import org.junit.Test; public class TestReflection {
@Test
public void test1() throws Exception{
Class<?> class1 = null;
Class<?> class2 = null;
Class<?> class3 = null;
//对象实例化的第一种方式:
class1 = Class.forName("com.at221.Person");
// Person person = (Person)class1.newInstance(); //对象实例化的第二种方式: class2 = new Person().getClass(); //第三种对象实例化的方式: class3 = Person.class; System.out.println(class1);
System.out.println(class2);
System.out.println(class3); } /*运行结果:
* class com.at221.Person
class com.at221.Person
class com.at221.Person *
*/ //获取类的父类和接口:
@Test
public void test2() throws Exception{
Class<?> class1 = Class.forName("com.at221.Person"); Class<?> parentclass = class1.getSuperclass();
System.out.println(parentclass.getName()); System.out.println("----------------"); Class<?> inte[] = class1.getInterfaces();
for(int i = 0; i < inte.length; i++){
System.out.println(inte[i].getName());
}
}
/*
* 运行结果:
* com.at221.Creture
----------------
java.io.Serializable */ //获取类的属性和属性的作用域和类型:
@Test
public void test3(){
Class<?> class2 = new Person().getClass();
Field[] fields = class2.getDeclaredFields(); for(int i = 0; i < fields.length; i++){
Class<?> types = fields[i].getType();
int mo = fields[i].getModifiers();
System.out.println(Modifier.toString(mo) + " " +
types.getName() + " " + fields[i].getName() + "\n");
}
/*运行结果:
* private static final long serialVersionUID private java.lang.String name private int age private java.lang.String nation
*
*/ //获取构造器的各种信息: Class<?> class3 = new Person().getClass();
Constructor<?> construtor[] = class3.getDeclaredConstructors(); for(int i = 0; i < construtor.length; i++){
int mo = construtor[i].getModifiers();
Class<?> types[] = construtor[i].getParameterTypes();
System.out.println(Modifier.toString(mo) + " " + construtor[i].getName());
System.out.println(i + ": ");
for(int j = 0; j < types.length; j++){
System.out.print(types[j].getName() + " ");
}
System.out.println("--------------");
}
/*运行结果:
* public com.at221.Person
0:
--------------
public com.at221.Person
1:
java.lang.String int java.lang.String --------------
*
*/
} @Test
public void test4(){
//获取类中的方法:
Class<?> class4 = Person.class;
Method methods[] = class4.getDeclaredMethods();
for(int i = 0; i < methods.length; i++){ int mod = methods[i].getModifiers();//获取函数的作用域;
Class<?> returnTypes = methods[i].getReturnType();//q就函数的返回值; System.out.print("method[" + i + "] :" + Modifier.toString(mod) + " " +
" " + returnTypes.getName() + " " + methods[i].getName() + " ( ");
Class<?> parameterType[] = methods[i].getParameterTypes();//获取函数的形参类型;
for(int j = 0; j < parameterType.length; j++){
System.out.print(parameterType[j].getName() + " ");
}
System.out.println(")"); }
}
/*运行结果:
* method[0] :public java.lang.String toString ( )
method[1] :public java.lang.String getName ( )
method[2] :public void setName ( java.lang.String )
method[3] :public int getAge ( )
method[4] :public void setAge ( int )
method[5] :public java.lang.String getNation ( )
method[6] :public void setNation ( java.lang.String ) *
*/
} class Person extends Creture implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private int age;
private String nation; public Person() {
super();
} public Person(String name, int age, String nation) {
super();
this.name = name;
this.age = age;
this.nation = nation;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getNation() {
return nation;
} public void setNation(String nation) {
this.nation = nation;
} @Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", nation=" + nation + "]";
}
} class Creture{
public Creture(){
super();
} public void show(){
System.out.println("!!!i am a creture!!!");
}
}

最新文章

  1. postgresql 设置只读用户
  2. GridView,Repeater增加自动序号列
  3. log4j.properties 详解与配置步骤
  4. 不错的开源FTP类库
  5. 转载:简化IT程序员工作生活的4个窍门
  6. Self-numbers 2 - SGU 108
  7. [TYVJ] P1005 采药
  8. Zend Framework 框架搭建
  9. 为部署ASP.NET Core准备:使用Hyper-V安装Ubuntu Server 16.10
  10. Myeclipse按包装SVN
  11. win7系统下dos界面无法自由调整大小
  12. Mac 下安装nvm 后vscode 输入node -v 不起作用
  13. EChart.js 笔记二
  14. 树链剖分模板(洛谷P3384)
  15. HTML5:链接与路径
  16. lgwr的两种模式(post/wait和polling)
  17. IMU 预积分推导
  18. Windows 下 Redis 服务无法启动,错误 1067 进程意外终止解决方案
  19. 关闭R语言载入包时候的警告
  20. linux下使用pip在虚拟环境下安装tensorflow-gpu

热门文章

  1. 外网訪问XAMPP失败 解决方式 XAMPP1.8.3
  2. oracle sql语句实现累加、累减、累乘、累除
  3. [hadoop] kettle spoon 基础使用 (txt 内容抽取到excel中)
  4. 网络编程 -- RPC实现原理 -- 目录
  5. Appium Server
  6. 为IONIC开发的安卓apk签名
  7. hdu2074 叠筐
  8. react 使用 lazyload 懒加载图片
  9. Strassen 矩阵相乘算法(转)
  10. 19. vue的原理