java 之 反射笔记(二)

import java.lang.reflect.Constructor;


public class ConstructorTest {

	/**
	 * 根据参数类型  区别选择构造方法
	 * @param args
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 */
	public static void main(String[] args) throws Exception {
		
		//new String(new StringBuffer("abc"));
		Constructor constructor1 = String.class.getConstructor(StringBuffer.class);
		
		//class java.lang.String
		System.out.println(constructor1.getDeclaringClass());
		String str1 = (String) constructor1.newInstance(new StringBuffer("abc"));
		
		System.out.println(str1.charAt(2));
	}

}
public class ReflectPoint {
	private int x;
	public int y;
	
	public String str1 = "a";
	public String str2 = "b";
	public String str3 = "c";
	
	public ReflectPoint(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ReflectPoint other = (ReflectPoint) obj;
		if (x != other.x)
			return false;
		if (y != other.y)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "ReflectPoint [str1=" + str1 + ", str2=" + str2 + ", str3="
				+ str3 + "]";
	}
	
	
}

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


public class FieldTest {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		ReflectPoint pt1 = new ReflectPoint(1, 3);
		
		Field fieldY = pt1.getClass().getField("y");
		//在pt1对象上的y值
		//fieldY不是对象身上的变量,而是类上。要用它去取每个对象的fieldY值
		System.out.println(fieldY.get(pt1));
		
		//getField("x")报错,私有不可见
		Field fieldX = pt1.getClass().getDeclaredField("x");
		//设置可以访问
		fieldX.setAccessible(true);
		System.out.println(fieldX.get(pt1));
		
		//改变x变量的值
		fieldX.set(pt1, 2);
		System.out.println(fieldX.get(pt1));
		
		//ReflectPoint [str1=new, str2=new, str3=new]
		changeStringValue(pt1);
		System.out.println(pt1);
		
		Method method1 =String.class.getMethod("charAt", int.class);
		String str1 = "abc";
		char ch = (Character) method1.invoke(str1, 1);
		//char ch = (Character) method1.invoke(str1, new Object[]{2});
		//b
		System.out.println(ch);
		
		//ReflectPoint [str1=new, str2=new, str3=new]
		Method toStr = ReflectPoint.class.getMethod("toString");
		System.out.println(toStr.invoke(pt1));
	}

	private static void changeStringValue(Object obj) throws Exception {
		Field[] fields = obj.getClass().getFields();
		for(Field field:fields) {
			//同一份字节码
			if(field.getType()==String.class) {
				String oldValue = (String) field.get(obj);
				String newValue = oldValue.replace(oldValue, "new");
				field.set(obj, newValue);
			}
		}
	}
}

import java.lang.reflect.Method;


public class MethodTest {

	/**
	 * @param args
	 * @throws ClassNotFoundException 
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 */
	public static void main(String[] args) throws Exception {
		String startingClassName = args[0];
		//根据类名拿到字节码,再拿到方法
		Method mainMethod = Class.forName(startingClassName).getMethod("main", String[].class);
		//main方法接收一个参数,数组对象。所以要把字符串数组包装成Object,数组也是对象
		//它自己会拆开,就是三个参数,三个对象,就会报wrong number of arguments 异常
		//或者new Object[]{new String[]{"1","2","3"}},拆开后是一个对象
		mainMethod.invoke(null, (Object)new String[]{"1","2","3"});
	}
	
}
class TestArguments {
	//main方法接收一个参数,数组对象。所以要把字符串数组包装成Object
	public static void main(String[] args) {
		for(String arg:args) {
			System.out.println(arg);
		}
	}
}

public class ReflectTest {

	/**
	 * @param args
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws ClassNotFoundException {
		String str1 = "abc";
		Class cls1 = str1.getClass();
		
		Class cls2 = String.class;
		
		Class cls3 = Class.forName("java.lang.String");
		
		//true,同一份字节码
		System.out.println(cls1==cls2);
		System.out.println(cls1==cls3);
		
		//非基本类型字节码
		System.out.println(cls1.isPrimitive());
		System.out.println(int.class.isPrimitive());
		
		//false
		System.out.println(int.class==Integer.class);
		//true,常量代表基本类型
		System.out.println(int.class==Integer.TYPE);
		//false
		System.out.println(int[].class.isPrimitive());
		
		System.out.println(int[].class.isArray());
	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值