# 定义三个类, 小狗, 小猫, 人
# 小狗: 姓名, 年龄(默认1岁); 吃饭, 玩, 睡觉, 看家(格式: 名字是xx, 年龄xx岁的小狗在xx)
# 小猫: 姓名, 年龄(默认1岁); 吃饭, 玩, 睡觉, 捉老鼠(格式: 名字是xx, 年龄xx岁的小猫在xx)
# 人: 姓名, 年龄(默认1岁), 宠物; 吃饭, 玩, 睡觉(格式: 名字是xx, 年龄xx岁的小猫在xx)
# 养宠物(让所有的宠物吃饭, 玩, 睡觉),
# 让宠物工作(让所有的宠物根据自己的职责开始工作)
# 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 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 work(self):
# print("%s在捉老鼠" % self)
#
# def __str__(self):
# return "名字是{},年龄是{}岁的小猫在".format(self.name, self.age)
#
# class Person():
# # 在创建小狗实例的时候,给他设置几个属性
# def __init__(self, name, pets, age=1):
# self.name = name
# self.age = age
# self.pets = pets
# def eat(self):
# print("%s在吃饭" % self)
#
# def play(self):
# print("%s在玩" % self)
#
# def sleep(self):
# print("%s在睡觉" % self)
#
# def yangPets(self):
# for pet in self.pets:
# pet.eat()
# pet.play()
# pet.sleep()
#
# def make_pets_work(self):
# for pet in self.pets:
# pet.work()
#
# def __str__(self):
# return "名字是{},年龄是{}岁的人在".format(self.name, self.age)
#
# d = Dog("小黑", 11)
# c = Cat("小红果", 2)
# # print(d.__dict__)
#
# p = Person("sz",[d,c],18)
# p.yangPets()
# p.make_pets_work()
#----------------------------优化代码------------------------------------
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 yangPets(self):
for pet in self.pets:
pet.eat()
pet.play()
pet.sleep()
def make_pets_work(self):
for pet in self.pets:
pet.work()
def __str__(self):
return "名字是{},年龄是{}岁的人在".format(self.name, self.age)
# p = Person("sz", [1,2], 18)
# print(p.__dict__)
class Cat(Animal):
# 在创建小狗实例的时候,给他设置几个属性
def work(self):
print("%s在捉老鼠" % self)
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)
d = Dog("小黑", 11)
c = Cat("小红果", 2)
# print(d.__dict__)
p = Person("sz",[d,c],18)
p.yangPets()
p.make_pets_work()
# 作业
# # 定义三个类:学生,组长,教师
# # 学生:
# 姓名, 年龄, 学号
# 吃饭, 睡觉, 学习
# # 组长:
# 姓名, 年龄, 学号, 职务
# 吃饭, 睡觉, 学习, 管理
# # 教师
# 姓名, 年龄, 职务
# 吃饭, 睡觉, 教学, 管理
# class student:
# def __init__(self, name ,age ,id):
# self.name = name
# self.age = age
# self.id = id
#
# def chifan(self):
# print("%吃饭" % self)
#
# def shuijiao(self):
# print("%睡觉" % self)
#
# def xuexi(self):
# print("%睡觉" % self)
#
# s = student("dd", 18 , 135456)
# print(s.__dict__)
#
# class groupLeader:
# def __init__(self, name ,age ,id, duties):
# self.name = name
# self.age = age
# self.id = id
# self.duties = duties
#
# def chifan(self):
# print("%吃饭" % self)
#
# def shuijiao(self):
# print("%睡觉" % self)
#
# def jiaoxue(self):
# print("%教书" % self)
#
# def zhiwu(self):
# print("%管理" % self)
#
#
# g = groupLeader("dd", 19 , 15616 ,"组长")
# print(g.__dict__)
#
#
# class teacher:
# def __init__(self, name ,age , duties):
# self.name = name
# self.age = age
# self.duties = duties
#
# def chifan(self):
# print("%吃饭" % self)
#
# def shuijiao(self):
# print("%睡觉" % self)
#
# def jiaoxue(self):
# print("%睡觉" % self)
#
# def zhiwu(self):
# print("%管理" % self)
#
# t = teacher("dd", 22,"语文老师")
# print(t.__dict__)
# --------------------------优化-----------------------
class xuexiao:
def __init__(self, name, age):
self.name = name
self.age = age
def chifan(self):
print("%s吃饭" % self)
def shuijiao(self):
print("%s睡觉" % self)
class student(xuexiao):
def __init__(self, name, age, id):
super().__init__(name, age)
self.id = id
def xuexi(self):
print("%s学习" % self)
def __str__(self):
return "姓名是{},年龄是{},学号是{}的职务是学生在".format(self.name,self.age,self.id)
s = student("dd", 18, 15613)
# print(s.__dict__)
s.chifan()
class groupLeader(xuexiao):
def __init__(self, name, age, id, duties):
super().__init__(name, age)
self.id = id
self.duties = duties
def xuexi(self):
print("%s学习" % self)
def guanli(self):
print("%s管理" % self)
def __str__(self):
return "姓名是{},年龄是{},学号是{}职责是{}的组长在".format(self.name,self.age,self.id, self.duties)
g = groupLeader("dd", 18, 3333,"收作业")
# print(g.__dict__)
g.chifan()
class teacher(xuexiao):
def __init__(self, name, age, duties):
super().__init__(name, age)
self.duties = duties
def jiaoxue(self):
print("%s教学" % self)
def guanli(self):
print("%s管理" % self)
def __str__(self):
return "姓名是{},年龄是{},职责是{}的老师在".format(self.name,self.age, self.duties)
t = teacher("dd", 18, "收作业")
t.chifan()