课程: Programming Concepts for Python

免费学习该课程!

今天就开通帐号,24,700 门业界名师课程任您挑!

Class inheritance

Class inheritance

- When creating new classes, we can pass on attributes and methods from other existing classes through a process called inheritance. This is useful when creating a new class, which represents a specific subset of a previously defined, more generic class. To demonstrate this concept, let's go out to the garage. Say I create a class called vehicle, which contains attributes and methods that are common to all types of vehicles. All vehicles can be described in terms of color, so my class will have a color attribute. Also, all vehicles were manufactured by somebody, so I'll create another data field to store that information. And finally, although it may not be the case for much longer, my vehicles run on gasoline. So I'll need a data field for the level of fuel in the tank. As far as methods go, for any vehicle to be useful, I need to be able to drive it. So I'll define a drive method, and when I use the drive method, it'll…

内容