反射机制整理

反射机制是在运行状态中,对于任意各类,都能够知道这个类的所有属性和方法;

对于任意一个对象,都能够调用它的任意一个方法和属性,这种动态获取信息以及动态调用对象的功能,称为java语言的反射机制。

获取字节码文件的方式:

方式一:

Object类中的getClass方法,想要用这些方式,必须要明确具体的类,并创建对象。

方式二:

任何数据类型都具备一个静态的属性:.class

方式三:

只要通过给定的类的字符串名称就可以获取该类,更为扩展

可用class类完成,方法时forName();

public static void main(String[] args) throws ClassNotFoundException {
		getClass_1();
		getClass_2();
		getClass_3();
	}
	//通过Object类中的getClass方法,想要用这个方法,必须要明确具体的类,并创建对象,麻烦
	public static void getClass_1(){
		Person p1=new Person();
		Class clazz1=p1.getClass();
		Person p2=new Person();
		Class clazz2=p2.getClass();
		System.out.println(clazz1==clazz2);
	}
	//任何数据类型都具备一个静态的属性.class,来获取其对应的class对象
	public static void getClass_2(){
		Class clazz1=Person.class;
		Class clazz2=Person.class;
		System.out.println(clazz1==clazz2);
		
	}
	//只要通过给定的类的字符串名称就可以获取该类对象,更为扩展,可用class类完成,该方法就是forName
	public static void getClass_3() throws ClassNotFoundException{
		Class clazz=Class.forName("com.hy.Person");
		System.out.println(clazz);
	}

public static void main(String[] args) throws Exception {
		//creatNewObject();
		getField();
	}
	//通过反射创建对象
	public static void creatNewObject() throws Exception{
		String name="com.hy.day28.Person";
		//找寻该名称类文件,并加载进内存,并产生Class对象
		Class clazz=Class.forName(name);
		//获取一个该字节码对象所表示的类的一个实例,空参形式
//		Object obj=clazz.newInstance();
		//当该对象初始化不试用空参构造方法时:
		Constructor cons=clazz.getConstructor(String.class,int.class);
		Object obj2=cons.newInstance("小强",26);
	}
	//通过反射获取字段
	public static void getField() throws Exception{
		Class clazz=Class.forName("com.hy.day28.Person");
		Field field=clazz.getDeclaredField("age");
		Object obj=clazz.newInstance();
		//暴力访问,对私有字段的访问取消权限检查
		field.setAccessible(true);
		Object o=field.get(obj);
		System.out.println(o);
	}

	public static void main(String[] args) throws Exception {
		getMethod_4();
	}
	//通过反射获取所有公有的包括父类的方法
	public static void getMethod_1() throws Exception{
		Class clazz=Class.forName("com.hy.day28.Person");
		Method[] methods=clazz.getMethods();
		for(Method method:methods){
			System.out.println(method);
		}
	}
	//通过反射获取所有方法,包括公有的、私有的、抽象的、保护的、默认的,但不包括父类的方法
	public static void getMethod_2() throws Exception{
		Class clazz=Class.forName("com.hy.day28.Person");
		Method[] methods=clazz.getDeclaredMethods();
		for(Method method:methods){
			System.out.println(method);
		}
	}
	//获取一个空参方法
	public static void getMethod_3() throws Exception{		
		Class clazz=Class.forName("com.hy.day28.Person");
		Method method=clazz.getMethod("show", null);
		Object obj=clazz.newInstance();
		method.invoke(obj, null);//调用方法
	}
	//获取一个带参数的方法
	public static void getMethod_4() throws Exception{
		Class clazz=Class.forName("com.hy.day28.Person");
		Method method=clazz.getMethod("paramsShow", String.class,int.class);
		Object obj=clazz.newInstance();
		method.invoke(obj, "杨成勇",28);
		
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值