加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
反射 5.95 KB
一键复制 编辑 原始数据 按行查看 历史
鸢也12123 提交于 2021-11-26 06:28 . add 反射.
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class Cat {
//私有属性name
private String name = "bit";
//公有属性age
public int age = 18;
//不带参数的构造方法
public Cat() {
System.out.println("Student()");
}
private Cat(String name, int age) {
this.name = name;
this.age = age;
System.out.println("Student(String,name)");
}
private void eat() {
System.out.println("i am eat");
}
public void sleep() {
System.out.println("i am pig");
}
private void function(String str) {
System.out.println(str);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class TestReflection {
//创建对象
public static void reflectNewInstanse() {
Class<?> c1=null;
try {
c1=Class.forName("练习.Cat");
//Student student=new Student();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Class<?> c2= Cat.class;
Cat student=new Cat();
Class<?> c3=student.getClass();
//System.out.println(c1.equals(c2));
}
// 反射私有的构造方法 屏蔽内容为获得公有的构造方法
public static void reflectPrivateConstrcter() {
Class<?> c1=null;
try {
c1=Class.forName("练习.Cat");
Cat student=(Cat) c1.newInstance();
//获得private方法权限
Constructor<?> constructor= c1.getDeclaredConstructor(String.class,int.class);
constructor.setAccessible(true);
//可以用方法调用了
Cat student1=(Cat) constructor.newInstance("yuanye",18);
System.out.println(student1);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
// 反射私有属性
public static void reflectPrivateField() {
Class<?> c1=null;
try {
c1=Class.forName("练习.Cat");
//开权限
Field field=c1.getDeclaredField("name");
field.setAccessible(true);
Cat student=(Cat) c1.newInstance();
//field.set(student,"yuanye");//要改里面的参数就用set方法改
System.out.println(student);
String str=(String)field.get(student);
System.out.println(str);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
// 反射私有方法
public static void reflectPriveatMethod(){
Class<?> c1=null;
try {
c1=Class.forName("练习.Cat");
Method method=c1.getDeclaredMethod("function", String.class);
method.setAccessible(true);
Cat student=(Cat)c1.newInstance();
System.out.println(student);
method.invoke(student,"func是一个参数");
} catch (ClassNotFoundException | NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
reflectNewInstanse();
reflectPrivateConstrcter();
System.out.println("===========================");
reflectPrivateField();
reflectPriveatMethod();
}
public static void main1(String[] args) {
Class<?> c1=null;
//1、获得class的对象的student类
try {
c1=Class.forName("练习.Cat");//用的最多的
//Student student=new Student();
Cat student=(Cat) c1.newInstance();//!!!!!!!!!
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
//2、获得class的对象的student类
Class<?> c2= Cat.class;
//3.获得class的对象的student类
Cat student=new Cat();
Class<?> c3=student.getClass();
System.out.println(c1.equals(c2));
System.out.println(c1==c3);
System.out.println(c2==c3);
}
// 反射私有的构造方法 屏蔽内容为获得公有的构造方法
public static void reflectPrivateConstructor() throws NoSuchMethodException {
Class<?> c1=null;
try {
c1=Class.forName("练习.Cat");
//Student student=new Student();
Cat student=(Cat) c1.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Constructor<?> constructor= c1.getDeclaredConstructor(String.class,int.class);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化