package step1;
/**
* 学员任务文件
*/
public class Reflect_stu {
public static void main(String[] args) {
System.out.println("通过Object 类中的 getClass() 获取的 Class 对象为:" + getPersonClass1());
System.out.println("通过静态方法 Class.forName() 获取的 Class 对象为:" + getPersonClass2());
System.out.println("通过类字面常量获取 Class 的对象为:" + getPersonClass3());
}
/**
* 通过 Object 类中的 getClass() 获取的 Class 对象
*
* @return
*/
public static Class getPersonClass1() {
/********** Begin *********/
Person p=new Person();
Class c1=p.getClass();
return c1;
/********** End *********/
}
/**
* 通过静态方法 Class.forName() 获取的 Class 对象
*
* 注意:Person 类的全路径为: step1.Person
*
* @return
*/
public static Class getPersonClass2() {
/********** Begin *********/
String classname="step1.Person";
Class c2=null;
try{
c2=Class.forName(classname);
}catch(ClassNotFoundException e)
{
}
return c2;
/********** End *********/
}
/**
* 通过类字面常量获取 Class 的对象
*
* @return
*/
public static Class getPersonClass3() {
/********** Begin *********/
Class c3=Person.class;
return c3;
/********** End *********/
}
}
package step2;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
class Apple {
private String name;
public Apple(){}
public Apple(String name){}
public void setName(String name) {
this.name = name;
}
}
public class Reflect_stu {
public static void main(String[] args) {
// 请根据提供的 classPath 获取 step2.Apple 的 Class 对象, 请使用 Class.forName() 方法, 注意捕获异常
// 通关之后,你也可以修改 clasapath 为其他类路径,分析某个类的能力, 例如: java.util.Date
String classPath = "step2.Apple";
Class clazz = null;
/********** Begin *********/
try
{
clazz=Class.forName(classPath);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
/********** End *********/
printFields(clazz);
printConstructors(clazz);
printMethods(clazz);
}
/**
* 请打印类的每个域,输出格式为:修饰符 类型 变量名;
* @param clazz
*/
public static void printFields(Class clazz) {
/********** Begin *********/
try{
Field[] fields=clazz.getDeclaredFields();
for(Field field:fields)
{
System.out.print(Modifier.toString(field.getModifiers())+" ");
System.out.print(field.getType().getTypeName()+" ");
System.out.println(field.getName()+";");
}
}catch(Exception e)
{
e.printStackTrace();
}
/********** End *********/
}
/**
* 打印构造函数,输出格式为:修饰符 方法名称(参数)
* @param clazz
*/
public static void printConstructors(Class clazz) {
Constructor[] constructors = clazz.getDeclaredConstructors();
for (Constructor constructor : constructors) {
Class[] paramTypes = null;
/********** Begin *********/
paramTypes=constructor.getParameterTypes();
System.out.print(Modifier.toString(constructor.getModifiers())+" ");
System.out.print(constructor.getName()+"(");
/********** End *********/
printParamTypes(paramTypes);
}
}
/**
* 请针对每个方法打印其签名,格式为:修饰符 返回值类型 方法名称(参数);
* @param clazz
*/
public static void printMethods(Class clazz) {
Method[] methos = clazz.getDeclaredMethods();
for (Method method : methos) {
Class[] paramTypes = null;
/********** Begin *********/
paramTypes=method.getParameterTypes();
System.out.print(Modifier.toString(method.getModifiers()) + " " + method.getReturnType().getName() + " "+ method.getName() + "(");
/********** End *********/
printParamTypes(paramTypes);
}
}
/**
* 打印方法参数
* @param paramTypes
*/
private static void printParamTypes(Class[] paramTypes) {
for (int j = 0; j < paramTypes.length; ++j) {
if (j > 0) {
System.out.print(",");
}
System.out.print(paramTypes[j].getName());
}
System.out.println(");");
}
}
package step3;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class Reflect_stu {
public static String toString(Object obj) {
Class cl = obj.getClass();
String r = "";
r += "[";
// 请获取所有 Field 并设置访问权限为 true
/********** Begin *********/
Field[] fields = null;
fields=cl.getDeclaredFields();
AccessibleObject.setAccessible(fields,true);
/********** End *********/
for (Field f : fields) {
// 此处 if,逻辑为判断 Field 域是否为非静态域
if (!Modifier.isStatic(f.getModifiers())) {
if (!r.endsWith("[")) r += ",";
r += f.getName() + "=";
try {
// 请获取域的类型及值
/********** Begin *********/
Class t = null;
Object val = null;
t=f.getType();
val=f.get(obj);
/********** End *********/
// isPrimitive() 用于判断是否为基本数据类型,若为基础数据类型直接拼接,否则递归调用 toString 方法
if (t.isPrimitive()) r += val;
else r += toString(val);
} catch (Exception e) {
e.printStackTrace();
}
}
}
r += "]";
return r;
}
public static void main(String[] args) {
Person person = new Person(88, 19, 175);
System.out.println(toString(person));
}
}
class Person {
public Integer weight;
private Integer age;
private Double height;
public Person(Integer weight, Integer age, double height) {
this.weight = weight;
this.age = age;
this.height = height;
}
}
package step4;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Reflect_stu {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws InvocationTargetException {
//使用反射调用
Class clazz = null;
try {
clazz = Class.forName("step4.Apple");
/********** Begin *********/
Method setPriceMethod = clazz.getMethod("setPrice", double.class);
Constructor appleConstructor = clazz.getConstructor();
Object apple = appleConstructor.newInstance();
setPriceMethod.invoke(apple, 14);
Method getPriceMethod = clazz.getMethod("getPrice");
System.out.println(getPriceMethod.invoke(apple));
Method getTotal = clazz.getMethod("getTotal", double.class, int.class);
System.out.println(getTotal.invoke(apple, 20, 24));
/********** End *********/
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Apple {
private double price;
private int count;
public Apple() {
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public double getTotal(double price, int count) {
return price * count;
}
}