包路径 getPackage() package对象 获取该类的存放路径
类名称 getName() String 对象 获取该类的名称
继承类 getSuperclass() Class 对象 获取该类继承的类
实现接口 getlnterfaces() Class 型数组 获取该类实现的所有接口
泛型擦除 通过反射来类型擦除,添加元素
获取class对象的三种方法:
// 1.获取Class对象的三种方法
// 方式1:Class.forName("全类名") 频率最高
Class clazz1 = Class.forName("day29_反射.Person");
System.out.println(clazz1);
// 方式2:类名.class 更多的作为参数进行传递
// 内存阶段
Class clazz2 = Person.class;
System.out.println(clazz2);
// 方式3:对象名.getClass() 要求要有类的对象,才可以进行使用、
// 运行阶段
Person p1 = new Person("爪爪");
Class clazz3 = p1.getClass();
System.out.println(clazz3);
Class class1 = Person.class;
// 包路径 getPackage() Package 对象 获取该类的存放路径
Package package1 = class1.getPackage();
System.out.println(package1);// package day30_反射_Class补充
// 类名称 getName() String 对象 获取该类的名称
String className = class1.getName();
System.out.println(className);// day30_反射_Class补充.Person
// 继承类 getSuperclass() Class 对象 获取该类继承的类
Class superclass = class1.getSuperclass();
System.out.println(superclass);// class java.lang.Object
System.out.println("=============");
// 实现接口 getlnterfaces() Class 型数组 获取该类实现的所有接口
Class[] Interfaces = class1.getInterfaces();
for (Class class2 : Interfaces) {
System.out.println(class2);
// interface java.io.Serializable
// interface java.lang.Comparable
// interface java.util.Comparator
}
Person p1 = (Person) class1.newInstance();
System.out.println(p1);
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("爪爪");
// arrayList.add(new Date());
// arrayList.add(123456);
// 通过反射来类型擦除,添加元素
Class class1 = arrayList.getClass();
Method method = class1.getDeclaredMethod("add", Object.class);
method.invoke(arrayList, new Date());
method.invoke(arrayList, 123456);
System.out.println(arrayList);
package day29_反射_class对象;
public class Demo01 {
public static void main(String[] args) throws ClassNotFoundException {
// 1.获取Class对象的三种方法
// 方式1:Class.forName("全类名") 频率最高
// 源码阶段
Class clazz1 = Class.forName("day29_反射.Person");
System.out.println(clazz1);
// 方式2:类名.class 更多的作为参数进行传递
// 内存阶段
Class clazz2 = Person.class;
System.out.println(clazz2);
// 方式3:对象名.getClass() 要求要有类的对象,才可以进行使用、
// 运行阶段
Person p1 = new Person("爪爪");
Class clazz3 = p1.getClass();
System.out.println(clazz3);
System.out.println(clazz1 == clazz2);
System.out.println(clazz2 == clazz3);
}
}
package day29_反射_class对象;
public class Person {
private String name;
public int age;
public Person() {
super();
}
public Person(String name) {
super();
this.name = name;
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
}
获取Field类
Field[] getFields():获取所有的public的field(包括父类)
Field[] getDeclaredFields():获取当前类中所定义的所有field(不包括父类)
Field getField(name):根据字段名获取当前类中某个public的field(包括父类)
Field getDeclaredField(name):根据字段名获取当前类中某个field(不包括父类)
解剖操作:
获取字段的修饰符:
field.getModifiers();
获取字段名称:
field.getName();
获取字段的数据类型:
field.getType();
获取字段的值
field.get(对象);
修改字段的值
field.set(对象,新的成员变量的值);
package day29_反射_成员变量;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class Demo01 {
public static void main(String[] args)
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
// 1.获取Class文件对象
Class class1 = Person.class;
// 2.获取Field类
// Field[] getFields():获取所有的public的field(包括父类)
// Field[] fields1 = class1.getFields();
// for (Field field : fields1) {
// System.out.println(field);
// }
// Field[] getDeclaredFields():获取当前类中所定义的所有field(不包括父类)
// Field[] fields2 = class1.getDeclaredFields();
// for (Field field : fields2) {
// System.out.println(field);
// }
// Field getField(name):根据字段名获取当前类中某个public的field(包括父类)
Field field3 = class1.getField("name");// 就近原则,重名时以当前类为准
System.out.println(field3);
// Field getDeclaredField(name):根据字段名获取当前类中某个field(不包括父类)
Field field = class1.getDeclaredField("age");
System.out.println(field);
// 3.解剖操作
// 获取字段的修饰符
int num = field.getModifiers();
System.out.println(num);
System.out.println(Modifier.isPublic(num));
System.out.println(Modifier.isPrivate(num));
System.out.println(Modifier.isProtected(num));
System.out.println(Modifier.isStatic(num));
System.out.println(Modifier.isFinal(num));
// 获取字段名称
String str = field.getName();
System.out.println("字段名称为" + str);
// 获取字段的数据类型
Class type = field.getType();
System.out.println(type);// int
// Fu p2 = new Person("爪爪", 21, '男');// 向下转型
// System.out.println(p2.name);// 获取的父类的name为null
// field3.setAccessible(true);
// String name = (String) field3.get(p2);
// System.out.println(name);// 子类的变量name为爪爪
// 获取某个对象对应的此字段的值
Person p1 = new Person("爪爪", 21, '男');
// 反射获取私有的成员变量字段的值--访问
field.setAccessible(true);
int age = (Integer) field.get(p1);
System.out.println(age);
// 通过反射修改成员变量的值
field.set(p1, 18);
System.out.println(p1);
}
}
package day29_反射_成员变量;
public class Fu {
private String card;
public String name;//父类的成员变量
private String birthday;
@Override
public String toString() {
return "Fu [card=" + card + ", name=" + name + ", birthday=" + birthday + "]";
}
}
package day29_反射_成员变量;
public class Person extends Fu {
public String name;
private int age;
protected char gender;
public Person(String name, int age, char gender) {
super();
this.name = name;
this.age = age;
this.gender = gender;
}
public Person() {
super();
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
package day29_反射_调用方法;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
public class Demo01 {
public static void main(String[] args) throws NoSuchMethodException, SecurityException {
// 1.获取文件对象
Class class1 = Person.class;
// 2.获取方法对象
// Method[] getMethods():获取所有public的Method(包括父类)
// Method[] methods = class1.getMethods();
// for (Method method : methods) {
// System.out.println(method);
// }
// Method[] getDeclaredMethods():获取当前类的所有Method(不/包括父类)
// Method[] methods = class1.getDeclaredMethods();
// for (Method method : methods) {
// System.out.println(method);
// }
// Method getMethod(name, Class...):获取某个public的Method(包括父类)
// Method method = class1.getMethod("eat", String.class);
// Method method = class1.getMethod("study", String.class);// 拿到的父类的study方法
// System.out.println(method);
// Method getDeclaredMethod(name, Class...):获取当前类的某个Method(不包括父类)
// Method method = class1.getDeclaredMethod("eat", String.class);
// Method method = class1.getDeclaredMethod("study");//错误,当前类没有study方法
// System.out.println(method);
System.out.println("==========");
// 3.解剖
Method method = class1.getDeclaredMethod("eat", String.class, int.class);
// 获取方法的权限修饰符
int num = method.getModifiers();
System.out.println(num);
// 获取方法的名称
String name = method.getName();
System.out.println(name);
// 获取方法的返回值类型
Class returnType = method.getReturnType();
System.out.println(returnType);
// 获取方法的参数
System.out.println("获取方法的参数类型,个数");
Parameter[] parameters = method.getParameters();
for (Parameter parameter : parameters) {
System.out.println(parameter);
}
// 获取方法的异常类型
Class[] exceptionType = method.getExceptionTypes();
for (Class exception : exceptionType) {
System.out.println(exception);
}
}
}
package day29_反射_调用方法;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//通过反射调用方法
public class Demo02 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// 1.获取字节码文件对象
Class class1 = Class.forName("day29_反射_调用方法.Person");
// 2.获取method对应的对象
// Method method = class1.getDeclaredMethod("eat", String.class, int.class);
// // 私有的设置取消校验符
// method.setAccessible(true);
// Person p1 = new Person();
// // 1.对象--实例对象
// // 2.动态参数--调用此方法需要传入的实际的值
// String str = (String) method.invoke(p1, "水煮鱼", 2);
// System.out.println(str);
//静态方法调用
Method method=class1.getDeclaredMethod("eat", String.class);
//1.对象--实例对象
//2.动态参数--调用此方法需要传入的实际的值
method.invoke(null, "水煮鱼");
}
}
package day29_反射_调用方法;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Demo03 {
public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
//获取hello方法
Method method=Father.class.getMethod("hello");
// Father father=new Father();
// method.invoke(father);
Father father=new Zi();
method.invoke(father);
}
}
class Father {
public void hello() {
System.out.println("father:hello");
}
}
class Zi extends Father {
public void hello() {
System.out.println("Zi:hello");
}
}
package day29_反射_调用方法;
public class Fu {
private void study() {
System.out.println("好好学习");
}
public void study(String sth) {
System.out.println("好好学" + sth);
}
}
package day29_反射_调用方法;
import java.io.IOException;
import javax.xml.bind.DataBindingException;
public class Person extends Fu {
private void eat() {
System.out.println("吃饭方法");
}
public static void eat(String sth) {
System.out.println("吃" + sth);
}
public String eat(String sth, int num) throws IOException {
// protected int eat(String sth, int num)
// throws IOException,
// RuntimeException,
// SecurityException,
// ClassCastException,
// DataBindingException {
return "吃" + sth + "已经" + num + "次了";
// return 1;
}
}
package day29_反射_构造方法;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Parameter;
public class Demo01 {
public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// 1.获取Class文件对象
Class class1 = Person.class;
// 2.获取构造方法
// getConstructors():获取所有public修饰的Constructor
// Constructor[] cons1 = class1.getConstructors();
// for (Constructor con : cons1) {
// System.out.println(con);
// }
// getDeclaredContruectors():获取所有定义的Constructor
// Constructor[] cons2 = class1.getDeclaredConstructors();
// for (Constructor con : cons2) {
// System.out.println(con);
// }
// getConstructor(Class...):获取某个public的Constructor:
// Constructor con1 = class1.getConstructor(String.class, int.class);
// System.out.println(con1);
// getDeclaredConstructor(Class...):获取某个定义的Constructor:
Constructor con2 = class1.getDeclaredConstructor(String.class, int.class);
System.out.println(con2);
// 3.解剖使用
// 获取修饰符
int num = con2.getModifiers();
System.out.println(num);
// 获取参数
Parameter[] pars = con2.getParameters();
for (Parameter par : pars) {
System.out.println(par);
}
// 反射创建对象
con2.setAccessible(true);// 临时取消权限修饰符
Person person = (Person) con2.newInstance("爪爪", 18);
System.out.println(person);
}
}
package day29_反射_构造方法;
public class Person {
private String name;
public int age;
protected Person() {
super();
}
private Person(String name) {
super();
this.name = name;
}
private Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
package day29_反射_构造方法;
public class Person {
private String name;
public int age;
protected Person() {
super();
}
private Person(String name) {
super();
this.name = name;
}
private Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
properties文件
classname=homework.Teacher
method=teach
package day30_反射_Class补充;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
public class Demo03_result {
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, IOException {
Object object = new Teacher("爪爪", "英语");
// Object object = new Student("爪爪", 30, '男', 163.6, "唱、跳、rap、篮球");
saveObject(object);
}
// 对于任意一个对象,都可以将对象中的字段名和字段值保存到文件
private static void saveObject(Object object) throws IllegalArgumentException, IllegalAccessException, IOException {
// 1.获取字节码文件对象
Class class1 = object.getClass();
// 2.获取成员变量
Field[] fields = class1.getDeclaredFields();
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
for (Field field : fields) {
field.setAccessible(true);
// 获取字段名
String fieldName = field.getName();
// 获取字段值
// 获取字段的值,返回值为Object类型的值
Object value = field.get(object);
bw.write(fieldName + ":" + value);
bw.newLine();
}
bw.close();
}
}
package day30_反射_Class补充;
public class Student {
private String name;
private int age;
private char gender;
private double height;
private String hobby;
public Student(String name, int age, char gender, double height, String hobby) {
super();
this.name = name;
this.age = age;
this.gender = gender;
this.height = height;
this.hobby = hobby;
}
public Student() {
super();
}
public void study() {
System.out.println("good good study");
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", gender=" + gender + ", height=" + height + ", hobby="
+ hobby + "]";
}
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 char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
}
package day30_反射_Class补充;
public class Teacher {
private String name;
private String subject;
public void teach() {
System.out.println("teach java");
}
public Teacher() {
super();
}
public Teacher(String name, String subject) {
super();
this.name = name;
this.subject = subject;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
@Override
public String toString() {
return "Teacher [name=" + name + ", subject=" + subject + "]";
}
}