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?
5 个解决方案
#1
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 :
在创建Object时,您使用的是Super类的引用,但您的对象是子类,因此在调用m1()方法时,将调用覆盖的方法。如果你想调用超类的方法,那么object应该是Super类。作为:
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[]{} ) ;
}
}
#2
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 super in child class method as
2.)在子类方法中使用super作为
@Override
public void m1() {
super.m1();
System.out.println("Child method");
}
#3
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范例作斗争!
#4
If you can not use super
then instead of creating the child class object you can directly use
如果你不能使用super,那么你可以直接使用而不是创建子类对象
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方法中的代码那么我认为这是不可能的。
#5
Apart from the already mentioned way, you can declare both the methods as static.
除了已经提到的方法之外,您可以将这两个方法声明为static。
so when you do this Parent p = new Child(); p.m1();
所以当你这样做时,父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 ++),更改将反映在所有实例中。
#1
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 :
在创建Object时,您使用的是Super类的引用,但您的对象是子类,因此在调用m1()方法时,将调用覆盖的方法。如果你想调用超类的方法,那么object应该是Super类。作为:
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[]{} ) ;
}
}
#2
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 super in child class method as
2.)在子类方法中使用super作为
@Override
public void m1() {
super.m1();
System.out.println("Child method");
}
#3
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范例作斗争!
#4
If you can not use super
then instead of creating the child class object you can directly use
如果你不能使用super,那么你可以直接使用而不是创建子类对象
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方法中的代码那么我认为这是不可能的。
#5
Apart from the already mentioned way, you can declare both the methods as static.
除了已经提到的方法之外,您可以将这两个方法声明为static。
so when you do this Parent p = new Child(); p.m1();
所以当你这样做时,父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 ++),更改将反映在所有实例中。