父类的子类的 Java 模式匹配

问题描述

我有一个 Parent 类和一个 extends Parent 名为 Child 的子类。我有一个方法 run 可以采用 ParentChild 实例。我使用代码 Child 创建了一个 Parent child = new Child() 实例。请参阅下面的代码

我希望代码输出“你是孩子”,但我收到的是“你是父母”。我理解这背后的原因,但是我想知道我可以使用什么来执行 Childrun 实现。

在我的系统中,我将有多个继承自 Parent 的“子”类和满足每个“子”类的多个 run 方法。因此,转换将不起作用,因此如果您在解决方案中包含转换,恐怕它对我没有帮助。我对此的灵感或多或少来源于 Haskell 等语言中的模式匹配。

public class Child extends Parent {
    public static void main(String[] args) {
        Parent child = new Child();
        run(child);
    }

    public static void run(Child child) {
        System.out.println("You are a child");
    }

    public static void run(Parent parent) {
        System.out.println("You are a parent");
    }
}

解决方法

不要使用静态方法。将方法添加到您的对象并覆盖它们。

class Parent {

   public void run() {
      System.out.println("You are a parent");
   }
}

class Child extends Parent {
   
   @Override
   public void run() {
      System.out.println("You are a child");
   }
}


class Test {
    public static void main(String[] args) {
        Parent child = new Child();
        child.run(); // <- prints "you are a child"
    }
}

证明:https://ideone.com/v1Ijhy

,

调用哪个方法的评估是编译的一部分。在您的代码中,您将 new Child 存储为 Parent 变量,因此从编译器的角度来看,它只知道您有一个 Parent,因此它解析对该方法的调用。

如果您希望显示“You are a child”,则需要将变量声明为 Child,或者使用一种方法对类型进行一些运行时检查(类似于 {{1 }}).