课程: Programming Concepts for Python
免费学习该课程!
今天就开通帐号,24,700 门业界名师课程任您挑!
Overriding inherited methods
- Nowadays, not all cars run on gasoline, some are electric. Electric cars have a radio and windows, so they could inherit those methods from the car class. An electric car also has a color and manufacturer. This is a white Nissan, so it should inherit those data fields from the vehicle class as well. Now, this is where I run into a bit of a problem. The drive method for the vehicle class uses gas, but my electric car doesn't. It uses electricity so that drive method doesn't make sense, to create an electric car subclass that inherits from the car and vehicle classes, I need to replace that gas powered drive method from the vehicle class with a specially designed eco-friendly, electric powered drive method. Let's revisit the garage full of classy vehicles from the previous video, which defines a base class for a vehicle and two subclasses for a car and a motorcycle. Now let's create a new class for electric cars named…