Книга: Fedora™ Unleashed, 2008 edition

Class Inheritance

Class Inheritance

Python allows you to reuse your code by inheriting one class from one or more others. For example, cars, trucks, and motorbikes are all vehicles, and so share a number of similar properties. In that scenario, you would not want to have to copy and paste functions between them; it would be smarter (and easier!) to have a vehicle class that defines all the shared functionality and then inherit each vehicle from that.

Consider the following code:

class car(object):
 color = "black"
 speed = 0
 def accelerateTo(self, speed):
  self.speed = speed
 def setColor(self, color):
  self.color = color
mycar = car()
print mycar.color

This creates a car class with a default color and also provides a setColor() function so that people can change their own colors. Now, what do you drive to work? Is it a car? Sure it is, but chances are it is a Ford, or a Dodge, or a Jeep, or some other make — you don't get cars without a make. On the other hand, you do not want to have to define a class Ford, give it the methods accelerateTo(), setColor(), and however many other methods a basic car has and then do exactly the same thing for Ferrari, Nissan, and so on.

The solution is to use inheritance: Define a car class that contains all the shared functions and variables of all the makes and then inherit from that. In Python, you do this by putting the name of the class from which to inherit inside parentheses in the class declaration, like this:

class car(object):
 color = "black"
 speed = 0
 def accelerateTo(self, speed):
  self.speed = speed
 def setColor(self, color):
  self.color = color
class ford(car): pass
class nissan(car): pass
mycar = ford()
print mycar.color

The pass directive is an empty statement — it means the class contains nothing new. However, because the ford and nissan classes inherit from the car class, they get color, speed, accelerateTo(), and setColor() provided by their parent class. Note that you do not need object after the classnames for ford and nissan because they are inherited from an existing class: car.

By default, Python gives you all the methods the parent class has, but you can override that by providing new implementations for the classes that need them. For example:

class modelt(car):
 def setColor(self, color):
  print "Sorry, Model Ts come only in black!"
myford = ford()
ford.setColor("green")
mycar = modelt()
mycar.setColor("green")

The first car is created as a Ford, so setColor() works fine because it uses the method from the car class. However, the second car is created as a Model T, which has its own setColor() method, so the call will fail.

This provides an interesting scenario: What do you do if you have overridden a method and yet want to call the parent's method also? If, for example, changing the color of a Model T was allowed but just cost extra, you would want to print a message saying, "You owe $50 more," but then change the color. To do this, you need to use the class object from which the current class is inherited — car, in this example. Here's an example:

class modelt(car):
 def setColor(self, color):
  print "You owe $50 more"
car.setColor(self, color)
mycar = modelt()
mycar.setColor("green")
print mycar.color

That prints the message and then changes the color of the car.

Оглавление книги


Генерация: 0.614. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз