定义Student 类:包含:姓名和年龄等属性,有参和无参构造方法,输出所有信息的方法
1.使用多种方法生成一个Student类的Class对象
2.使用Class类获取Student类的结构信息并输出
3.通过有参(无参)构造方法动态创建Student类的对象
4.使用反射修改和查询Student类的姓名属性
5.使用反射动态执行Student类输出信息的方法
代码:
Student类
package 敲代码;
//定义Student 类,包含:姓名和年龄等属性,有参和无参构造方法,输出所有信息的方法
public class Student {
private String name;
private int age;
private String sex;
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
public Student(){}
public Student(String name,int age,String sex){
this.name=name;
this.age=age;
this.sex=sex;
}
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void eat(){
System.out.println("eat apple!");
}
private void run(){
System.out.println("run and listen to music!");
}
public static void play(){
System.out.println("play game");
}
}
测试类:
package 敲代码;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) {
//1.获取类对象
//first
Student student=new Student();
Class clazz=student.getClass();
System.out.println(clazz.hashCode());
//second
Class clazz2=Student.class;
System.out.println(clazz2.hashCode());
//third
try {
Class clazz3=Class.forName("敲代码.Student");
System.out.println(clazz3.hashCode());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.使用Class类获取Student类的结构信息并输出
try {
//构造方法
Constructor[] constructors=clazz.getDeclaredConstructors();
for (Constructor constructor2 : constructors) {
System.out.println(constructor2);
}
//获取方法对象
Method[] methods=clazz.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method);
}
//获取属性
Field[] fields=clazz.getDeclaredFields();
for (Field field : fields) {
System.out.println(field);
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//3.通过有参(无参)构造方法动态创建Student类的对象
try {
Constructor<Student> constructor=clazz2.getConstructor(String.class,int.class,String.class);
Student student2=constructor.newInstance("张三",18,"张三");
System.out.println(student2);
//4.使用反射修改和查询Student类的姓名属性
student2.setName("李四");
System.out.println(student2.getName());
//5.使用反射动态执行Student类输出信息的方法
Method method=clazz.getMethod("eat", null);
method.invoke(student, null);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果: