我可以覆盖Java中的私有方法吗? [英] Can I override a private method in Java?
问题描述
我知道我可以使用反射来调用私有方法,并获取或设置私有变量的值,但我想覆盖一个方法。
I know I can use reflection to invoke a private method, and to get or set the value of a private variable, but I want to override a method.
public class SuperClass {
public void printInt() {
System.out.println("I am " + getClass() + ". The int is " + getInt());
}
private int getInt() {
return 1;
}
}
public class SubClass extends SuperClass {
public static void main(String[] args) {
(new SubClass()).printInt();
}
public int getInt() {
return 2;
}
}
我想要 main
SubClass
中的方法打印出 2
,但打印出 1
。
我听说这可以通过反思完成,但我无法弄清楚如何。
如果没有反思,有没有人知道另一种方式呢?
(除了使 SuperClass.getInt()
受保护,或复制并粘贴 printInt()
方法进入 SubClass
。)
如果实际覆盖私有方法是不可能的,有没有办法在它上面放置某种触发器来调用我的方法私有方法执行之前或之后的子类?
I want the main
method in SubClass
to print out 2
, but it prints out 1
.
I've heard this can be done through reflection, but I can't figure out how.
If not reflection, does anyone know of another way of doing it?
(Other than making SuperClass.getInt()
protected, or copying and pasting the printInt()
method into SubClass
.)
If actually overriding the private method is not possible, is there a way of placing some sort of trigger on it that will invoke a method in my sub-class either before or after the private method executes?
推荐答案
私有方法不会被继承,也不能以任何方式覆盖。无论谁告诉你你可以用反射来做这件事要么是说谎还是在谈论其他事情。
Private methods are not inherited and cannot be overridden in any way. Whoever told you you can do it with reflection was either lying or talking about something else.
但是,你可以访问私有方法 getInt
的子类正在调用 printInt
,如下所示:
However, you can access the private method getInt
of whatever subclass is invoking printInt
like so:
public void printInt() throws Exception {
Class<? extends SuperClass> clazz = getClass();
System.out.println("I am " + clazz + ". The int is " +
clazz.getMethod("getInt").invoke(this) );
}
这将具有子类' getInt的效果
从超类' printInt
调用的方法。
当然,如果子类没有声明 getInt
,现在这将失败,所以你必须添加一个检查能够处理不试图覆盖私有方法的普通子类:
This will have the effect of the subclass' getInt
method being called from the superclass' printInt
.
Of course, now this will fail if the subclass doesn't declare a getInt
, so you have to add a check to be able to handle "normal" subclasses that don't try to "override" a private method:
public void printInt() throws Exception {
Class<? extends SuperClass> clazz = getClass();
// Use superclass method by default
Method theGetInt = SuperClass.class.getDeclaredMethod("getInt");
// Look for a subclass method
Class<?> classWithGetInt = clazz;
OUTER: while( classWithGetInt != SuperClass.class ){
for( Method method : classWithGetInt.getDeclaredMethods() )
if( method.getName().equals("getInt") && method.getParameterTypes().length == 0 ){
theGetInt = method;
break OUTER;
}
// Check superclass if not found
classWithGetInt = classWithGetInt.getSuperclass();
}
System.out.println("I am " + classWithGetInt + ". The int is " + theGetInt.invoke(this) );
}
您仍然需要更改超类代码才能使其正常工作,并且因为你有要更改超类代码,您只需将 getInt
上的访问修饰符更改为 protected
,而不是进行反射黑客攻击。
You still have to change superclass code to make this work, and since you have to change superclass code, you should just change the access modifier on getInt
to protected
instead of doing reflection hack-arounds.
这篇关于我可以覆盖Java中的私有方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!