java 有没有什么方法可以在不修改方法的情况下从java中的子类对象调用父类方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30068210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
is there any way to call parent class method from child class object in java without modifying methods
提问by kavi temre
I have parent class and a child class, both of having a method m1 with same signature (Override), can I call parent class method in following scenario. I dont want to change child class method.
我有父类和子类,它们都有一个具有相同签名(覆盖)的方法 m1,我可以在以下场景中调用父类方法吗?我不想改变子类方法。
// Super class
public class Parent
{
public void m1()
{
System.out.println("Parent method");
}
}
// Sub class
public class Child extends Parent {
@Override
public void m1() {
System.out.println("Child method");
}
}
// User class
public class Kavi {
public static void main(String[] args) {
Parent p = new Child();
p.m1();
}
}
I want to call parent class m1 method. I know that I can use super in child class method to call its parent method. but I have no right to change the source code of child class. and I have to call it from child class object. please anybody help !!! is it possible in java ??
我想调用父类 m1 方法。我知道我可以在子类方法中使用 super 来调用它的父方法。但我无权更改子类的源代码。我必须从子类对象调用它。请任何人帮忙!!!在java中可以吗??
回答by Charles Stevens
While creating the Object you are using reference of Super class but your object is of child class, so while calling m1() method the overrided method will be invoked. If you want the method of the super class to be invoked then object should be of Super class. As :
在创建对象时,您使用的是超级类的引用,但您的对象是子类,因此在调用 m1() 方法时,将调用覆盖的方法。如果你想调用超类的方法,那么对象应该是超类。作为 :
Parent parent=new Parent();
parent.m1();
OR
或者
you can invoke the super class m1() method from the child class.
您可以从子类调用超类 m1() 方法。
@Override
public void m1() {
super.m1();
System.out.println("Child method");
}
OR ELSE
要不然
import java.lang.reflect.*;
class A {
public void method() {
System.out.println("In a");
}
}
class B extends A {
@Override
public void method() {
System.out.println("In b");
}
}
class M {
public static void main( String ... args ) throws Exception {
A b = new B();
b.method();
b.getClass()
.getSuperclass()
.getMethod("method", new Class[]{} )
.invoke( b.getClass().getSuperclass().newInstance() ,new Object[]{} ) ;
}
}
回答by Steve Chaloner
Without changing the code, you can't do this. You're essentially talking about p.super.m1()
which isn't legal in Java. If you want your parent to act like a parent, don't make it a child.
不改变代码,你不能这样做。您基本上是在谈论p.super.m1()
在 Java 中哪些是不合法的。如果你想让你的父母像父母一样行事,不要把它当成孩子。
If both parent and child are stateless, you could create a facade over them and explicitly manage the state; this would work, but I wouldn't recommend it.
如果父子节点都是无状态的,您可以在它们之上创建一个外观并显式地管理状态;这会起作用,但我不推荐它。
public class Facade extends Parent {
public enum State {PARENT, CHILD};
private final Child delegate;
private State state = State.CHILD;
public Facade(Child delegate) {
this.delegate = delegate;
}
@Override
public void m1() {
if (State.CHILD == state) {
delegate.m1();
} else {
super.m1();
}
}
public void setState(State state) {
this.state = state;
}
}
This is a purely academic exercise - I can't think of a single good reason to do this in the real world. If you're using an OO language, don't fight the OO paradigm!
这是一个纯粹的学术练习 - 我想不出在现实世界中这样做的一个好理由。如果您使用的是 OO 语言,请不要与 OO 范式抗争!
回答by java guy
I think it not possible. There are two ways to call a parent class method
我认为这是不可能的。调用父类方法有两种方式
1.) crate object of parent class as
1.) 父类的 crate 对象为
Parent p = new Parent();
父 p = new Parent();
2.) Use superin child class method as
2.)在子类方法中使用super作为
@Override
public void m1() {
super.m1();
System.out.println("Child method");
}
回答by Nidhi Bhardwaj
Apart from the already mentioned way, you can declare both the methods as static.
除了已经提到的方法,您可以将这两种方法声明为静态方法。
so when you do this
Parent p = new Child();
p.m1();
所以当你这样做的时候
Parent p = new Child();
p.m1();
the static method of parent class would be called and the output will be "Parent method"
将调用父类的静态方法,输出将是“父方法”
Note :The static keyword in Java means that the variable or function is shared between all instances of that class as it belongs to the type, not the actual objects themselves. So if you have a variable: private static int i = 0; and you increment it ( i++ ) in one instance, the change will be reflected in all instances.
注意:Java 中的 static 关键字意味着变量或函数在该类的所有实例之间共享,因为它属于类型,而不是实际对象本身。所以如果你有一个变量:private static int i = 0; 并且您在一个实例中增加它 ( i++ ),更改将反映在所有实例中。
回答by Neeraj Jain
If you can not use super
then instead of creating the child class object you can directly use
如果你不能使用super
then 而不是创建子类对象,你可以直接使用
Parent p = new Parent();
p.m1();
if you can't even modify the code inside main method then I think it's not possible .
如果您甚至无法修改 main 方法中的代码,那么我认为这是不可能的。