面向对象程序设计最重要的目的之一就是创建稳定的,可靠的,可重用的代码。如果你为每个对象都创建一个新类,你也很难写出可重用的代码。
在 Python 和任意支持面向对象编程的语言中,一个类可以继承另一个类。这也意味着你可以在旧类的基础上创建新类。新类继承旧类中所有的属性和行为。
新类可以重写覆盖任一继承自旧类的属性或行为。也可以添加新的属性和行为。旧类被称为父类,新类被称为父类的孩子。父类也被称为 superclass ,子类被称为 subclass。
子类继承父类的所有属性和行为,但在子类中定义的属性和行为不能为父类所用。这虽然看起来很明显,但也有必要说明一下。
这也以为着子类可以重写父类中的方法。如果子类中定义了一个已经在父类中出现的方法,子类对象会调用子类的方法而不是父类的。
为了更好的理解继承,让我们看一个基于 Rocket 类继承的例子。
SpaceShuttle 类
如果你想模拟一个航天飞船,你可能要写一个新的类。但是航天飞机是火箭的一种特殊形式。你可以继承 Rocket 类,添加一些新的属性和方法,生成一个 Shuttle 类而不是新建一个类。
航天飞船的一个重要特性是他可以重用。因此我们添加记录航天飞船服役的次数。其他基本和 Rocket 类相同。
实现一个 Shuttle 类,如下所示:
from math import sqrt
class Rocket():
# Rocket simulates a rocket ship for a game,
# or a physics simulation.
def __init__(self, x=0, y=0):
# Each rocket has an (x,y) position.
self.x = x
self.y = y
def move_rocket(self, x_increment=0, y_increment=1):
# Move the rocket according to the paremeters given.
# Default behavior is to move the rocket up one unit.
self.x += x_increment
self.y += y_increment
def get_distance(self, other_rocket):
# Calculates the distance from this rocket to another rocket,
# and returns that value.
distance = sqrt((self.x-other_rocket.x)**2+(self.y-other_rocket.y)**2)
return distance
class Shuttle(Rocket):
# Shuttle simulates a space shuttle, which is really
# just a reusable rocket.
def __init__(self, x=0, y=0, flights_completed=0):
super().__init__(x, y)
self.flights_completed = flights_completed
shuttle = Shuttle(10,0,3)
print(shuttle)
当一个子类要继承父类时,在定义子类的圆括号中填写父类的类名:
class NewClass(ParentClass):
新类的 __init__() 函数需要调用新类的 __init__() 函数。新类的 __init__() 函数接受的参数需要传递给父类的 __init__() 函数。由 super().__init__() 函数负责:
class NewClass(ParentClass):
def __init__(self, arguments_new_class, arguments_parent_class):
super().__init__(arguments_parent_class)
# Code for initializing an object of the new class.
super()函数会自动将self参数传递给父类。你也可以通过用父类的名字实现,但是需要手动传递self参数。如下所示:
class Shuttle(Rocket):
# Shuttle simulates a space shuttle, which is really
# just a reusable rocket.
def __init__(self, x=0, y=0, flights_completed=0):
Rocket.__init__(self, x, y)
self.flights_completed = flights_completed
这样写看起来可读性更高,但是我们更倾向于用 super() 的语法。当你使用 super() 的时候,不必关心父类的名字,以后有改变时会变得更加灵活。而且随着继承的学习,以后可能会出现一个子类继承自多个父类的情况,使用 super() 语法就可以在一行内调用所有父类的 __init__() 方法。
新的 Shuttle 类已经被定义好了,你可以使用 Rocket 类中的方法,计算距离等。
接下来运用定义好的子类。其中使用了 randint 函数用来生成随机数。示例如下:
from math import sqrt
from random import randint
class Rocket():
# Rocket simulates a rocket ship for a game,
# or a physics simulation.
def __init__(self, x=0, y=0):
# Each rocket has an (x,y) position.
self.x = x
self.y = y
def move_rocket(self, x_increment=0, y_increment=1):
# Move the rocket according to the paremeters given.
# Default behavior is to move the rocket up one unit.
self.x += x_increment
self.y += y_increment
def get_distance(self, other_rocket):
# Calculates the distance from this rocket to another rocket,
# and returns that value.
distance = sqrt((self.x-other_rocket.x)**2+(self.y-other_rocket.y)**2)
return distance
class Shuttle(Rocket):
# Shuttle simulates a space shuttle, which is really
# just a reusable rocket.
def __init__(self, x=0, y=0, flights_completed=0):
super().__init__(x, y)
self.flights_completed = flights_completed
# Create several shuttles and rockets, with random positions.
# Shuttles have a random number of flights completed.
shuttles = []
for x in range(0,3):
x = randint(0,100)
y = randint(1,100)
flights_completed = randint(0,10)
shuttles.append(Shuttle(x, y, flights_completed))
rockets = []
for x in range(0,3):
x = randint(0,100)
y = randint(1,100)
rockets.append(Rocket(x, y))
# Show the number of flights completed for each shuttle.
for index, shuttle in enumerate(shuttles):
print("Shuttle %d has completed %d flights." % (index, shuttle.flights_completed))
print("\n")
# Show the distance from the first shuttle to all other shuttles.
first_shuttle = shuttles[0]
for index, shuttle in enumerate(shuttles):
distance = first_shuttle.get_distance(shuttle)
print("The first shuttle is %f units away from shuttle %d." % (distance, index))
print("\n")
# Show the distance from the first shuttle to all other rockets.
for index, rocket in enumerate(rockets):
distance = first_shuttle.get_distance(rocket)
print("The first shuttle is %f units away from rocket %d." % (distance, index))
继承是面向对象程序设计的一个强大特性。结合目前所学,你已经可以在应用中写出稳定和可重用的代码了。
动手试一试
Refining Shuttle向 Shuttle 类中添加更多的属性。如最大飞行次数,支持的太空行走步数,与国际空间站对接的能力等。
添加更多的方法。可以是诸如 dock_ISS() 这样的方法简单打印一条语句:"Docking with the ISS" 。
创建一个对象,并执行这些方法。
# Ex : Refining Shuttle
# put your code here