测试类加载器及反射

package com.example.demo.service.impl;

import com.example.demo.service.IFruitFactory;

public class Apple implements IFruitFactory {
	
	@Override
	public void sell(int count) {
		System.out.println("苹果售价:"+count);
		
	}
	
	@Override
	public void eat() {
		System.out.println("开吃");

	}
	
	
	public String sing() {
		//System.out.println("我唱了一首歌");
		return "我唱了一首歌";
	}
	
	public void sing(String name,int count) {
		System.out.println(name + "唱了" +count+ "一首歌");
	}

}
package com.example.demo.model;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.example.demo.service.IFruitFactory;
import com.example.demo.service.impl.Apple;

public class TestClass {
	
	public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException, NoSuchMethodException, SecurityException {
		System.out.println("类加载器");
		System.out.println(Apple.class);
		System.out.println(Apple.class.getClassLoader());
		System.out.println(Apple.class.getClassLoader().getClass());
		
		System.out.println(Apple.class.getClassLoader().getParent());
		
		System.out.println(Apple.class.getClassLoader().getParent().getClass().getClassLoader());
		System.out.println(System.class.getClassLoader());
		
		System.out.println("************");
		
		Apple a = new Apple();
		a.sell(4);
		System.out.println(a.getClass());
		//判断a.getClass()返回的结果正是Apple的类型类,在Java中表示一个特定类型的类型类可以用“类型.class”的方式获得,因为a.getClass()获得是Apple的类型类,也就是A.class
		System.out.println(a.getClass()==Apple.class);
		//父类或者接口的类型类和子类的类型类是不同的
		System.out.println(a.getClass()==IFruitFactory.class);
		
		System.out.println("********");
		System.out.println(a.getClass().getMethods());
		
		Method[] methods = a.getClass().getMethods();
		Method[] methods2 = a.getClass().getDeclaredMethods();
		
		System.out.println("反射---");
		System.out.println("1.本类及父类或接口中方法(仅包含声明为public类型)");
		for (Method method : methods) {
			System.out.println(method.getName());
		}
		
		System.out.println("2.本类方法(包含该类的所有类型,比如private,protected等)");
		for (Method method : methods2) {
			System.out.println(method.getName());
			
			Class<?>[] types = method.getParameterTypes();//获取参数
			if("sell".equals(method.getName()))
			method.invoke(a,4); 
		}
		
		System.out.println("3.本类方法");
		Class clazz = a.getClass(); 
        Method m1 = clazz.getDeclaredMethod("sell",int.class);//原始类型对应的虚拟机中的class实例和封装类对应的class实例是不相同的。int 对应的class实例为 int.class 或者 Integer.TYPE,但是 Integer 对应的 class 实例为 Integer.class
        Method m2 = clazz.getDeclaredMethod("eat");
        Method m3 = clazz.getDeclaredMethod("sing");
        Method m4 = clazz.getDeclaredMethod("sing",String.class,int.class);
        m1.invoke(a,20); //可看做实参的数据,没有则不用写
        m2.invoke(a); //可看做实参的数据
        String retmsg = (String) m3.invoke(a); 
        System.out.println(retmsg); 
        m4.invoke(a, "藏羚羊",13);
		
	}

}


打印输出结果:

类加载器

class com.example.demo.service.impl.Apple
sun.misc.Launcher$AppClassLoader@2a139a55
class sun.misc.Launcher$AppClassLoader
sun.misc.Launcher$ExtClassLoader@7852e922
null
null
************
苹果售价:4
class com.example.demo.service.impl.Apple
true
false
********
[Ljava.lang.reflect.Method;@33909752
反射---
1.本类及父类或接口中方法(仅包含声明为public类型)
sell
eat
sing
sing
wait
wait
wait
equals
toString
hashCode
getClass
notify
notifyAll
2.本类方法(包含该类的所有类型,比如private,protected等)
sell
苹果售价:4
eat
sing
sing
3.本类方法
苹果售价:20
开吃
我唱了一首歌
藏羚羊唱了13一首歌



补充下百科关于反射定义:

JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制。
JAVA反射(放射)机制:“程序运行时,允许改变程序结构或变量类型,这种语言称为 动态语言”。从这个观点看,Perl,Python,Ruby是动态语言,C++,Java,C#不是动态语言。但是JAVA有着一个非常突出的动态相关机制:Reflection,用在Java身上指的是我们可以于运行时加载、探知、使用编译期间完全未知的classes。换句话说,Java程序可以加载一个运行时才得知名称的class,获悉其完整构造(但不包括methods定义),并生成其对象实体、或对其fields设值、或唤起其methods

反射判断成员变量是否静态,并获得其静态成员的值,也仅能获取静态值,非静态是无法获取的。

   Field[] fields = cls.getDeclaredFields();
        Field field = fields[0];
        boolean isStatic = Modifier.isStatic(field.getModifiers());
        if(isStatic) {
            System.out.println(field.get(null).toString());
        }
另附:反射是可以获取private私有方法,但是私有方法是无法调用的。




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值