- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果我有一个构造函数对象 Constructor<?> productConstructor
, 我怎样才能调用 run()
该类中存在的方法?
例如,假设我有一个类:
public class product1{
public product1(Instances instance){
// constructor
}
public void run(){
// do something
}
}
我可以像这样为我的类运行构造函数:
Constructor<?> productConstructor;
productConstructor = getProductConstructor(instance.getProductName());
// pass instance into constructor to run the tests
// example of what is happening here -> new product1(instance)
productConstructor.newInstance(new Object[] { instance });
public Constructor<?> getProductConstructor(String productName) {
try {
logger.info("Looking for Product Class: {}", productName);
String fullQualifiedClassPath = "com.products." + productName;
Class<?> clazz = Class.forName(fullQualifiedClassPath);
return clazz.getConstructor(Instances.class);
} catch (Exception e) {
logger.error("Product: {} does not exist", productName);
}
return null;
}
如果我保持代码不变,那么我必须放置 run()
构造函数内部的方法来运行它,但我想要 run()
构造函数之外的方法。如何使用我的类的 Constructor 类调用我的 run 方法?
例如,像这样的东西是我想到的但不起作用:
Constructor<?> productConstructor;
productConstructor = getProductConstructor(instance.getProductName());
productConstructor.newInstance(new Object[] { instance }).run(); // added .run() to the end but this doesn't work.
这不起作用,因为 run
未定义。这是因为我不能在一个直到运行时才知道的类上以这种方式调用方法。这就是我的问题所在。
最佳答案
问题是 productConstructor.newInstance(new Object[] { instance });
检索了一个 Object
并且您必须将其转换为所需的类型 product1
:
((product1)productConstructor.newInstance(new Object[] { instance })).run();
让我向您提供我的一个项目reflection-utils .
<dependency>
<groupId>ru.oleg-cherednik.utils.reflection</groupId>
<artifactId>reflection-utils</artifactId>
<version>1.0</version>
</dependency>
product1 product1 = ConstructorUtils.invokeConstructor(product1.class, Instances.class, new Instances());
product1.run();
如果您不能包含该文件(例如文件具有包可见性),您可以使用名称来做同样的事情:
String className = "com.products.product1";
Object product1 = ConstructorUtils.invokeConstructor(className, Instance.class, new Instance());
String methodName = "run";
MethodUtils.invokeMethod(product1, methodName);
您可以在此处找到有关如何使用反射的更多示例。
关于java - 假设我有构造函数,如何从类中调用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69866192/
一旦我看到了用C++进行某种假设的方法,例如: int x=7; assume (x==7);//if not right a red error will appear and program wi
我正在尝试测试我的数据库类。这是它的简化示例。 class Database: """ it has more methods but I show only the most important "
这只是一个思考练习,我会对任何意见感兴趣。尽管如果它有效,我可以想出一些我会使用它的方法。 传统上,如果你想对由数组或范围等形成的嵌套循环的结果执行一个函数,你会这样写: def foo(x, y)
当某些假设无效时,MSTest 是否有办法不运行测试?就像 JUnit 的“Assume.*”方法一样: //Setup Assume.assumeEquals(2, count); //Only r
为什么会出现这个警告?如果我检查边界,这并不是一个真正的假设。以及如何修复? 如果num_actions_to_skip设置为 1,而不是 2,错误消失。 谢谢 error: assuming sig
书理解和使用 C 指针 , by Richard Reese 说: The null concept is an abstraction supported by the null pointer c
所以我有两个假设,一个是 h : A -> B,另一个是 h2 : A。如何让 h3 : B 出现在我的假设中? 最佳答案 pose proof (h h2) as h3. 引入h3 : B作为新假设
我知道发生冲突的可能性很小,但如果我生成了一批 1000 个 GUID(例如),是否可以安全地假设它们都是唯一的以节省对每个 GUID 的测试? 奖励问题 测试 GUID 唯一性的最佳方法是什么?也许
这个问题已经有答案了: Jackson JSON: get node name from json-tree (5 个回答) 已关闭 7 年前。 我正在尝试迭代 JsonNode 树,并且我编写了以下
我无法弄清楚如何在 Sympy 中假设复数的正实部。Mathematica 代码示例: a = InverseFourierTransform[ R/(I omega - lambda) + Con
这个问题在这里已经有了答案: 关闭 14 年前。 重复: Do web sites really need to cater for browsers that don’t have Javascr
我使用hypothesis 已经有一段时间了。我想知道如何重用 @given parts。 我有一些大约 20 行,我将整个 @given 部分复制到几个测试用例之上。 一个简单的测试例子 @give
您好,我的 C++ 代码中有一个错误。我有 2 个 .cpp 文件和 1 个 .h 文件,我试图从头文件访问 5 个字符串和 1 个 int,但我收到一条错误消息,提示“缺少显式类型(假设为‘int’
我正在尝试使用 IAR 开发一个项目。这是错误消息:错误 [Pe260]:缺少显式类型(假定为“int”) 问候。 当我尝试:void send_data_byte(unsigned char dat
我正在处理一个数组,我想在其中添加它的一些值。在某些时候,为了仅通过一次计算即可完成此操作,它会要求数组外的索引。 有没有办法说,“如果索引在数组之外,则假定值为 0”? 有点像这样:
在 Python 2 中,我想评估一个包含文字表示的字符串。我想安全地执行此操作,所以我不想使用 eval()——相反,我已经习惯了使用 ast.literal_eval()的任务。 但是,我还想在纯
我正在对时间进行大量计算,通过添加秒数来构建相对于其他时间对象的时间对象。该代码应该在嵌入式设备和服务器上运行。大多数文档都说 time_t 是某种算术类型,通常存储自纪元以来的时间。假设 time_
我正在编写一个程序,其中大多数使用的库函数返回-1 并设置错误号。程序的行为是在发生错误时退出。要从程序外部确定确切的退出点和错误(例如使用 gdb),我想使用以下方法: err = func_1(.
这是我今天考试的一道题: 在 C 中,假设指针是严格类型化的(即,指向 int 的指针不能用于指向 char)。这会降低它的表达能力吗?如果不是,您为什么以及如何补偿此限制?如果是,如何?您还需要添加
我将星期几存储在数据库中,其中星期日 = 1,星期一 = 2 等。 在数据库查询中,我需要将日期转换为 System.DayOfWeek。 根据 MSDN : The value of the con
我是一名优秀的程序员,十分优秀!