【问题标题】:How do I call a childs class method from a parent abstract class [duplicate]如何从父抽象类调用子类方法[重复]
【发布时间】:2020-08-09 13:39:43
【问题描述】:

大家好,我是 Java 新手,还在学习 OOP 原理。直奔问题。所以我有一个抽象类和两个继承自它的类。它看起来像这样:

public abstract class A { ... }
public class B extends A { 
    ...
    void methodB() { ... }

public class C extends A {
    ...
    void methodC() { ... }

现在我做:

A a = null;
if (that)
    a = new B();
else
    a = new C();

这个这一点上,我想做 a.methodB();a.methodC(); (当然不是两者兼有,因为我创造了一个孩子) 但我不能两者都做。

【问题讨论】:

    标签: java intellij-idea


    【解决方案1】:

    您违反了 OOPS 概念。因为父类不需要知道子行为。您可以做什么在类 A. 中创建一个抽象方法,然后调用方法 A.something()。只需在 B 和 C 类中覆盖它的某个方法,并在 methodB 和 methodC 中给出相同的实现。

    public abstract class A { abstract something(); }
    
    public  class B { overide something(){} }
    
    public  class C { overide something(){} }
    
    A a = null;
    if (that)
      a = new B();
    else
      a = new C();
    
    if that is true then it equivalent to B.something() else C.something(). You can read more about method overriding/runtime polymorphism.  
    

    【讨论】:

    • B和C不应该标记为抽象吗?
    • 是的,更新后的评论 B 和 C 不需要是 abstract 。谢谢
    【解决方案2】:

    如何从父抽象类调用子类方法

    这是不可能的。使用超类型的引用,您可以根据access control mechanism 直接访问超类型的成员。但是,您可以将引用强制转换为子类并访问子类的成员。

    如何查看实例类型?

    使用instanceof 运算符。


    演示:

    abstract class SomeA {
        abstract void commonMethod();
    }
    
    class SomeB extends SomeA {
        void methodB() {
            System.out.println("methodB()");
        }
    
        @Override
        public void commonMethod() {
            System.out.println("SomeB's implementation of commonMethod()");
        }
    }
    
    class SomeC extends SomeA {
        void methodC() {
            System.out.println("methodC()");
        }
    
        @Override
        public void commonMethod() {
            System.out.println("SomeC's implementation of commonMethod()");
        }
    }
    
    public class Testing {
        public static void main(String[] args) {
            SomeB bb = new SomeB();
            bb.commonMethod();
            bb.methodB();
    
            SomeC cc = new SomeC();
            cc.commonMethod();
            cc.methodC();
    
            SomeA b = new SomeB();
            SomeA c = new SomeC();
            System.out.println(b instanceof SomeB);
            System.out.println(c instanceof SomeB);
            System.out.println(b instanceof SomeC);
            System.out.println(c instanceof SomeC);
    
            b.commonMethod();
            // b.methodB();//Error as b is a reference of super type
            ((SomeB) b).commonMethod();// Fine after casting the super type reference to SomeB
    
            c.commonMethod();
            // c.methodC();//Error as c is a reference of super type
            ((SomeC) c).commonMethod();// Fine after casting the super type reference to SomeC
        }
    }
    

    输出:

    SomeB's implementation of commonMethod()
    methodB()
    SomeC's implementation of commonMethod()
    methodC()
    true
    false
    false
    true
    SomeB's implementation of commonMethod()
    SomeB's implementation of commonMethod()
    SomeC's implementation of commonMethod()
    SomeC's implementation of commonMethod()
    

    【讨论】:

      【解决方案3】:

      你可以这样做:

      if (a instanceof B) ((B)a).methodB();
      else if (a instanceof C) ((C)a).methodC();
      

      但这是一种反模式,我建议在 A abstract class 中将 methodA 定义为 abstract 方法。 BC 将同时实现它,然后您可以轻松调用 a.methodA(),然后将调用 methodA 的正确实现。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-06-20
        • 2020-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-13
        相关资源
        最近更新 更多