当涉及内部类时,Java继承如何工作 [英] How does Java inheritance work when inner classes are involved

查看:158
本文介绍了当涉及内部类时,Java继承如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当内部类存在时,我无法理解继承在Java中的工作原理。我正在研究一个子类需要稍微改变它的父类内部类的功能的东西。我在下面提出了一个更简单,更加讽刺的例子。

I am having trouble understanding how inheritance works in Java when inner classes are present. I'm currently working on something where a child class needs to slightly change the functionality of the inner class of it's parent. I've come up with an simpler, analagous example below.

我希望这段代码打印我是一个ChildClass.InnerClass,而是打印
我是ParentClass.InnerClass。为什么是这样?另外,如果我将main
中的obj对象更改为ChildClass类型,则输出将更改为我是ChildClass.InnerClass。这是为什么?

I expected this code to print "I am a ChildClass.InnerClass" but instead it prints "I am a ParentClass.InnerClass". Why is this? Also, if I change the obj object in main to be of type ChildClass then the output changes to "I am a ChildClass.InnerClass". Why is this?

一般来说,改变对象父类内部对象行为的推荐方法是什么?

In general, what is the recommended way of altering the behavior of an object's parent class's inner object?

class InnerClassTest {
   //-----------------------------------------------------------------------
   // PARENT CLASS
   class ParentClass {
      public ParentClass() {
         x = new InnerClass();
      }

      InnerClass x;

      class InnerClass {
         public void speak() {
            System.out.println("I am a ParentClass.InnerClass");
         }
      }
   }

   //-----------------------------------------------------------------------
   // CHILD CLASS
   class ChildClass extends ParentClass {
      public ChildClass() {
         x = new InnerClass();
      }

      InnerClass x;

      class InnerClass extends ParentClass.InnerClass {
         public void speak() {
            System.out.println("I am a ChildClass.InnerClass");
         }
      }
   }

   //-----------------------------------------------------------------------
   // MAIN
   public static void main(String[] args) {
      ParentClass obj = (new InnerClassTest()).new ChildClass();
      obj.x.speak();
   }
}


推荐答案

变量不会像方法那样覆盖。

Variable are not "overriden" as methods are.

在你的通话中,你期望 x 成为 Child 是一个,但不是因为 x 是变量,而不是方法。

In your call, you expected x to be the Child's one but it isn't because x is a variable, not a method.

但要注意:你的引用类型是 ParentClass 所以 obj.x 指向 ParentClass InnerClass 属性,即使 parentClass 后面的真实实例是 ChildClass

But pay attention: Your reference type is ParentClass so obj.x points to the ParentClass's InnerClass attribute even though the real instance behind parentClass is a ChildClass!

为了显示您的预期句子,您必须将类型引用更改为 ChildClass

In order to display your expected sentence, you have to change the type reference to ChildClass:

public static void main(String[] args) {
      ChildClass obj = (new InnerClassTest()).new ChildClass();
      obj.x.speak();
}

为了更好地理解这个概念,尝试在<$ c中定义一个方法$ c> ParentClass 和 ChildClass 类:

To better understand the concept, try to define a method in both ParentClass and ChildClass classes:

public InnerClass getInnerClass(){
  return x;
}  

并使 x 私有。

以便覆盖概念适用。

在这种情况下,您的最终通话将是:

Your final call would be in this case:

ParentClass obj = (new InnerClassTest()).new ChildClass();
obj.getInnerClass().speak();

要改变内部类的行为,请考虑模板方法模式或更好:策略模式(因为更尊重DIP)

To alter the behavior of the inner classes, think of Template method pattern or better: Strategy pattern (since more respectful of DIP)

这篇关于当涉及内部类时,Java继承如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆