public class People {
private String name;
private int age;
public People(String name, int age) {
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void speak() {
System.out.println(getName() + " " + getAge());
}
}
public class Student extends People {
private int grade;
public Student(String name, int age) {
super(name, age);
}
public Student(String name, int age, int grade) {
super(name, age);
this.grade = grade;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
private void learn(String course) {
System.out.println(name + " learn " + course);
}
}
Class<People> peopleClass = People.class;
//假设People类在com.test包中
Class<People> peopleClass = Class.forName("com.test.People");
People people = new People("Bill", 18);
Class<People> peopleClass = people.getClass();
public static void main(String[] args) {
Class<People> pClass = People.class;
try {
Constructor<People> constructor = pClass.getConstructor(String.class, int.class);
People people = constructor.newInstance("Bill", 18);
obj.speak();
} catch (Exception e) {
}
}
Constructor<People> constructor = peopleClass.getConstructor(String.class, int.class); // 设置 constructor 的 Accessible属性为ture以取消Java的访问检查 constructor.setAccessible(true);
private static void showDeclaredMethods() {
Student student = new Student("Bill", 18);
//获取Student类声明的所有方法
Method[] methods = student.getClass().getDeclaredMethods();
try {
//获取learnMethod对象(封装了learn方法)
Method learnMethod = student.getClass().getDeclaredMethod("learn",
String.class);
//获取learn方法的参数列表并打印出来
Class<?>[] paramClasses = learnMethod.getParameterTypes() ;
for (Class<?> class : paramClasses) {
System.out.println("learn方法的参数: " + class.getName());
}
//判断learn方法是否为private
System.out.println(learnMethod.getName() + " is private "
+ Modifier.isPrivate(learnMethod.getModifiers()));
//调用learn方法
learnMethod.invoke(student, "Java Reflection");
} catch (Exception e) {
}
}
private static void showMethods() {
Student student = new Student("mr.simple");
// 获取所有public方法(包括Student本身的和从父类继承来的)
Method[] methods = student.getClass().getMethods();
try {
//注意,通过 getMethod只能获取public方法,若尝试获取private方法则会抛出异常
Method learnMethod = student.getClass().getMethod("learn", String.class);
} catch (Exception e) {
}
}
private static void showDeclaredFields() {
Student student = new Student("Bill", 18);
// 获取当前类中定义的所有属性
Field[] fields = student.getClass().getDeclaredFields();
try {
// 获取指定的属性
Field gradeField = student.getClass().getDeclaredField("grade");
// 获取属性值
System.out.println("The grade is : " + gradeField.getInt(student));
// 设置属性值
gradeField.set(student, 10);
} catch (Exception e) {
}
}
private static void showFields() {
Student student = new Student("Bill", 18);
// 获取当前类和父类的所有public属性
Field[] publicFields = student.getClass().getFields();
}
Student student = new Student("Bill", 18);
Class<?> superClass = student.getClass().getSuperclass();
private static void showInterfaces() {
Student student = new Student("Bill", 19);
Class<?>[] interfaces = student.getClass().getInterfaces();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有