简单原理介绍java反射(2)
来源:互联网 发布:硬盘数据恢复软件排名 编辑:程序博客网 时间:2024/06/05 22:58
public class Person {private String name;private int age;public Person(String name,int age) {super();this.name = name;this.age = age;}public Person(){}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;}@Overridepublic String toString() {return "Person [name=" + name + ", age=" + age + "]";}public void show(){System.out.println("show");}public void fun(String str){System.out.println(str);}public static void function(){System.out.println("static");}}
public class Demo1 {/** * 反射:动态获取字节码文件对象(Person.class),并对其成员进行运行 * * 动态获取字节码文件对象的方式: * 1:任何一个对象都是由字节码文件对象创建的,所以任何一个对象都可以得到自己的字节码文件对象 * 那么这个功能应该定义在Object中,所以使用 getClass() * 需要先new对象 * * 2:每种数据类型都有一个 静态的class 属性,通过该属性可以得到字节码文件对象 * 不需要new对象,但是需要Person类存在 * * 3:Class类提供了一个静态的forName(String str)方法 * 只需要提供字符串形式的包名+类名 * @throws ClassNotFoundException */public static void main(String[] args) throws ClassNotFoundException { getClaz(); getClaz2(); getClaz3();}//Class类提供了一个静态的forNamepublic static void getClaz3() throws ClassNotFoundException{Class<?> claz1 = Class.forName("com.qianfeng.reflect.Person");Class<?> claz2 = Class.forName("com.qianfeng.reflect.Person");System.out.println(claz1==claz2);}//通过静态属性class获取字节码文件对象public static void getClaz2(){Class<Person> claz1 = Person.class;Class<Person> claz2 = Person.class;System.out.println(claz1==claz2);}//1:使用Object中的 getClass()获取字节码文件对象public static void getClaz(){//Person person1 = new Person();//先加载Person.class到方法区//Class<? extends Person> claz1 = person1.getClass();//得到了 Person.class////Person person2 = new Person();//Class<? extends Person> claz2 = person2.getClass();//得到了 Person.class////System.out.println(claz1==claz2);//true}}
import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;public class Demo2 {/** * 动态获取字节码文件对象,并创建对象 * @throws IllegalAccessException * @throws InstantiationException * @throws ClassNotFoundException * @throws InvocationTargetException * @throws IllegalArgumentException * @throws SecurityException * @throws NoSuchMethodException */public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {createObj1();createObj2();}//使用带参数的构造方法创建对象public static void createObj2() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ //Person person = new Person("小红",20);//获取字节码文件对象---Person.classClass<?> claz = Class.forName("com.qianfeng.reflect.Person");//得到构造方法所属的Constructor类型的对象Constructor<?> constructor = claz.getConstructor(String.class,int.class);//使用构造方法创建对象--使用Constructor提供的创建对象的功能Person person =(Person)constructor.newInstance("小红",20);System.out.println(person);}//当空参的构造方法不存在时,会发生InstantiationException//当构造方法的权限过低时,会发生IllegalAccessExceptionpublic static void createObj1() throws ClassNotFoundException, InstantiationException, IllegalAccessException{//Person person = new Person();//获取字节码文件对象---Person.classClass<?> claz = Class.forName("com.qianfeng.reflect.Person");//使用Class提供的newInstance()方法创建Person类型的对象Object obj = claz.newInstance();//使用空参的构造方法创建对象Person person = (Person)obj;System.out.println(person);}}
import java.lang.reflect.Field;public class Demo3 {/** * 动态创建对象并给属性赋值 * @throws ClassNotFoundException * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalAccessException * @throws InstantiationException */public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException, InstantiationException, IllegalAccessException {//Person person = new Person();//person.setName("小红");//获取字节码文件对象---Person.class Class<?> claz = Class.forName("com.qianfeng.reflect.Person"); //得到属性name所属的 Field类型的对象 //Field field = claz.getField("name");//只能获取权限是public 的属性 //System.out.println(field);//NoSuchFieldException Field field = claz.getDeclaredField("name"); //因为name是非静态属性,所以必须通过对象访问,所以先创建对象 Object obj = claz.newInstance(); //设置name属性为可访问的 field.setAccessible(true);// 该方法是从父类中继承的 //使用Field类提供的赋值功能,给属性赋值 field.set(obj, "小红"); System.out.println(obj); }}
import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class Demo4 {/** * 动态创建对象,并调用方法 * @throws InvocationTargetException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InstantiationException * @throws SecurityException * @throws NoSuchMethodException * @throws ClassNotFoundException */public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {method1();method2();method3();}//调用静态方法public static void method3() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{//获取字节码文件对象---Person.class Class<?> claz = Class.forName("com.qianfeng.reflect.Person"); ///获取被调用的方法所属的 Method类型的对象 Method method = claz.getMethod("function", null); //执行方法 method.invoke(null, null); }//调用带参的构造方法public static void method2() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{//获取字节码文件对象---Person.class Class<?> claz = Class.forName("com.qianfeng.reflect.Person"); //获取被调用的方法所属的 Method类型的对象 Method method = claz.getMethod("fun", String.class); //fun()属于非静态方式,需要对象去调用 Object obj = claz.newInstance(); //执行方法 method.invoke(obj, "hello");}//调用无参的构造方法public static void method1() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{// Person person = new Person();person.show();//获取字节码文件对象---Person.class Class<?> claz = Class.forName("com.qianfeng.reflect.Person"); //获取被调用的方法所属的 Method类型的对象 Method method = claz.getMethod("show", null); //show()属于非静态方式,需要对象去调用 Object obj = claz.newInstance(); //执行方法 method.invoke(obj, null); }}
0 0
- 简单原理介绍java反射(2)
- java反射简单介绍
- 简单原理来介绍java编程之反射
- Java 反射学习(四)反射技术开发框架原理简单实现
- JAVA-反射-getGenericSuperclass()介绍
- JAVA-反射-GETGENERICSUPERCLASS()介绍
- JAVA-反射-getGenericSuperclass()介绍
- JAVA-反射-GETGENERICSUPERCLASS()介绍
- JAVA-反射-GETGENERICSUPERCLASS()介绍
- JAVA-反射-getGenericSuperclass()介绍
- 内省 + 反射(简单应用实现原理)
- 反射简单介绍
- C#反射简单介绍
- 反射的简单介绍
- 反射的简单介绍
- Java反射机制的原理及在Android下的简单应用2
- Java反射机制的原理与简单使用
- java反射机制的原理与简单使用
- java_Collection_介绍
- 2015 Multi-University Training Contest 3
- UML——包图
- linux进程环境及进程属性
- 黑马程序员-OC-继承
- 简单原理介绍java反射(2)
- PAT的C++编译器
- 第⼋八讲 :UINavigationController、界⾯面通信
- Accessibilityservice(辅助服务开发)学习笔记
- 疯狂Android讲义第二讲
- There is a war (hdu 2435 最小割+枚举)
- ioc笔记
- 2015 Multi-University Training Contest 4
- 华为OJ(查找兄弟单词)