鱼C论坛

 找回密码
 立即注册
查看: 2080|回复: 3

[已解决]定义了2个类,就报错了 ?

[复制链接]
发表于 2022-8-31 09:05:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x


要求:艺术团招生,在3岁至10岁之间的孩子


  1. class Children():
  2.     def __init__(self,name,age,district):
  3.         self.name=name
  4.         self.age=age
  5.         self.district=district
  6. class Art_group():
  7.     def __init__(self):
  8.         self.child=0
  9.     def __str__(self):
  10.         return f"招生:姓名{self.name},年龄{self.age},地区{self.district}"
  11.     def add_children(self,item):
  12.         if 3<self.age<=10 :
  13.             self.child += 1
  14.             print(f"已经招生了{self.child}人!")
  15.         else :
  16.             print("不满足招生条件!")
  17. a=Children("完么智曲",6,"青海")
  18. gyl=Art_group()
  19. gyl.add_children(a)
  20. print(gyl)
复制代码
最佳答案
2022-8-31 10:03:49
  1. class Child():
  2.         def __init__(self, name, age, district):
  3.                 self.name = name
  4.                 self.age = age
  5.                 self.district = district

  6. class Artist():
  7.         def __init__(self):
  8.                 self.group = list()
  9.        
  10.         def __str__(self):
  11.                 detail = list()
  12.                 for each in self.group:
  13.                         detail.append(f"姓名:{each.name} 年龄:{each.age} 地区:{each.district}")
  14.                 detail = '\n'.join(detail)
  15.                 return detail
  16.        
  17.         def add(self, child: Child):
  18.                 if 3 >= child.age > 10:
  19.                         print("不满足招生条件!")
  20.                 else:
  21.                         self.group.append(child)
  22.                         print(f"已经招生了{len(self.group)}人!")

  23. dragov = Child("完么智曲", 6, "青海")
  24. artist = Artist()

  25. artist.add(dragov)
  26. print(artist)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-8-31 09:24:39 | 显示全部楼层
如果子类有自己的INIT  那么不能直接调用父类的属性
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-31 10:03:49 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
  1. class Child():
  2.         def __init__(self, name, age, district):
  3.                 self.name = name
  4.                 self.age = age
  5.                 self.district = district

  6. class Artist():
  7.         def __init__(self):
  8.                 self.group = list()
  9.        
  10.         def __str__(self):
  11.                 detail = list()
  12.                 for each in self.group:
  13.                         detail.append(f"姓名:{each.name} 年龄:{each.age} 地区:{each.district}")
  14.                 detail = '\n'.join(detail)
  15.                 return detail
  16.        
  17.         def add(self, child: Child):
  18.                 if 3 >= child.age > 10:
  19.                         print("不满足招生条件!")
  20.                 else:
  21.                         self.group.append(child)
  22.                         print(f"已经招生了{len(self.group)}人!")

  23. dragov = Child("完么智曲", 6, "青海")
  24. artist = Artist()

  25. artist.add(dragov)
  26. print(artist)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-31 15:03:19 | 显示全部楼层
学习一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 11:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表