黑马程序员——基础加强——反射


补充:数组类型的Class实例对象 
class.isArray()
总之,只要是在源程序中出现的类型,都有各自的Class实例对象,例如,int[],void。。。

class  ReflectTest
{
	public static void main(String[] args) throws Exception
	{
		String str1="abc";
		Class cls1 = Str1.getClass();
		Class cls2 = String.class;
		Class cls3 = Class.forName("java.lang.String");

		System.out.println(cls1==cls2);//true
		System.out.println(cls2==cls3);//true

		System.out.println(cls1.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.TYPE所包装的基本类型的字节码。
		System.out.println(int[].class.isPrimitive());//false
		System.out.println(int[].class.isArray());//true
	}
}


Constructor类

Field类


method类

用反射方法执行某个类中的main方法
为什么要用反射的方式调用?

注意:


<span style="font-size:10px;">class  ReflectTest
{
	public static void main(String[] args) throws Exception
	{
		String str1="abc";
		Class cls1 = Str1.getClass();
		Class cls2 = String.class;
		Class cls3 = Class.forName("java.lang.String");

		System.out.println(cls1==cls2);//true
		System.out.println(cls2==cls3);//true

		System.out.println(cls1.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.TYPE所包装的基本类型的字节码。
		System.out.println(int[].class.isPrimitive());//false
		System.out.println(int[].class.isArray());//true

		//new String(new StringBuffer("abc"));
		Constructor constructor1 = String.class.getConstructor(StringBuffer.class);
		String str2 = (String)constructor1.newInstance(/*"abc"*/new StringBuffer("abc"));
		System.out.println(str2.charAt(2));	//c	

		ReflectPoint pt1 = new ReflectPoint(3,5);
		Field fieldY = pt1.getClass().getField("y");
		//fieldY的值是多少?是5,错!fieldY不是对象身上的变量,而是类上,要用它去取某个对象上对应的值,
		System.out.println(fieldY.get(pt1));//5
		Field fieldX = pt1.getClass().getFiedl("x");
		fieldX.setAccessible(true);//暴力反射
		System.out.println(fieldX.get(pt1));//3

		changeStringValue(pt1);
		System.out.rintln(pt1);
		
		//str1.charAt(1);
		//ciecle.draw();
		Method methodCharAt = String.class.getMethod("charAt",int.class);
		System.out.println(methodCharAt.invoke(str1,1));//(null,1)静态方法不需要对象。
		System.out.println(methodCharAt.invoke(str1,new Object[](2)));//按1.4的语法调用

		//TestArguments.main(new String[]("111","222","333"));
		String startingClassName = args[0];
		Method mainMethod = Class.forName(startingClassName).getMethod("main",.class);
		//mainMethod.invoke(null,new String[]("111","222","333"));
		mainMethod.invoke(null,(Object)new String[]("111","222","333"));

		int[] a1 = new int[]{1,2,3};
		int[] a2 = new int[4];
		int[][] a3 = new int[2][3];
		String[] a4 = new String[]{"a""b""c"};
		System.out.println(a1.getClass() == a2.getClass());//true
		System.out.println(a1.getClass()==a4.getClass());//false
		System.out.println(a1.getClass()==a3.getClass());//false
		System.out.println(a1.getClass().getName());//[I
		System.out.println(a1.getClass().getSuperclass().getName());
		System.out.printlna4.getClass().getSuperclass().getName());

		Object aObj1 = a1;
		Object aObj2 = a4;
		//Object[] aObj3 = a1;不可以,int不是Object基本类型
		Object[] aObj4 = a3;
		Object[] aObj5 = a4;

		System.out.println(a1);//[I@...
		System.out.println(a4);//[Ljava.lang.String;@...

		System.out.println(Arrays.asList(a1));//[[I@....]
		System.out.println(Arrays.asList(a4));//[a,b,c]

		printObject(a4);

		printObjcet("xyz");
	}
	private static void printObjcet(Object obj)
	{
		Class clazz = obj.getClass();
		if(clazz.isArray())
		{
			int len = Array.getLength(obj);
			for(int i=0;i<len;i++)
			{
				System.out.println(Array.get(obj,i));
			}
		}
		else
			System.out.println(obj);
	}

	private static void changeStringValue(Object obj) throws Exception
	{
		Field[] fields = obj.getClass().getFields();
		for(Fiedl field : fields)
		{
			//if(field.getType().equals(String.class))
			if(field.getType() == String.class)//同一份字节码用双等号。
			{
				String oldValue =(String)field.get(obj);
				String newValue = oldValue.replace('b','a');
				field.set(obj,newValue);
			}
		}
	}
}
class TestArguments
{
	public static void main(String[] args)
	{
		for(String arg : args)
		{
			System.out.println(arg);
		}
	}
}</span>

class ReflectPoint 
{
	private int x;
	public int y;
	public String str1 = "ball";
	public String str2 = "basketball";
	public String str2 = "itcast";

	public ReflectPoint(int x,int y)
	{
		super();
		this.x = x;
		this.y = y;
	}
	public String toString()
	{
		return str1 + ":" + str2 + ":" + str3;
	}
	public int hashCode()
	{
		final int prime = 31;
		int resule = 1;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}

	public boolean equals(Object obj)
	{
		if(this == obj)
			return true;
		if(obj == null)
			return false;
		if(getClass() != obj.getClass())
			retrun false;
		final ReflectPoint other = (ReflectPoint) Obj;
		if(x != other.x)
			return false;

	}
}



数组的反射

思考题:int[ ] a = new int[3];
Object[ ] a = new Object[ ]("a",1);
a[0].getClass().getName();





import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

class ReflectTest2 
{
	public static void main(String[] args) throws Exception
	{
		InputStream ips = new FileInputStream("config.properties");
		Properties props = new Properties();
		props.load(ips);
		ips.close();//把windows里的关掉,释放资源,减少内存泄露,不是把java里的关掉
		String className = prop.getProperty("className");
		Collection collections = (Collection)Class.forName(className).newInstance;

		Collection collections = new ArrayList();//打印结果是4,
		//Collection collections = new HashSet();//打印结果是3,
		ReflectPoint pt1 = new ReflectPoint(3,3);
		ReflectPoint pt2 = new ReflectPoint(5,5);
		ReflectPoint pt3 = new ReflectPoint(3,3);

		collections.add(pt1);
		collections.add(pt2);
		collections.add(pt3);
		collections.add(pt1);

		//pt1.y = 7;
		//collections.remove(pt1);//内存泄露

		System.out.println(collections.size());
	}
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值