Java反射---reflect

本文深入探讨Java反射机制,包括Class对象获取方式、判断数据类型、访问构造器、方法及字段等核心功能。通过实例演示如何利用反射进行动态类型判断、调用私有成员和创建实例。

Person p = new Person();

1.Class c = String.class;

2.Class c = p.getClass();

3.Class c = Class.forName("java.lang.String");//throw exception

 

Person p = (Person)c.newInstance();

 

package com.hoo.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class TestReflect {
	public static void main(String[] args) throws Exception {
		String str = "abc";
		Class c1 = str.getClass();
		Class c2 = String.class;
		Class c3 = null;
		c3 = Class.forName("java.lang.String");
		System.out.println(c1 == c2);// true
		System.out.println(c1 == c3);// true

		System.out.println(c1.isPrimitive());// false //判断是否基本数据类型
		System.out.println(int.class.isPrimitive());// true
		System.out.println(int.class == Integer.class);// false
		System.out.println(int.class == Integer.TYPE);// true
		System.out.println(int[].class.isPrimitive());// false
		System.out.println(int[].class.isArray());// true
          
		/*
		 * Constructor
		 */
		Constructor<?>[] cs = String.class.getDeclaredConstructors();
		// String.class.getConstructors(); 只能访问public
          for (Constructor c : cs) { c.setAccessible(true); System.out.println(c); } System.out.println(String.class.getConstructor(String.class)); /* * Method */ Method[] ms = String.class.getDeclaredMethods(); for (Method m : ms) {
              // String s =  Modifier.toString(m.getModifiers()); //public private default protected m.setAccessible(true); System.out.println(m); } /* * Field */ Field[] fs = String.class.getDeclaredFields(); for (Field f : fs) { f.setAccessible(true); System.out.println(f); } } }

 

package com.hoo.reflect;

import java.lang.reflect.Field;

public class TestReflect2 {
	public static void main(String[] args) throws Exception {
		RefletPoint rp = new RefletPoint(3, 4);
		changeBtoA(rp);
		System.out.println(rp);

	}
	
	private static void changeBtoA(Object obj) throws Exception{
		Field[] fs = obj.getClass().getDeclaredFields();
		for(Field f : fs){
			f.setAccessible(true);
			if(f.getType() == String.class){
				String oldValue = (String)f.get(obj);
				String newValue = oldValue.replace('b', 'a');
				f.set(obj, newValue);
			}
		}
	}

}

class RefletPoint {
	private int x = 0;
	public int y = 0;
	public String str1 = "ball";
	public String str2 = "basketball";
	public String str3 = "itcat";

	public RefletPoint(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "ReflectPoint [str1 = " + str1 + " ,str2 = " + str2 + " ,str3 = " + str3 + "]";
	}

}

  

package com.hoo.reflect;

import java.lang.reflect.Method;

public class TestReflect3 {
	public static void main(String[] args) throws Exception {
		Class c = Class.forName("com.hoo.reflect.S");
		System.out.println(c.isInstance(new Integer(10)));
		System.out.println(c.isInstance(new S()));
		
		
		
		String s = "asdf";
		Method m = String.class.getMethod("charAt", int.class);
		Object ch = m.invoke(s, 3);
		System.out.println(ch);
		
//		System.out.println(m.invoke(s, new Object[]{2}));
	}
}

class S{
}

  

转载于:https://www.cnblogs.com/Uncho/p/4137574.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值