Python面向对象编程与决策树算法详解
1. 面向对象编程概念
1.1 继承(Inheritance)
继承是指一个对象能够从另一个对象派生而来。以跑车为例,所有跑车都是车辆,但并非所有车辆都是跑车。同样,所有轿车都是车辆,但并非所有车辆都是轿车,且轿车肯定不是跑车,尽管它们都属于车辆类别。
在Python中,通过派生类来实现继承。以下是一个示例代码:
class Vehicle(object):
def __init__(self, makeAndModel, prodYear, airConditioning):
self.makeAndModel = makeAndModel
self.prodYear = prodYear
self.airConditioning = airConditioning
self.doors = 4
def honk(self):
print "%s says: Honk! Honk!" % self.makeAndModel
class SportsCar(Vehicle):
def __init__(self, makeAndModel, prodYear, airConditioning):
self.makeAndModel = makeAndModel
self.prodYear = prodYear
self.airConditioning = airConditioning
超级会员免费看
订阅专栏 解锁全文
895

被折叠的 条评论
为什么被折叠?



