Java类信息作为函数参数
Posted szcloud
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java类信息作为函数参数相关的知识,希望对你有一定的参考价值。
import java.lang.reflect.InvocationTargetException;
interface IA{
void fun();
}
class C implements IA{
public void fun() {
System.out.println("fun in C");
}
}
class D implements IA{
public void fun() {
System.out.println("fun in D");
}
}
class B{
public void fun(Class<?> ia) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
System.out.println(ia.getName());
IA exp=(IA) ia.getDeclaredConstructor().newInstance(); //根据反射机制实例化
exp.fun(); //多态
}
}
public class test {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
B myB=new B();
myB.fun(D.class);
System.out.println("normal running.");
}
}
以上是关于Java类信息作为函数参数的主要内容,如果未能解决你的问题,请参考以下文章