从.class调用对象的 toString() 方法(类<?>)

我有一个名为“Test”的类,我正在将其传递给一个方法,该方法的类型为。Test.classClass<T> clazz

我需要从该 clazz 变量调用类 Test 的 toString() 方法。我们可以使用反射来做到这一点,而不会对测试进行类型转换吗?


慕尼黑5688855
浏览 122回答 2
2回答

慕的地8271018

首先从 clazz 变量创建对象,然后调用该对象的方法。toString()clazz.newInstance().toString();如果你有一个默认的构造函数,那么它也可以工作:clazz.getConstructor().newInstance().toString();更新:如果你想做一般的测试函数,那么你必须实现这样的测试函数。public&nbsp;void&nbsp;TestToString(Object&nbsp;obj,String&nbsp;expectedVal){ &nbsp;&nbsp;&nbsp;&nbsp;assertEquals(obj.toString(),expectedVal); &nbsp;&nbsp;}

达令说

不能对参数调用方法。这只是类的描述,但要调用方法,您需要实例化该类。您可以:- 使用反射创建.class参数的实例以调用该方法,或者- 使用反射来调用实例上的方法。Class但底线是,你需要一个实例。还有一点评论(正如Joachim Sauer已经说过的那样):方法存在于类上,并且所有内容都扩展了类,因此它始终可用。不需要反射,泛型或转换。您只需在类中重写此方法即可。toString()ObjectObject一个小例子来演示调用String()的不同方法:public class Test {&nbsp; &nbsp; private String name = "unnamed";&nbsp; &nbsp; public Test() {}&nbsp; &nbsp; public Test(String name) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; }&nbsp; &nbsp; public void setName(String name) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; }&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return name;&nbsp; &nbsp; }&nbsp; &nbsp; public static <T> T doReflectionCreateToString(Class<T> clazz) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; T newT = clazz.getConstructor(String.class).newInstance("Name 2");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Created by reflection:&nbsp; &nbsp; "+ newT);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return newT;&nbsp; &nbsp; &nbsp; &nbsp; } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | NoSuchMethodException | SecurityException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }&nbsp; &nbsp; @SuppressWarnings("unchecked")&nbsp; &nbsp; public static <T, E> E doReflectionCall(T obj, String methodName, Object[] arguments, Class<E> returnClazz) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Class<?> clazz = obj.getClass();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Method myMethod = clazz.getDeclaredMethod(methodName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (E) myMethod.invoke(obj, arguments);&nbsp; &nbsp; &nbsp; &nbsp; } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }&nbsp; &nbsp; public static <T> void doGenericToString(T obj) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("toString by Generic way:&nbsp; "+ obj);&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Test t = new Test();&nbsp; &nbsp; &nbsp; &nbsp; t.setName("Name 1");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("toString by standard way: "+ t);&nbsp; &nbsp; &nbsp; &nbsp; doGenericToString(t);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Called by reflection:&nbsp; &nbsp; &nbsp;"+ doReflectionCall(t, "toString", new Object[]{}, String.class));&nbsp; &nbsp; &nbsp; &nbsp; Test newT = doReflectionCreateToString(Test.class);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Returned by reflection:&nbsp; &nbsp;"+ newT);&nbsp; &nbsp; }}哪些输出:按标准方式 toString: 名称 1to按通用方式排列: 名称 1由反射调用: 名称 1由反射创建: 名称 2由反射返回: 名称 2如您所见,无论是通过标准方式还是通过泛型或更糟糕的反射,都会导致相同的逻辑。我希望这能启发你(和其他人);)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java