#   ---------------------  类的三大特性的综合运用 案例  -------------------------
# 定义三个类:小狗,小猫,人
# 小狗:姓名,年龄(默认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()

Python 类的三大特性的综合运用 案例的更多相关文章

  1. 第9章 Java类的三大特性之一:继承

    1.什么是继承 子类继承父类就是对父类的扩展,继承时会自动拥有父类所拥有的处private之外的所有成员作用:增加代码复用语法格式: class 子类名 extends 父类名{…………}第9章 Ja ...

  2. (转载)OC学习篇之---类的三大特性:封装,继承,多态

    之前的一片文章介绍了OC中类的初始化方法和点语法的使用,今天来继续学习OC中的类的三大特性,我们在学习Java的时候都知道,类有三大特性:继承,封装,多态,这个也是介绍类的时候,必须提到的话题,那么今 ...

  3. OC基础 类的三大特性

    OC基础  类的三大特性 OC的类和JAVA一样,都有三大特性:继承,封装,多态,那么我们就来看一下OC中类的三大特性. 1.继承 继承的特点: (1)子类从父类继承了属性和方法. (2)子类独有的属 ...

  4. 转 OC温故:类的三大特性(封装,继承,多态)

    原文标题:OC学习篇之---类的三大特性(封装,继承,多态) 我们都知道,面向对象程序设计中的类有三大特性:继承,封装,多态,这个也是介绍类的时候,必须提到的话题,那么今天就来看一下OC中类的三大特性 ...

  5. OC学习篇之---类的三大特性(封装,继承,多态)

    之前的一片文章介绍了OC中类的初始化方法和点语法的使用:http://blog.csdn.net/jiangwei0910410003/article/details/41683873,今天来继续学习 ...

  6. Python:Day24 类、类的三大特性

    Python有两种编程方式:函数式+面向对象 函数式编程可以做所有的事情,但是否合适? 面向对象: 一.定义 函数: def + 函数名(参数) 面象对象: class  bar--->  名字 ...

  7. Python 面向对象的三大特性:封装,继承,多态

    # 面向对象的三大特性:封装,继承,多态 # 继承的影响:资源的继承,资源的使用,资源的覆盖,资源的累加 # 资源的继承,在Python中的继承是指能使用父类的资源,而不是说在子类也复制一份父类代码到 ...

  8. Python - 面向对象编程 - 三大特性之继承

    继承 继承也是面向对象编程三大特性之一 继承是类与类的一种关系 定义一个新的 class 时,可以从某个现有的 class 继承 新的 class 类就叫子类(Subclass) 被继承的类一般称为父 ...

  9. Python 面向对象的三大特性

    面向对象的三大特性:继承,封装,多态 什么时候用封装: 同一种功能的时候, 譬如:把一部分数据或方法,封装到同一个类的中 PS:在构造方法中,原始数据中....

随机推荐

  1. 怎么查看mac系统是32位还是64位的操作系统

    如何查看mac系统是32位还是64位的操作系统 (一)点击工具栏左上角点击 (苹果Logo)标志,关于本机  -->  更多信息 --> 系统报告  -->(左侧栏中)软件 (二) ...

  2. 160708、JQuery解析XML数据的demo

    用JavaScript解析XML数据是常见的编程任务,JavaScript能做的,JQuery当然也能做.下面我们来总结几个使用JQuery解析XML的例子. 方案1 当后台返回的数据类型是xml对象 ...

  3. 在Scrapy中使用IP池或用户代理更新版(python3)

    middlewares.py # -*- coding: utf-8 -*- # 导入随机模块 import random # 导入有关IP池有关的模块 from scrapy.downloaderm ...

  4. 使用CocoaPods配置管理开源项目

    今天从GitHub下载了MMProcessHUB,想先看看demo,但是不巧的是作者是用CocoaPods配置的,需要安装CocoaPods,CocoaPods是一个第三方的类库管理工具.找了一篇很详 ...

  5. protoc-gen-go: error:bad Go source code was generated: 163:6: illegal UTF-8 encoding (and 2915 more errors)

    protoc-gen-go: error:bad Go source code was generated: 163:6: illegal UTF-8 encoding (and 2915 more ...

  6. When an HTTP server receives a request for a CGI script

    cgicc: Overview of the Common Gateway Interface https://www.gnu.org/software/cgicc/doc/cgi_overview. ...

  7. Spring Boot Dubbo Dubbok spring cloud

    比较spring cloud和dubbo,各自的优缺点是什么 - 趁年轻再疯狂一次吧 - CSDN博客 https://blog.csdn.net/u010664947/article/details ...

  8. Casperjs中fill提交表单遇到的问题

    1.if you access internet with proxy please add             --ignore-ssl-errors=true --ssl-protocol=a ...

  9. 国内java,oa,weixin opensource framework www.jeecg.org

    Soap/rest 为API生,为框架死,为Debug奋斗一辈子!吃符号的亏,上大小写的当,最后死在需求上!

  10. Windows数据库定时备份

    首先打开:任务计划程序 右键任务计划程序库,选择创建基本任务 然后即可以按照实际情况逐步进行 直到启动程序--浏览(程序或脚本)时,这里本人导入的是backup.bat文件,文件内容为 @echo 设 ...