【发布时间】:2014-09-14 17:58:36
【问题描述】:
我是 Java 新手,我完全坚持它。 我必须实现“summator”类,对其中的所有数字求和。 例如这个类包含一个数字
public class NumericNode <N extends Number>{
private N nodeValue = null;
public NumericNode(N initValue){
this.nodeValue = initValue;
}
public N getNodeValue() {
return nodeValue;
}
}
第二课需要做一些总结
public class NumericSummatorNode<NumericNode, T> {
private T nodeValue;
private NumericNode[] inlets;
public NumericSummatorNode(NumericNode...inlets) {
this.summ(inlets);
}//constructor
public void summ(NumericNode... summValues) {
ArrayList<NumericNode> numericList = new ArrayList<>();
int count = summValues.length;
for (int i = 0; i < count; i++){
numericList.add(summValues[i]);
}
for (int j = 0; j < count; j++){
Method method = numericList.get(j).getClass().getMethod("getNodeValue", null);
method.invoke(numericList.get(j), null);
}
}
这里是主要的:
public static void main(String[] args){
NumericNode n1 = new NumericNode(5);
NumericNode n2 = new NumericNode(4.3f);
NumericNode n3 = new NumericNode(24.75d);
NumericNode n5 = new NumericNode((byte)37);
NumericNode n6 = new NumericNode((long)4674);
NumericSummatorNode s1 = new NumericSummatorNode(5, 4.6f, (double)4567);
NumericSummatorNode s2 = new NumericSummatorNode(n1, n2);
NumericSummatorNode s3 = new NumericSummatorNode();
s2.summ(n1, n2);
}//main
所以我在从数组列表中的 NumericNode 对象调用 getNodeValue() 方法时遇到了问题。 我该如何完成这项工作?
【问题讨论】:
-
有什么异常??
-
NuSuchMethodException 和其他一些异常。但是还是不行
标签: java reflection methods arraylist