java父类子类继承_Java:继承,超类和子类

Bertha 。 2022-12-06 15:39 384阅读 0赞

java父类子类继承

An important concept in object-oriented programming is inheritance. It provides a way for objects to define relationships with each other. As the name suggests, an object is able to inherit characteristics from another object.

面向对象编程中的一个重要概念是继承。 它为对象提供了一种定义彼此之间关系的方法。 顾名思义,一个对象能够从另一个对象继承特征。

In more concrete terms, an object is able to pass on its state and behaviors to its children. For inheritance to work, the objects need to have characteristics in common with each other.

更具体而言,对象能够将其状态和行为传递给子对象。 为了使继承起作用,对象需要具有彼此相同的特征。

In Java, classes can be taken from other classes, which can be taken from others, and so on. This is because they can inherit features from the class above it, all the way up to the topmost Object class.

在Java中 ,可以从其他类中获取类,而从其他类中获取类,依此类推。 这是因为它们可以从其上方的类继承功能,一直到最顶层的Object类。

Java继承的示例 ( An Example of Java Inheritance )

Let’s say we make a class called Human that represents our physical characteristics. It’s a generic class that could represent you, me, or anyone in the world. Its state keeps track of things like the number of legs, number of arms, and blood type. It has behaviors like eat, sleep, and walk.

假设我们制作了一个名为Human的类,它代表了我们的身体特征。 这是一个通用类,可以代表您,我或世界上任何人。 它的状态跟踪腿数,臂数和血型等信息。 它具有饮食,睡眠和散步等行为。

Human is good for getting an overall sense of what makes us all the same but it can’t, for instance, tell me about gender differences. For that, we’d need to make two new class types called Man and Woman. The state and behaviors of these two classes will differ from each other in a lot of ways except for the ones that they inherit from Human.

人类有利于全面了解使我们保持一致的原因,但是例如,它不能告诉我性别差异。 为此,我们需要创建两个新的类类型,分别称为Man和Woman。 这两个类的状态和行为将在很多方面彼此不同,除了它们是从人类继承的。

Therefore, inheritance allows us to encompass the parent class’ state and behaviors into its child. The child class can then extend the state and behaviors to reflect the differences it represents. The most important aspect of this concept to remember is that the child class is a more specialized version of the parent.

因此,继承使我们能够将父类的状态和行为包含在其子类中。 然后,子类可以扩展状态和行为以反映其表示的差异。 要记住的这一概念的最重要方面是,子类是父类的更专门的版本。

什么是超类? ( What’s a Superclass? )

In the relationship between two objects, a superclass is the name given to the class that is being inherited from. It sounds like a super duper class, but remember that it’s the more generic version. Better names to use might be base class or simply parent class.

在两个对象之间的关系中,超类是给继承自该类的名称。 听起来像是超级duper类,但请记住,它是更通用的版本。 使用更好的名称可能是基类或简单的父类。

To take a more real-world example this time, we could have a superclass called Person. Its state holds the person’s name, address, height, and weight, and has behaviors like go shopping, make the bed, and watch TV.

这次以更真实的示例为例,我们可以有一个称为Person的超类。 它的状态包含该人的姓名,地址,身高和体重,并且具有诸如逛街,铺床和看电视的行为。

We could make two new classes that inherit from Person called Student and Worker. They are more specialized versions because although they have names, addresses, watch TV, and go shopping, they also have characteristics that are different from each other.

我们可以创建两个继承自Person的新类,称为Student和Worker。 它们是更专业的版本,因为尽管它们具有名称,地址,看电视和购物,但它们也具有彼此不同的特征。

Worker could have a state that holds a job title and place of employment whereas Student might hold data on an area of study and an institution of learning.

工人可能拥有一个拥有职务和工作地点的州,而学生可能拥有一个研究领域和一个学习机构的数据。

超类示例: ( Superclass Example: )

Imagine you define a Person class:

假设您定义了一个Person类:

  1. public class Person
  2. {
  3. }

A new class can be created by extending this class:

可以通过扩展此类来创建一个新类:

  1. public class Employee extends Person
  2. {
  3. }

The Person class is said to be the superclass of the Employee class.

据说Person类是Employee类的超类。

什么是子类? ( What’s a Subclass? )

In the relationship between two objects, a subclass is the name given to the class that is inheriting from the superclass. Although it sounds a little drabber, remember that it’s a more specialized version of the superclass.

在两个对象之间的关系中,子类是赋予从超类继承的类的名称。 尽管听起来有些傻瓜,但请记住,它是超类的一个更专业的版本。

In the previous example, Student and Worker are the subclasses.

在前面的示例中,Student和Worker是子类。

Subclasses can also be known as derived classes, child classes, or extended classes.

子类也可以称为派生类,子类或扩展类。

我可以拥有几个子类? ( How Many Subclasses Can I Have? )

You can have as many subclasses as you want. There is no limitation to how many subclasses a superclass can have. Likewise, there isn’t a limitation on the number of levels of inheritance. A hierarchy of classes can be built upon a certain area of commonality.

您可以根据需要拥有任意多个子类。 对于超类可以具有多少个子类没有限制。 同样,继承级别的数量也没有限制。 类的层次结构可以建立在特定的公共领域上。

In fact, if you look at the Java API libraries you will see many examples of inheritance. Every class in the APIs is inherited from a class called java.lang.Object. For example, any time you use a JFrame object, you’re at the end of a long line of inheritance:

实际上,如果您查看Java API库,将会看到许多继承示例。 API中的每个类均继承自名为java.lang.Object的类。 例如,每当您使用JFrame对象时,您就处于一长串继承的末尾:

  1. java.lang.Object
  2. extended by java.awt.Component
  3. extended by java.awt.Container
  4. extended by java.awt.Window
  5. extended by java.awt.Frame
  6. extended by javax.swing.JFrame

In Java, when a subclass inherits from a superclass, it’s known as “extending” the superclass.

在Java中,当子类从超类继承时,称为“扩展”超类。

我的子类可以继承许多超类吗? ( Can My Subclass Inherit From Many Superclasses? )

No. In Java, a subclass can only extend one superclass.

不可以。在Java中,子类只能扩展一个超类。

为什么要使用继承? ( Why Use Inheritance? )

Inheritance allows programmers to reuse code they’ve already written. In the Human class example, we don’t need to create new fields in the Man and Woman class to hold the blood type because we can use the one inherited from the Human class.

继承允许程序员重用他们已经编写的代码。 在“人类”类示例中,我们不需要在“男人”和“女人”类中创建新字段来保存血液类型,因为我们可以使用从“人类”类继承的字段。

Another benefit of using inheritance is that it lets us treat a subclass as if it was a superclass. For example, let’s say a program has created multiple instances of the Man and Woman objects. The program might need to call the sleep behavior for all these objects. Because the sleep behavior is a behavior of the Human superclass, we can group all the Man and Woman objects together and treat them as if they were Human objects.

使用继承的另一个好处是,它使我们可以将子类视为超类。 例如,假设一个程序创建了Man和Woman对象的多个实例。 该程序可能需要调用所有这些对象的睡眠行为。 因为睡眠行为是人类超类的行为,所以我们可以将所有“男人”和“女人”对象组合在一起,并将它们视为“人类”对象。

翻译自: https://www.thoughtco.com/what-is-inheritance-2034264

java父类子类继承

发表评论

表情:
评论列表 (有 0 条评论,384人围观)

还没有评论,来说两句吧...

相关阅读

    相关 golang继承——接口

    从面向对象的角度上看,总会有一些使用子类不想做,希望父类去做的事情,在java这样的纯面向对象的语言中,方法就是使用父类和子类,子类通过继承父类的方法,实现子类自己的属性,如果