#   ---------------------  类的三大特性的综合运用 案例  -------------------------
# 定义三个类:小狗,小猫,人
# 小狗:姓名,年龄(默认1岁) 吃饭,玩,睡觉,看家(格式:名字是xx,年龄xx岁的小狗在xx)
# 小猫:姓名,年龄(默认1岁) 吃饭,玩,睡觉,捉老鼠(格式:名字是xx,年龄xx岁的小猫在xx)
# 人:姓名,年龄(默认1岁),宠物 吃饭,玩,睡觉,(格式:名字是xx,年龄xx岁的人在xx)
# 养宠物(让所以的宠物吃饭,玩,睡觉)
# 工作(让所有的宠物做自己工作职能的工作) class Person():
def __init__(self, name, pets, age=1):
self.name = name
self.pets = pets
self.age = age def eat(self):
print("%s 在吃饭" % self) def play(self):
print("%s 在玩" % self) def sleep(self):
print("%s 在睡觉" % self) def yangPets(self):
print("%s 养宠物" % self)
for pet in self.pets:
pet.eat()
pet.play()
pet.sleep() def make_pets_work(self):
# for pet in self.pets:
# if isinstance(pet, Dog):
# pet.wacth()
# elif isinstance(pet, Cat):
# pet.cacth() # 上面的如果在加入其他的对象可能就需要重新写判断条件,可以把宠物的智能都写成相同的一个函数
for pet in self.pets:
pet.work() def __str__(self):
return "名字为{},年龄为{}的人".format(self.name, self.age) class Dog():
def __init__(self, name, age = 1):
self.name = name
self.age = age def eat(self):
print("%s 在吃饭" % self) def play(self):
print("%s 在玩" % self) def sleep(self):
print("%s 在睡觉" % self) # 改造
# def wacth(self):
def work(self):
print("%s 看家" % self) def __str__(self):
return "名字为{},年龄为{}的小狗".format(self.name, self.age) class Cat():
def __init__(self, name, age=1):
self.name = name
self.age = age def eat(self):
print("%s 在吃饭" % self) def play(self):
print("%s 在玩" % self) def sleep(self):
print("%s 在睡觉" % self) # 改造
# def cacth(self):
def work(self):
print("%s 捉老鼠" % self) def __str__(self):
return "名字为{},年龄为{}的小猫".format(self.name, self.age) d = Dog("小黑", 18)
c = Cat("小花", 20)
print(d) # 名字为小黑,年龄为18的小狗
d.eat() # 名字为小黑,年龄为18的小狗 在吃饭
d.play() # 名字为小黑,年龄为18的小狗 在玩
print("-" * 30) p = Person("小明", [d, c], 30)
p.yangPets()
print("-" * 30)
p.make_pets_work() print("=" * 30) # -------------------- 改造上面的案例 ---------------------
class Animal():
def __init__(self, name, age = 1):
self.name = name
self.age = age def eat(self):
print("%s 在吃饭" % self) def play(self):
print("%s 在玩" % self) def sleep(self):
print("%s 在睡觉" % self) class Person(Animal):
def __init__(self, name, pets, age=1):
super(Person, self).__init__(name, age)
self.pets = pets # 修改命名方式
def yang_pets(self):
print("%s 养宠物" % self)
for pet in self.pets:
pet.eat()
pet.play()
pet.sleep() def make_pets_work(self):
# for pet in self.pets:
# if isinstance(pet, Dog):
# pet.wacth()
# elif isinstance(pet, Cat):
# pet.cacth() # 上面的如果在加入其他的对象可能就需要重新写判断条件,可以把宠物的智能都写成相同的一个函数
for pet in self.pets:
pet.work()
#
def __str__(self):
return "名字为{},年龄为{}的人".format(self.name, self.age) class Dog(Animal):
def work(self):
print("%s 看家" % self) def __str__(self):
return "名字为{},年龄为{}的小狗".format(self.name, self.age) class Cat(Animal):
def work(self):
print("%s 捉老鼠" % self) def __str__(self):
return "名字为{},年龄为{}的小猫".format(self.name, self.age) d = Dog("小黑", 10)
c = Cat("小花", 11)
p = Person("小明", [d, c], 20)
p.yang_pets()
p.make_pets_work()
#   ---------------------  类的三大特性的综合运用 案例  -------------------------
# 定义三个类:小狗,小猫,人
# 小狗:姓名,年龄(默认1岁) 吃饭,玩,睡觉,看家(格式:名字是xx,年龄xx岁的小狗在xx)
# 小猫:姓名,年龄(默认1岁) 吃饭,玩,睡觉,捉老鼠(格式:名字是xx,年龄xx岁的小猫在xx)
# 人:姓名,年龄(默认1岁),宠物 吃饭,玩,睡觉,(格式:名字是xx,年龄xx岁的人在xx)
# 养宠物(让所以的宠物吃饭,玩,睡觉)
# 工作(让所有的宠物做自己工作职能的工作) class Person():
def __init__(self, name, pets, age=):
self.name = name
self.pets = pets
self.age = age def eat(self):
print("%s 在吃饭" % self) def play(self):
print("%s 在玩" % self) def sleep(self):
print("%s 在睡觉" % self) def yangPets(self):
print("%s 养宠物" % self)
for pet in self.pets:
pet.eat()
pet.play()
pet.sleep() def make_pets_work(self):
# for pet in self.pets:
# if isinstance(pet, Dog):
# pet.wacth()
# elif isinstance(pet, Cat):
# pet.cacth() # 上面的如果在加入其他的对象可能就需要重新写判断条件,可以把宠物的智能都写成相同的一个函数
for pet in self.pets:
pet.work() def __str__(self):
return "名字为{},年龄为{}的人".format(self.name, self.age) class Dog():
def __init__(self, name, age = ):
self.name = name
self.age = age def eat(self):
print("%s 在吃饭" % self) def play(self):
print("%s 在玩" % self) def sleep(self):
print("%s 在睡觉" % self) # 改造
# def wacth(self):
def work(self):
print("%s 看家" % self) def __str__(self):
return "名字为{},年龄为{}的小狗".format(self.name, self.age) class Cat():
def __init__(self, name, age=):
self.name = name
self.age = age def eat(self):
print("%s 在吃饭" % self) def play(self):
print("%s 在玩" % self) def sleep(self):
print("%s 在睡觉" % self) # 改造
# def cacth(self):
def work(self):
print("%s 捉老鼠" % self) def __str__(self):
return "名字为{},年龄为{}的小猫".format(self.name, self.age) d = Dog("小黑", )
c = Cat("小花", )
print(d) # 名字为小黑,年龄为18的小狗
d.eat() # 名字为小黑,年龄为18的小狗 在吃饭
d.play() # 名字为小黑,年龄为18的小狗 在玩
print("-" * ) p = Person("小明", [d, c], )
p.yangPets()
print("-" * )
p.make_pets_work() print("=" * ) # -------------------- 改造上面的案例 ---------------------
class Animal():
def __init__(self, name, age = ):
self.name = name
self.age = age def eat(self):
print("%s 在吃饭" % self) def play(self):
print("%s 在玩" % self) def sleep(self):
print("%s 在睡觉" % self) class Person(Animal):
def __init__(self, name, pets, age=):
super(Person, self).__init__(name, age)
self.pets = pets # 修改命名方式
def yang_pets(self):
print("%s 养宠物" % self)
for pet in self.pets:
pet.eat()
pet.play()
pet.sleep() def make_pets_work(self):
# for pet in self.pets:
# if isinstance(pet, Dog):
# pet.wacth()
# elif isinstance(pet, Cat):
# pet.cacth() # 上面的如果在加入其他的对象可能就需要重新写判断条件,可以把宠物的智能都写成相同的一个函数
for pet in self.pets:
pet.work()
#
def __str__(self):
return "名字为{},年龄为{}的人".format(self.name, self.age) class Dog(Animal):
def work(self):
print("%s 看家" % self) def __str__(self):
return "名字为{},年龄为{}的小狗".format(self.name, self.age) class Cat(Animal):
def work(self):
print("%s 捉老鼠" % self) def __str__(self):
return "名字为{},年龄为{}的小猫".format(self.name, self.age) d = Dog("小黑", )
c = Cat("小花", )
p = Person("小明", [d, c], )
p.yang_pets()
p.make_pets_work()

最新文章

  1. Python标准模块--threading
  2. FastJson的简单实用
  3. 如何透过HTC Vive拍摄Mixed Reality (混合现实)影片
  4. 马旭飞:共探H3 BPM社区发展战略
  5. [BZOJ 2186][Sdoi2008]沙拉公主的困惑(欧拉函数)
  6. AppSettingManager
  7. hdu How to Type
  8. 企业级搜索引擎Solr 第三章 索引数据(Indexing Data)[1]
  9. grep正则表达式后面的单引号和双引号的区别
  10. (转载)【C++】new A和new A()的区别详解
  11. xmemcached user guide --存档
  12. thinkphp action.class.php 学习
  13. response和request
  14. Android编程中的5种数据存储方式
  15. js数组元素的添加和删除
  16. 004dayPython学习输入并输出用户名和密码
  17. spring cloud: Hystrix(三):健康指数 health Indicator
  18. Maven项目打Jar包,如何添加依赖
  19. python mysql connector
  20. 众安尊享e生·新全保通2017成人精选版60岁以后续保的保费清单

热门文章

  1. location.assign 与 location.replace的区别
  2. cocos2d-X学习之主要类介绍:精灵角色(CCSprite)
  3. 【IDEA】Maven踩坑:pom文件中的默认profiles不生效+IDEA中Maven的profiles使用说明
  4. 并发测试 java.lang.OutOfMemoryError: GC overhead limit exceeded Xms Xmx 阻塞请求 单节点 请求分发 负载均衡
  5. ssh login waiting too much time
  6. Java 集合框架工具类
  7. 2015-03-10——简析javascript对象
  8. 搞懂head 和 tail 命令
  9. Python(数据库安装与基本语句)
  10. pip安装lxml报错 Fatal error in launcher: Unable to create process using '"c:\users\administrator\appdata\local\programs\python\python36\python.exe" "C:\Users\Administrator\AppData\L