Java中的内部类与子类 [英] Inner Classes vs. Subclasses in Java

查看:119
本文介绍了Java中的内部类与子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在这样的情况,在Java中,使用内部类比使用子类更为有利(反之亦然)吗?根据我目前的理解,内部类可以访问外部类的字段和方法.这与使用继承有何不同?

Is there a situation where it is more beneficial to use an inner class over a subclass in Java (or vice-versa)? Based on my current understanding, inner classes have access to the fields and methods of the outer class. How is this any different from using inheritance?

子类通常可以访问所有标记为public和protected的字段/方法.可以使用getter方法在子类中访问父类中标记为private的字段.基于到目前为止我所看到的,当方法被标记为私有时,通常在该类的其他方法中被称为公共或受保护的方法来调用它们.当然,我不是经验丰富的Java程序员,但这似乎是大势所趋.

Subclasses generally have access to all the fields/methods labeled public and protected. Fields labeled private in the parent class can be accessed in the subclass using a getter method. Based off what I've seen thus far, when methods are labeled private, they're usually called in other methods of the class that are labeled either public or protected. Granted, I'm not an experienced Java programmer, but that seems to be the general trend.

根据我目前的理解,选择一个而不是另一个似乎没有任何好处.有人可以提供关于为什么以及何时应该在继承上使用内部类的见解(反之亦然)吗?

Based on my current understanding, there seems to really be no benefit between choosing one over the other. Can someone give insight as to why and when I should use an inner class over inheritance (or vise versa)?

推荐答案

内部类与子类之间存在很大差异:

There are big differences between inner classes and subclasses:

  • 内部类位于相同文件中,而子类可以位于另一个文件中,也可以位于另一个包中.
  • 没有包含它的类的实例,您将无法获得一个内部类的实例.
  • 内部类具有所需的方法,而子类具有其父类的方法.子类当然可以定义其他方法,但是它们将始终具有其父类的方法.
  • inner classes are in the same file, whereas subclasses can be in another file, maybe in another package.
  • You cannot get an instance of an inner class without an instance of the class that contains it.
  • inner classes have the methods they want, whereas subclasses have the methods of their parent class. Subclasses can of course define additional methods, but they'll always have those of their parent.

关于情况:

    当您的大类需要一个(通常为短型)与内部操作相关的类时,并且没有其他人需要它时,可以使用
  • 内部类. Nik G引用的一个很好的例子是LinkedList:它需要一个Node类才能工作,这很简短,并且不需要其他类.因此,Node是LinkedList的内部类.
  • 子类在定义"is-a"关系时使用.想象一下:您想制造不同类型的汽车.它们具有共同的特性和特征:它们全部可以移动,它们全部具有乘客,等等.因此,您可以使用这些共同的事物创建一个抽象类汽车".然后,您可以为每种不同类型的汽车创建一个子类.
  • inner classes are used when your big class needs a (usually short) class, related to its internal operation, and when nobody else needs it. A good example Nik G quoted is the LinkedList: it needs a Node class to work, that is short, and that no other class needs. Therefore Node is an inner class of LinkedList.
  • subclasses are used when you defines a "is-a" reliationship. Picture this: you want to make different types of cars. They have common properties and features: they all can move, they all have passengers, etc. So you create an abstract class "Car" with these common things. And you create a subclass for every different type of car.

这篇关于Java中的内部类与子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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