反射与内省(一)Java

Class类

很多的人---可以定义一个person类(有年龄、性别)

很多的车----可以定义一个car类(有发动机,颜色,车轮)

很多的类------可一定以为一个Class类(类名,构造方法,属性,方法)

得到Class类的对象有三种方法

Object类中的getClass()方法

类.class

通过Class类的forName()方法

创建类的方法:右击项目名称打开properties然后build path--->add librart---->junit---->第一个应用

import org.junit.Test;

class Dog{
	public String type;
	private String name;
	private int age;
	private String color;
	@Override
	public String toString() {
		return "Dog [name=" + name + ", age=" + age + ", color=" + color + "]";
	}
	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;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public Dog(String name,int age,String color){
		this.name=name;
		this.age=age;
		this.color=color;
	}
	public Dog(){
		
	}
	private void set(){
		System.out.println("set");
	}
	
}
public class ReflectionDemo {
	@Test
	public void test1(){//获取class对象的三种形式
		Dog dog=new Dog("哈士奇",3,"蓝色");
		Class aClass=dog.getClass();
		Class dogclass=Dog.class;
		try{
		Class aclass1=getClass().forName("Dog");
		}catch(ClassNotFoundException  e){
			e.printStackTrace();
		}
	}
	@Test
	public void test3() throws IllegalAccessException, IllegalArgumentException{
		Class<Dog>dogClass=Dog.class;
		Constructor<?>[] constructors=dogClass.getConstructors();//获取所有构造方法
		for(int i=0;i<constructors.length;i++){
			System.out.println(constructors[i].getName());
			System.out.println(constructors[i].getParameterCount());
			//System.out.println(constructors[i].get);
		}
		try{
		Constructor<Dog> constructor =dogClass.getConstructor(String.class,int.class,String.class);
		//调用带参数的构造器来实例化对象
		Dog dog=constructor.newInstance("小白",3,"黄色");
		}catch(NoSuchMethodException e){}	
		 catch(InstantiationException e){}
		catch(InvocationTargetException e){}
	}
	@Test
	public void test4(){
		Class<Dog> dogClass=Dog.class;
		Field[] fields=dogClass.getFields();//所有属性的抽象
		System.out.println(fields.length);//输出所有非私有属性
		Field[] declaredFields=dogClass.getDeclaredFields();
		//System.out.println(declaredFields.length);
		for(int i=0;i<declaredFields.length;i++){
			int modifiers=declaredFields[i].getModifiers();
			System.out.println(Modifier.toString(modifiers)+" "+declaredFields[i].getName());
		}
	}
	@Test
	public void test2() throws InstantiationException, IllegalAccessException{//通过反射来实例化对象
		Class<Dog> dogClass=Dog.class;
		try{
		Dog dog=((Dog)dogClass.newInstance());
		}catch(InstantiationError e){
			
		}catch(IllegalAccessError e){
			
		}
	}
	@Test
	public void test5() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		Dog dog=new Dog("旺旺",4,"白色");
		Class<Dog> dogClass=Dog.class;
		Package aPackage=dogClass.getPackage();
		//System.out.println(aPackage.getName());
		Method[] methods=dogClass.getMethods();//获取公共的方法包括继承的共有方法
		for(int i=0;i<methods.length;i++){
			System.out.println(methods[i]);
			if(methods[i].getName().equals("toString")){
			String s=(String)methods[i].invoke(dog);
				System.out.println(s);
			}
		}//
		System.out.println("------------");
		//访问私有方法,获取到本类中定义的所有方法(不包括父类)
		Method[] declaredMethods=dogClass.getDeclaredMethods();
		for(int i=0;i<declaredMethods.length;i++){
			System.out.println(declaredMethods[i]);
			
			if(declaredMethods[i].equals("set")){
				declaredMethods[i].setAccessible(true);//设置私有方法可以被访问(去除访问修饰符)
				declaredMethods[i].invoke(dog);
			}
		}
	}
	public static void main(String[] args) {
		 
	}

}


然后运行的时候,点住方法名右击找到run  as--->junt test然后就能看到该方法的运行结果了,如果没出现结果可能是没写@Test

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值