获取类并调用其主要功能的方法 [英] method that gets a class and calls its main function
问题描述
我想编写一个获取类和字符串数组的方法,并使用给定的数组调用类的主方法.
类似这样的东西:
公共静态无效ExecuteMain(DefaultClass cls,String [] args){cls.main(args);}
问题是,我想用几个类来做,而不能将确切的类作为类型传递.我也不能让所有其他类都实现默认接口或类,因为它应该用作具有相同类名的同学的测试器.
有什么办法可以解决这些限制吗?
以下是您应如何定义方法的方法:
public static void executeMain(Class<?> cls,Object [] args){尝试 {Object [] param = {args};方法method = cls.getDeclaredMethod("main",args.getClass());method.invoke(cls,param);} catch(NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException|InvocationTargetException e){e.printStackTrace();}}
演示
Assignment1.java:
import java.util.Arrays;公共类Assignment1 {公共静态void main(String [] args){if(args == null){System.out.println(欢迎来到分配1");} 别的 {System.out.println("Assignment1中main的参数:"+ Arrays.toString(args)));}}}
Assignment2.java
import java.util.Arrays;公共类Assignment2 {公共静态void main(String [] args){if(args == null){System.out.println(欢迎来到分配2");} 别的 {System.out.println("Assignment2中main的参数:"+ Arrays.toString(args)));}}}
测试类别:
import java.lang.reflect.InvocationTargetException;导入java.lang.reflect.Method;公共班级主要{公共静态void main(String [] args){Assignment1.main(null);Assignment1.main(new String [] {"Hello","World"});Assignment2.main(null);Assignment2.main(new String [] {"Hello","World"});executeMain(Assignment1.class,new String [] {"Good","Morning"});executeMain(Assignment2.class,new String [] {"Good","Morning"});}公共静态void executeMain(Class<?> cls,Object [] args){尝试 {Object [] param = {args};方法method = cls.getDeclaredMethod("main",args.getClass());method.invoke(cls,param);} catch(NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException|InvocationTargetException e){e.printStackTrace();}}}
输出:
欢迎来到Assignment1作业1中的main参数:[Hello,World]欢迎来到Assignment2作业2中main的参数:[Hello,World]作业1中主要的参数:[早上好]作业2中main的参数:[Good,Morning]
选中此,以了解有关Java Reflection API的更多信息./p>
I want to write a method that gets a class and an Array of strings and call the main method of the class with the array given.
Something like this:
public static void ExecuteMain(DefaultClass cls, String[] args){
cls.main(args);
}
The problem is, I want to do it with several classes, and can't pass the exact class as the type. I also can't have all of the other classes implement a default interface or class because it should be used as a tester for my classmates with the same class names.
Is there any way to do it with those restrictions?
Given below is how you should define the method:
public static void executeMain(Class<?> cls, Object[] args) {
try {
Object[] param = { args };
Method method = cls.getDeclaredMethod("main", args.getClass());
method.invoke(cls, param);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
e.printStackTrace();
}
}
Demo
Assignment1.java:
import java.util.Arrays;
public class Assignment1 {
public static void main(String[] args) {
if (args == null) {
System.out.println("Welcome to Assignment1");
} else {
System.out.println("Arguments to main in Assignment1: " + Arrays.toString(args));
}
}
}
Assignment2.java
import java.util.Arrays;
public class Assignment2 {
public static void main(String[] args) {
if (args == null) {
System.out.println("Welcome to Assignment2");
} else {
System.out.println("Arguments to main in Assignment2: " + Arrays.toString(args));
}
}
}
Test class:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args) {
Assignment1.main(null);
Assignment1.main(new String[] { "Hello", "World" });
Assignment2.main(null);
Assignment2.main(new String[] { "Hello", "World" });
executeMain(Assignment1.class, new String[] { "Good", "Morning" });
executeMain(Assignment2.class, new String[] { "Good", "Morning" });
}
public static void executeMain(Class<?> cls, Object[] args) {
try {
Object[] param = { args };
Method method = cls.getDeclaredMethod("main", args.getClass());
method.invoke(cls, param);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
e.printStackTrace();
}
}
}
Output:
Welcome to Assignment1
Arguments to main in Assignment1: [Hello, World]
Welcome to Assignment2
Arguments to main in Assignment2: [Hello, World]
Arguments to main in Assignment1: [Good, Morning]
Arguments to main in Assignment2: [Good, Morning]
Check this to learn more about Java Reflection API.
这篇关于获取类并调用其主要功能的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!