class Cat:
color = 'black' # 属性
def __init__(self, name): # 构造方法
self.name = name
def eat(self, food): # 自定义方法
self.food = food
print(self.name + '正在吃' + self.food)
__cid = '小黑' # 私有对象
def __run(self, speed): # 私有方法 外部不能访问私有属性和调用私有方法
print('__cid是' + self.__cid + '的猫,以' + speed + '的速度在奔跑')
def run(self, speed): # 通过公有方法,访问私有属性和调用私有方法
self.__run(speed)
c = Cat('小短腿') # 创建对象
print(c.name)
print(c.color)
c.eat('猫粮') # 先调用方法才有food
print(c.food)
c.run('50迈')
print('---------------------------------------------------------------------')
class GarfieldCat(Cat):
def __init__(self, name):
self.name = name
# def run(self, speed):
# print(self.name + '以' + speed + '的速度在奔跑')
Ga = GarfieldCat('加菲猫')
Ga.run('100迈')
输出结果: