这是用户在 2024-8-17 22:01 为 https://www.codecademy.com/resources/docs/java/inheritance 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?

Inheritance 继承

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Oct 29, 2021Updated Nov 2, 2021
发布于 2021 年 10 月 29 日更新于 2021 年 11 月 2 日
Contribute to Docs 为文档贡献

Inheritance is an object-oriented programming (OOP) concept by which the properties and behaviors from a parent class are passed on to a child class.
继承是面向对象编程(OOP)中的一个概念,通过它,父类的属性和行为被传递给子类。

In day-to-day life, when something gets passed from a parent to a child, it is said that child inherits from their parent. Similarly, in Java, when some properties or behaviors get passed from parent class to the child class, it is said that child class inherits from a parent class. 

Child classes can have properties and methods of its own as well. 

They can also override the behaviors (methods) of the parent class. This is called polymorphism (another OOP concept). 

We can implement inheritance by using the extends keyword. 

Syntax 

Here’s the basic syntax for implementing inheritance: 

public class Child extends Parent {
// Class body
}

Example 

public class Vehicle {
private int numWheels;
private String engineNum;
private String maker;
private String color;
public int getNumWheels() {
return numWheels;
}
public void setNumWheels(int wheels) {
this.numWheels = wheels;
}
public String getEngineNum() {
return engineNum;
}
public void setEngineNum(String engineNum) {
this.engineNum = engineNum;
}
// Similar getters and setters for other properties
}

In the code above, Vehicle is a class that has properties and behaviors. This is more of a generic class. 

While creating a specific class, for example, Car which has these properties and behaviors and some extra properties, instead of defining all these properties again in the new class, we can make use of inheritance to pass on the properties defined in Vehicle class and access them in Car class. This would make code written in Vehicle class reusable. 

Similarly, we can make another class, for example, Airplane, which can access the properties present in Vehicle class.
同样地,我们可以创建另一个类,例如Airplane,它可以访问Vehicle类中的属性。

Here’s a Car class which inherits Vehicle class:
这是一个继承Vehicle类的Car类:

public class Car extends Vehicle {
// Vehicle class properties and methods accessible here
private int airbagCount;
public int getAirbagCount() {
return airbagCount;
}
public void setAirbagCount(int airbagCount) {
this.airbagCount = airbagCount;
}
}

Below is how you can access properties and methods of parent class:
以下是如何访问父类的属性和方法:

public static void main(String args[]) {
Car car = new Car();
car.setNumWheels(4); // Parent class method. This would set the wheel count to 4
System.out.println(car.getNumWheels()); // Parent class method. Prints numWheels
car.setAirbagCount(2); // Child class method. This would set the airbag count
System.out.println(car.getAirbagCount()); // Child class method. Prints airbag count
}

Types of Inheritance 继承的类型

Single Inheritance 单一继承

Single inheritance is when a child class inherits from a parent class.
单一继承是指子类从父类继承。

public class Parent {
// Parent class properties and methods
}
public class Child extends Parent {
// Parent class properties and methods accessible here due to inheritance
// Child class properties and methods
}

Multilevel Inheritance 多级继承

Multilevel inheritance is when a child class inherits from a parent class which in turn inherits from another parent class.
多级继承是指子类继承自一个父类,而这个父类又继承自另一个父类。

public class Parent {
// Parent class properties and methods
}
public class Child extends Parent {
// Parent class properties and methods accessible here due to inheritance
// Child class properties and methods
}
public class GrandChild extends Child {
// Parent class and Child class properties and methods accessible here due to inheritance
// GrandChild class properties and methods
}

Hierarchical Inheritance 层次继承

Hierarchical inheritance is when a child class inherits from a parent class and there is another sibling class that also inherits from the same parent class.
层次继承是指一个子类继承自父类,同时还有另一个子类也继承自同一个父类。

public class Parent {
// Parent class properties and methods
}
public class Child extends Parent{
// Parent class properties and methods accessible here due to inheritance
// Child class properties and methods
}
public class Sibling extends Parent{
// Parent class properties and methods accessible here due to inheritance
// Child class properties and methods are not accessible here
// Sibling class properties and methods
}

Advantages of Inheritance
继承的优势

  • Code reusability: Same properties and methods of a class can be used by inheriting that class.
    代码重用性:通过继承,一个类的属性和方法可以被多个子类使用。
  • Lower maintenance cost: If a piece of code needs to be updated, it can be done at minimal place if inheritance is used.
    维护成本低:如果需要更新代码,使用继承可以在最少的地方进行修改。
  • Easier to add new features: If a same new feature needs to be added to multiple classes, through inheritance it can be added in parent class and all the child classes inheriting this parent class would get that feature instantly.
    更易于添加新功能:如果需要向多个类添加相同的新功能,通过继承可以在父类中添加该功能,所有继承此父类的子类将立即获得该功能。

All contributors 所有贡献者

Looking to contribute? 想要贡献吗?

Learn Java on Codecademy 在 Codecademy 学习 Java

×
拖拽到此处
图片将完成下载
由AIX智能下载器(图片/视频/音乐/文档) Pro提供