Java 中的内部类与子类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33421432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inner Classes vs. Subclasses in Java
提问by Joel J.
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?
是否存在在 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.
子类通常可以访问所有标记为 public 和 protected 的字段/方法。可以使用 getter 方法在子类中访问父类中标记为私有的字段。根据我目前所见,当方法被标记为私有时,它们通常会在类的其他标记为 public 或 protected 的方法中调用。诚然,我不是一个有经验的 Java 程序员,但这似乎是大势所趋。
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)?
根据我目前的理解,选择一个而不是另一个似乎真的没有任何好处。有人可以深入了解为什么以及何时应该使用内部类而不是继承(反之亦然)?
回答by ElectronWill
There are big differences between inner classes and subclasses:
内部类和子类之间有很大的区别:
- inner classes are in the samefile, 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.
- 内部类在同一个文件中,而子类可以在另一个文件中,也许在另一个包中。
- 如果没有包含它的类的实例,则无法获得内部类的实例。
- 内部类有他们想要的方法,而子类有他们父类的方法。子类当然可以定义额外的方法,但它们将始终具有其父类的方法。
About the situation:
关于情况:
- 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 allcan move, they allhave passengers, etc. So you create an abstract class "Car" with these common things. And you create a subclass for every different type of car.
- 当你的大类需要一个(通常是短的)类,与其内部操作相关,并且没有其他人需要它时,使用内部类。 Nik G 引用的一个很好的例子是 LinkedList:它需要一个 Node 类来工作,它很短,其他类不需要。因此 Node 是 LinkedList 的内部类。
- 当您定义“is-a”关系时使用子类。想象一下:你想制造不同类型的汽车。他们有共同的属性和特点:它们都可以移动,它们都具有乘客,等于是您创建抽象类的“驾驶”这些共同的东西。您为每种不同类型的汽车创建一个子类。
回答by Mage Xy
If you use inheritance, you define an "is-a" relationship between the two classes. That is, ChildClass
is aParentClass
.
如果使用继承,则在两个类之间定义“is-a”关系。也就是说,ChildClass
是一个ParentClass
。
On the other hand, inner classes are not always directly related to the outer classes. The main reason to have an inner class is for the outer class to make use of its features without letting other classes know about it.
另一方面,内部类并不总是与外部类直接相关。拥有内部类的主要原因是外部类可以在不让其他类知道的情况下使用其功能。
The best example I can think of off the top of my head is a LinkedList
. It uses some sort of private Node
class to store the contents of each element. Of course, a single Node
is not a LinkedList
... but a LinkedList
can't work unless it it is made up of Nodes
. There's also no reason for any class other than LinkedList
to know that the Node
class exists.
我能想到的最好的例子是LinkedList
. 它使用某种私有Node
类来存储每个元素的内容。当然,singleNode
不是LinkedList
... 但是 aLinkedList
不能工作,除非它是由Nodes
. 除了LinkedList
知道Node
该类存在之外,任何类也没有任何理由。
回答by MrBlueSky
A subclass inherits it's parent class' variables and methods, an inner class doesn't.
子类继承其父类的变量和方法,而内部类则不会。
Therefor, you would want to use a subclass when the child class should have it's parent's members and use inner class when you only need it to perform it's part.
因此,当子类应该拥有它的父类成员时,你会想要使用子类,而当你只需要它来执行它的一部分时,你会想要使用内部类。
e.g you would use a an inner class named Node in a class named List so you can use Node as a member.
例如,您将在名为 List 的类中使用名为 Node 的内部类,以便您可以使用 Node 作为成员。
e.g you would use a subclass named Mercedes in a class named Car, as a Mercedes should inherit the members of a Car and override Car's methods if needed.
例如,您将在名为 Car 的类中使用名为 Mercedes 的子类,因为 Mercedes 应该继承 Car 的成员并在需要时覆盖 Car 的方法。
回答by Aviad
This is simple.. But inner class and sub class are not the same..
这很简单.. 但是内部类和子类不一样..
Sub classes uses when you have inheritance logic. For example Rectangle is Shape so: Rectangle can inherit from Shape
当您具有继承逻辑时使用子类。例如 Rectangle 是 Shape 所以: Rectangle 可以从 Shape 继承
Inner classes are used when you have a class that you need to use only in particular class. That way no one will use the class unless he is in the class that need to use the inner class. For example: You have class A Class a has a Map. The Key class is class you created to define compound key class and it is used only inside of A. So Key can be inner class.. This way you can reduce files(Classes) so other developer won't need to handle and understand unless he is using A
当您有一个仅需要在特定类中使用的类时,将使用内部类。这样,除非他在需要使用内部类的类中,否则没有人会使用该类。例如:你有一个类 A 类有一个地图。Key 类是您创建的用于定义复合键类的类,它仅在 A 内部使用。因此 Key 可以是内部类。他正在使用 A
Hope that make sense
希望有意义
回答by Manish Mallavarapu
A subclass essentially shares an "is-a" relationship with its parent, whereas an Inner class shares a "has-a" relationship. For example, we have class yolk which is an inner class of Egg class, then we say Egg class "has a" yolk class. The object of yolk class may be as a member of Egg class, but there is no link of their feature. Also, say we have class Dog extends class Animal, so Dog class "is a" Animal class, and Dog class will inherit all features of Animal, and possibly have some more.
子类本质上与其父类共享“is-a”关系,而内部类共享“has-a”关系。例如,我们有 yolk 类,它是 Egg 类的内部类,那么我们说 Egg 类“有一个”蛋黄类。yolk 类的对象可能是Egg 类的成员,但没有其特征的链接。另外,假设我们有类 Dog 扩展了类 Animal,因此 Dog 类“是”Animal 类,而 Dog 类将继承 Animal 的所有特性,并且可能还有更多特性。