反射学习01

1、反射机制是程序在编写或者运行状态中对于任意一个类的属性和方法的操作。

2、反射弄够动态的获取类中的信息,对于任意一个类可以获得它的所有属性。

3、可以将反射机制理解为对于类的解刨。

4、在成型的应用程序中,不改变程序的代码结构,加入自己想要的独特功能。

5、以前的设计接口,实现接口,的多态方式,虽然在一定程度上提高了程序的可扩展性,但是没有达到最佳状态,最佳状态就是通过反射实现的。

6、反射可以获得类中的属性,方法,构造方法

举例说明:

package 反射;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
class Persons {
private String name;
private int age;

public Persons() {
super();
// TODO 自动生成的构造函数存根
}
public Persons(String name) {
super();
this.name = name;
}
public Persons(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;
}
public void show() {
System.out.println("公共方法1");
}
public void show1(String string) {
System.out.println("公共方法2" + string);
}


private void show2() {
System.out.println("私有方法1");
}
private void show3() {
System.out.println("私有方法2");
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}

public class ReflectDemo08 {
public static void main(String[] args) throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
NoSuchMethodException, SecurityException, IllegalArgumentException,
InvocationTargetException, NoSuchFieldException {
/*
* 创建字节码对象的三种方式。
*/
// 1:利用每个类型的class属性
Class class1 = Persons.class;
// 2,利用Object对象的getClass()对象
Persons person = new Persons();
Class class2 = person.getClass();
// 3 非常提倡的方法。利用Class类的静态方法。。
Class class3 = Class.forName("反射.Persons");
System.out.println(class1 == class2);
System.out.println(class2 == class3);
/*
* 利用反射创建对象
*/


// 这个创建对象的过程是:首先根据new的名称找到字节码文件,然后创建字节码文件对象,然后创建字节码对应的Persons对象。
Persons p = new Persons();
p.setName("zbx");
p.setAge(27);
System.out.println(p.toString());
// //
Persons pp = new Persons("zzbbxx", 27);
System.out.println(pp.toString());
/*
* 利用反射创建对象
*/
// 这个创建对象的过程是:首先根据名称找到字节码文件,然后创建字节码文件对象,然后创建构造方法没有任何参数的对象。
Class class4 = Class.forName("反射.Persons");
Persons p1 = (Persons) class4.newInstance();
System.out.println(p1.toString());
// 创建构造函数有参数的对象
Class class5 = Class.forName("反射.Persons");
Constructor c = class5.getConstructor(String.class, int.class);
Persons pp1 = (Persons) c.newInstance("zzzbbbxxx", 27);
System.out.println(pp1.toString());
/*
* 通过反射调用变量
*/
Class class6 = Class.forName("反射.Persons");
Field name = class6.getDeclaredField("name");
Field age = class6.getDeclaredField("age");
// 对私有对象取消私有化
System.out.println(name);
System.out.println(age);
System.out.println("------------------------------------------");
Persons p3 = (Persons) class6.newInstance();
p3.setAge(27);
p3.setName("zzzzbbbbxxx");
// 对私有对象取消私有化
name.setAccessible(true);
String str_name = (String) name.get(p3);
System.out.println(str_name);
// 同样的age的方法方式和name的方法方式完全一样。。
/*
* 通过反射调用方法
*/
Class class7 = Class.forName("反射.Persons");
// Method []methods=class7.getMethods();
Method[] methods = class7.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method);
}


// 输出具体的对象的内容
// 1,调用的是没有参数的函数
Class class8 = Class.forName("反射.Persons");
Method method = class8.getMethod("show", null);
Persons p4 = (Persons) class8.newInstance();
p4.setAge(27);
p4.setName("zzzzzbbbbbxxxxx");
method.invoke(p4, null);


// 2,调用的是有参数的函数
Class class9 = Class.forName("反射.Persons");
Method method1 = class9.getMethod("show1", String.class);
Persons p5 = (Persons) class9.newInstance();
method1.invoke(p5,"aaa");
}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值