为什么要尝试调用
try {
method.invoke(super, "abc", "def");
}
catch (Exception e) {
// ignore for now
}
给了我这样一个错误:
'.' expected
在Netbeans 7.2.1中?清洁和建设之后:
Compiling 2 source files to C:\Documents and Settings\u\Moje dokumenty\NetBeansProjects\ServletPlus\build\web\WEB-INF\classes
C:\Documents and Settings\u\Moje dokumenty\NetBeansProjects\ServletPlus\src\java\pl\adrian\servlets\ServletPlus.java:45: error: '.' expected
method.invoke(super, "abc", "def");
C:\Documents and Settings\u\Moje dokumenty\NetBeansProjects\ServletPlus\src\java\pl\adrian\servlets\ServletPlus.java:45: error: ')' expected
method.invoke(super, "abc", "def");
C:\Documents and Settings\u\Moje dokumenty\NetBeansProjects\ServletPlus\src\java\pl\adrian\servlets\ServletPlus.java:45: error: ';' expected
method.invoke(super, "abc", "def");
C:\Documents and Settings\u\Moje dokumenty\NetBeansProjects\ServletPlus\src\java\pl\adrian\servlets\ServletPlus.java:45: error: not a statement
method.invoke(super, "abc", "def");
C:\Documents and Settings\u\Moje dokumenty\NetBeansProjects\ServletPlus\src\java\pl\adrian\servlets\ServletPlus.java:45: error: ';' expected
method.invoke(super, "abc", "def");
5 errors
C:\Documents and Settings\u\Moje dokumenty\NetBeansProjects\ServletPlus\nbproject\build-impl.xml:851: The following error occurred while executing this line:
C:\Documents and Settings\u\Moje dokumenty\NetBeansProjects\ServletPlus\nbproject\build-impl.xml:284: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 3 seconds)
答案 0 :(得分:3)
super
是reserved word in java。
尝试更改变量名称。
请在此处查看如何使用super
关键字。 http://docs.oracle.com/javase/tutorial/java/IandI/super.html
如果您想invoke a superclass method,则必须使用super.methodName()
答案 1 :(得分:2)
我相信你不是正确的:fyi如何使用java反射调用方法的例子:
java.lang.reflect.Method method;
try {
method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
} catch (SecurityException e) {
// ...
} catch (NoSuchMethodException e) {
// ...
}
try {
method.invoke(obj, arg1, arg2,...);
}
修改强>
同样@MaVRoSCyhas提到super
是java中的关键词
答案 2 :(得分:0)
替换< classInstance>与你班级的名字。
Method m = <classInstance>.getClass().getSuperClass().getDeclaredMethod("ABC");
m.invoke(<classInstance>);
答案 3 :(得分:0)
问题在于我对多态性的理解不好,嗯,这有点可怜,因为我认为“超级”指的是超类'对象,而“这”指的是当前对象 - 就像那样。
当然,只有一个对象是扩展类的实例。 所以我不得不用“this”替换“super”。