Python支持面向对象,但是跟C++有些不一样。
Python没有明显的构造函数和析构函数,但是它的方法__init__ 类似于C++的构造函数,不同在于__init__ 必须要第一形参是self,感觉和C++的this比较接近,其它参数,例如 Dog的name, age在__init__被初始化。
Python的方法如果需要访问属性变量的话,一定要通过self.<变量名>。
下面这个例子 dog.py 用于模拟小狗的简单尝试:
class Dog(object):
""" a dog """
def __init__(self, name, age):
self.name = name
self.age = age
def sit(self):
print(self.name.title() + " is now sitting.")
def roll_over(self):
print(self.name.title() + " rolled over!")
def show_name(self):
print("The dog's name is " + self.name.title() + ".")
def show_age(self):
print("The dog's age is " + str(self.age) + " years old.")
def main():
my_dog = Dog('willie', 6)
my_dog.show_name()
my_dog.show_age()
my_dog.sit()
my_dog.roll_over()
if __name__ == '__main__':
main()
my_dog是类Dog的一个实例,通过两个实参’willie’和6初始化,然后调用一些方法来打印一些信息。
总体感觉下来,Python创建和使用类还是相对简单的。
1)属性odometer_reading 没有在__init__的输入参数中,但是在__init__做了初始化(指定默认值)
2) 一般还是建议通过方法来修改属性的值,例如:update_odometer用来更新属性odometer_reading 的值
class Car(object):
""" a car """
def __init__(self, maker, model, year):
self.maker = maker
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
long_name = str(self.year) + ' ' + self.maker + ' ' + self.model
return long_name.title()
def read_odometer(self):
print("This car has " + str(self.odometer_reading) + " miles on it.")
def update_odometer(self, mileage):
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't rollback an odometer!")
def increment_odometer(self, miles):
self.odometer_reading += miles
def main():
my_new_car = Car('Audi', 'A4', 2016)
print(my_new_car.get_descriptive_name())
my_new_car = Car('Subaru', 'Outback', 2013)
print(my_new_car.get_descriptive_name())
my_new_car.update_odometer(23500)
my_new_car.read_odometer()
my_new_car.increment_odometer(100)
my_new_car.read_odometer()
if __name__ == '__main__':
main()
Python支持继承,原来的类称为父类,而新类称为子类。子类继承了其父类的所有属性和方法,同时子类还可以定义其自己的属性和方法。
下面来模拟电动汽车,创建一个简单的ElectricCar类,它是Car类的子类。
class Car(object):
""" a car """
def __init__(self, maker, model, year):
self.maker = maker
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
long_name = str(self.year) + ' ' + self.maker + ' ' + self.model
return long_name.title()
def read_odometer(self):
print("This car has " + str(self.odometer_reading) + " miles on it.")
def update_odometer(self, mileage):
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't rollback an odometer!")
def increment_odometer(self, miles):
self.odometer_reading += miles
class ElectricCar(Car):
""" represent aspect of a car, specific to electric vehicles """
def __init__(self, maker, model, year):
super(ElectricCar, self).__init__(maker, model, year)
self.battery_size = 70
def describe_battery(self):
print("This car has a " + str(self.battery_size) + "-kWh battery.")
def main():
my_tesla = ElectricCar('Tesla', 'Model S', 2016)
print(my_tesla.get_descriptive_name())
my_tesla.describe_battery()
if __name__ == '__main__':
main()
注意Python子类的__init__方法继承了父类Car __init__的方法,但是使用了特殊函数super(),让子类EelectricCar和父类Car关联起来。同时,子类ElectricCar有一个自己的属性battery_size和方法describe_battery()。
下面的例子就一点:子类ElectricCar的属性battery 在__init__方法里使用了一个类Battery来初始化。在子类ElectricCar里使用类Battery,可以更加清晰和详细地描述电瓶。
class Car(object):
""" a car class """
def __init__(self, maker, model, year):
self.maker = maker
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
long_name = str(self.year) + ' ' + self.maker + ' ' + self.model
return long_name.title()
def read_odometer(self):
print("This car has " + str(self.odometer_reading) + " miles on it.")
def update_odometer(self, mileage):
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't rollback an odometer!")
def increment_odometer(self, miles):
self.odometer_reading += miles
class Battery():
""" a battery class for electric car """
def __init__(self, battery_size=70):
self.battery_size = battery_size
def get_range(self):
if self.battery_size == 70:
range = 240
elif self.battery_size == 85:
range = 270
message = "This car can go approximately " + str(range)
message += " miles on a full charge."
print(message)
def update_battery(self):
if self.battery_size != 85:
self.battery_size = 85
def describe_battery(self):
print("This car has a " + str(self.battery_size) + "-kWh battery.")
class ElectricCar(Car):
""" represent aspect of a car, specific to electric vehicles """
def __init__(self, maker, model, year):
super(ElectricCar, self).__init__(maker, model, year)
self.battery = Battery()
def main():
my_tesla = ElectricCar('Tesla', 'Model S', 2016)
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
my_tesla.battery.update_battery()
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
if __name__ == '__main__':
main()
文章内容来自《Python编程 从入门到实践》 [美] Eric Matthes 袁国忠 译