java反射语法

package com.jvm.seventh;

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


public class TestReflect {
	
	public static void main(String[] args) throws Exception {
		//根据对象获取类名加包名
		Demo demo=new Demo();
		System.out.println(demo.getClass().getName());
		
		//获取class的三种途径
		Class clazz1=null;
		Class clazz2=null;
		Class clazz3=null;
		clazz1=demo.getClass();
		clazz2=Demo.class;
		clazz3=Class.forName("com.jvm.seventh.Demo");
		System.out.println(clazz1+","+clazz2+","+clazz3);
		
		//获取该类对象的接口
		System.out.println("----------implements interface----------------");
		Class[] inters=clazz1.getInterfaces();
		for(int i=0;i<inters.length;i++){
			System.out.println(inters[i].getName());
		}
		
		//获取该类的父类
		System.out.println("----------super class----------------");
		Class superClass=clazz1.getSuperclass();
		System.out.println(superClass.getName());
		
		//获取对象
		Demo d1=null;
		d1=(Demo)clazz1.newInstance();
		d1.setAge(10);
		d1.setName("d1--name");
		System.out.println(d1.toString());
		
		//获取构造器 构造对象 
		//构造器的数组的参数索引根据实际类中构造器的顺序来寻找的
		System.out.println("----------cosntructors ----------------");
		Constructor[] cons=clazz1.getConstructors();
		Demo d2=null;
		Demo d3=null;
		for(int i=0;i<cons.length;i++){
			System.out.println(cons[i]);
		}
		d2=(Demo)cons[0].newInstance("d3--name",100);
		d3=(Demo)cons[1].newInstance();
		System.out.println("d2:"+d2.toString());
		System.out.println("d3:"+d3.toString());
		
	
		
		//获取该类的属性
		//self-class fields
		System.out.println("----------declared fields----------------");
		Field[] fields=clazz1.getDeclaredFields();
		for(int i=0;i<fields.length;i++){
			int mod=fields[i].getModifiers();
			String priv=Modifier.toString(mod);
			Class type=fields[i].getType();
			System.out.println(priv+" "+type.getName()+" "+fields[i].getName());
		}
		//super fileds
		System.out.println("----------super fields----------------");
		Field[] superFields=clazz1.getFields();
		for(int i=0;i<superFields.length;i++){
			int mod=superFields[i].getModifiers();
			String priv=Modifier.toString(mod);
			Class type=fields[i].getType();
			System.out.println(priv+" "+type.getName()+" "+superFields[i].getName());
		}
		
		//操作属性
		System.out.println("----------oper name fields----------------");
		Field name=clazz1.getDeclaredField("name");
		name.setAccessible(true);
		name.set(demo, "asia");
		System.out.println(name.get(demo));
		
		
		
		//获取对象的方法
		System.out.println("----------invoke methods----------------");
		Method method=clazz1.getMethod("print",String.class);
		method.invoke(demo,"myArg");
		
		
	}
}


interface int1{
	public static final String int1="int1";
}

interface int2{
	public static final String int2="int2";
}


class Demo implements int1,int2{
	
	public Demo() {
		// TODO Auto-generated constructor stub
	}
	
	public Demo(String name,int age){
		this.name=name;
		this.age=age;
	}
	
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "[name="+name+",age="+age+"]";
	}
	
	public void print(String arg){
		System.out.println("[arg="+arg+",name="+name+",age="+age+"]");
	}
	public void print(){
		System.out.println("[name="+name+",age="+age+"]");
	}
}
 

你可能感兴趣的:(java反射)