package com.sysware.adapter.forclient.action;java
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Field;对象
public class Test {继承
@SuppressWarnings("rawtypes")
public static void main(String[] args) {get
// NoodleFactory nf = new NoodleFactory();
// nf.createNoodle(NoodleFactory.GB);
// ProductFactory pf = new ProuctFactoryC();
// pf.getProdut1().show();
// System.out.println("start");
//
// try {
// Class.forName("com.sysware.adapter.forclient.action.User",false,Test.class.getClassLoader());
//
// System.out.println("123");
//
// } catch (Exception e) {
// e.printStackTrace();
// }
try {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//获取类描述符
Class clazz = Class.forName("com.sysware.adapter.forclient.action.Student");
//进行实例化(默认使用空构造的实例化方法)
Object obj = clazz.newInstance();
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//-----------------------------------------------Method---------------------------------------
//经过类描述符获得全部的继承关系下的全部公共方法
Method[] method = clazz.getMethods();
System.out.println("--------getMethods----------");
for(Method m : method){
System.out.println(m.getName());
}
//经过类描述符得该类下的方法
Method[] md = clazz.getDeclaredMethods();
System.out.println("--------getDeclaredMethods----------");
for(Method m : md){
System.out.println(m.getName());
}
//经过类描述符活的指定的方法
Method med = clazz.getMethod("setAge", String.class);
//调用该对象的该方法
med.invoke(obj, "12");
//经过类描述符活的指定的方法
Method medd = clazz.getDeclaredMethod("show",null);
//设置可访问性(因为该方法为私有方法)
medd.setAccessible(true);
medd.invoke(obj, null);
//---------------------------------------------Field----------------------------------------
Field[] field = clazz.getFields();
for(Field f:field){
System.out.println(f.getName());
}
Field[] field2 = clazz.getDeclaredFields();
for(Field f:field2){
System.out.println(f.getName());
}
Field f = clazz.getDeclaredField("age");
f.setAccessible(true);
System.out.println("--"+f.get(obj));
//设置字段的值
f.set(obj, "test");
System.out.println("++"+f.get(obj));
//---------------------------------------------Constructor----------------------------------------
Constructor[] constructor = clazz.getConstructors();
for(Constructor c:constructor){
System.out.println(c.getName());
}
Constructor[] constructor2 = clazz.getDeclaredConstructors();
for(Constructor c:constructor2){
System.out.println(c.getName());
}
//---
Class cl = Class.forName("com.sysware.adapter.forclient.action.Famer");
Constructor c3 = cl.getDeclaredConstructor(String.class);
Object oj = c3.newInstance("2");
Field fkd = cl.getDeclaredField("no");
fkd.setAccessible(true);
System.out.println(fkd.get(oj));
io
} catch (Exception e) {
e.printStackTrace();
}
}
}
class