反射机制属性,方法,使用的代码描述

反射机制

反射机制:

在运行时,我们可以动态的获取类中的信息;在运行期间可以实例化对象,调用方法属性等等。

1.反射机制直接直接操作字节码文件,获取字节码对象

下面展示一些 内联代码片


     class DogF {
    	public String hhh;
    	protected int xxx;
    }
    
    class Dog extends DogF{
    	private String name;
    	public Integer age;
    	protected int height;
    	public static double aa;
    	
    	public  Dog() {
    		
    	}
    	private Dog(String name) {
    		
    	}
    	
    	public void aa() {
    		System.out.println("aa方法");
    	}
    	private void bb(int x) {
    		System.out.println("x = " + x);
    	} 	
    	public static void ss() {		
    	}
    }
      class DogF {
    	public String hhh;
    	protected int xxx;
    }
    
    class Dog extends DogF{
    	private String name;
    	public Integer age;
    	protected int height;
    	public static double aa;
    	
    	public  Dog() {
    		
    	}
    	private Dog(String name) {
    		
    	}
    	
    	public void aa() {
    		System.out.println("aa方法");
    	}
    	private void bb(int x) {
    		System.out.println("x = " + x);
    	} 	
    	public static void ss() {		
    	}
    }
反射机制:在运行时,我们可以动态的获取类中的信息; 在运行期间可以实例化对象、调用方法、属性等等。

下面展示一些 内联代码片

   public class MyReflect {
    
 public static void main(String[] args) throws ClassNotFoundException, 

NoSuchFieldException, SecurityException, NoSuchMethodException {
1.反射机制直接操作的字节码文件,获取字节码对象
Class class1 = Dog.class;
		 Class class2 = Class.forName("yandaCollection1.Dog");
 		 Class class3 = new Dog().getClass();
 		 
 
//获取类中的信息
 		Class class1 = Dog.class;
 		//获取类中定义的属性
 		//只能获取被public修饰的,还有从父类继承而来的
 		Field[] fields = class1.getFields();
 		//本类中定义的所有的属性
 		Field[] fields = class1.getDeclaredFields();
 		for (Field field : fields) {
 			System.out.println(field);
 		}
//获取指定的属性对象
 	Field ageF = class1.getField("name");
 	Field ageF = class1.getDeclaredField("name");
 	System.out.println(ageF);
//获取构造方法信息
   		Constructor[] constructors = 
 		class1.getConstructors();
  		Constructor[] constructors = 
  				class1.getDeclaredConstructors();
 		 for (Constructor constructor : constructors) {
   		System.out.println(constructor);
   	}
 		//获取指定的构造方法
  		Constructor constructor = 
 				class1.getConstructor();
 		System.out.println(constructor);
  
  	Constructor constructor2 = 
  				class1.getDeclaredConstructor(String.class);
 		System.out.println(constructor2);
 		//获取方法的信息
 		Method[] methods = class1.getMethods();
 		Method[] methods = class1.getDeclaredMethods();
		for (Method method : methods) {
 			System.out.println(method);
		}
 	Method method = class1.getMethod("aa");
 		System.out.println(method);
 		Method method2 = class1.getDeclaredMethod("bb", int.class);
 		System.out.println(method2);
 	}
反射机制使用
public static void main(String[] args) throws InstantiationException, 
IllegalAccessException, NoSuchMethodException, SecurityException, 
IllegalArgumentException, InvocationTargetException, 
NoSuchFieldException {
  		Class class1 = Dog.class;
  		//使用无参的构造方法创建对象
  		Object obj = class1.newInstance();
  		System.out.println(obj);
  		//使用构造方法对象 创建 当前实例
  		Constructor constructor = 
  				class1.getDeclaredConstructor(String.class);
  		 new Dog("aa");
  		使用非public的构造方法,需要授权
  		constructor.setAccessible(true);
  		Object dog = constructor.newInstance("aa");
  		System.out.println(dog);
 		Field name = class1.getDeclaredField("name");
 		name.setAccessible(true);
 		//为dog对象的name属性赋值为张三
 		//如果是静态变量,第一参数设置为null
 		name.set(dog, "张三");
 		//获取dog对象的name属性的值
 		System.out.println(name.get(dog));
 		
 		Method aa = class1.getMethod("aa");
 		//调用了dog对象的aa方法
 		aa.invoke(dog);
 		
 		Method bb = class1.getDeclaredMethod("bb", int.class);
 		bb.setAccessible(true);
 		//静态方法将第一个参数设置为null
 		bb.invoke(dog, 10);
 	}
 
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值