简体   繁体   English

有没有办法从java中的子类对象调用父类方法而不修改方法

[英]is there any way to call parent class method from child class object in java without modifying methods

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. 我有父类和子类,都有一个方法m1具有相同的签名(覆盖),我可以在下面的场景中调用父类方法。 I dont want to change child class method. 我不想改变子类方法。

// 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. 我想调用父类m1方法。 I know that I can use super in child class method to call its parent method. 我知道我可以在子类方法中使用super来调用它的父方法。 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 ?? 是不是可能在java?

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. 在创建Object时,您使用的是Super类的引用,但您的对象是子类,因此在调用m1()方法时,将调用覆盖的方法。 If you want the method of the super class to be invoked then object should be of Super class. 如果你想调用超类的方法,那么object应该是Super类。 As : 作为:

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[]{}                  ) ;

}
}

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");
    }

Without changing the code, you can't do this. 在不更改代码的情况下,您无法执行此操作。 You're essentially talking about p.super.m1() which isn't legal in Java. 你基本上是在谈论p.super.m1() ,这在Java中是不合法的。 If you want your parent to act like a parent, don't make it a child. 如果您希望您的父母像父母一样行事,请不要让它成为孩子。

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范例作斗争!

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方法中的代码那么我认为这是不可能的。

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(); 所以当你这样做时, 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. 注意: Java中的static关键字表示变量或函数在该类的所有实例之间共享,因为它属于类型,而不是实际的对象本身。 So if you have a variable: private static int i = 0; 所以如果你有一个变量:private static int i = 0; and you increment it ( i++ ) in one instance, the change will be reflected in all instances. 并且在一个实例中递增它(i ++),更改将反映在所有实例中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM