鱼C论坛

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

第63章节练习出现BUG

[复制链接]
发表于 前天 19:06 | 显示全部楼层 |阅读模式

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

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

x
class Shape():
    def __init__(self,name):
        self.name=name
        #print(name)
    def area(self):
        pass

class Square(Shape):
    def __init__(self,length):
        super().__init__("正方形")
        self.length=length
    def area(self):
        return self.length*self.length

class Circle(Shape):
    def __init__(self,radius):
        super().__init__("圆形")
        self.radius=radius
    def area(self):
        return  3.14*self.radius*self.radius

class Triangle(Shape):
    def __init__(self):
        super().__init__("三角形")
    def area(self,base,height):
        self.base=base
        self.height=height
        return self.base*self.height/2


s=Square(5)
s.name()
t=Triangle()
c=Circle(6)
s.area()
t.area(3,4)
c.area()

代码运行提示:
Traceback (most recent call last):
  File "C:\python学习\text.py", line 40, in <module>
    s.name()
TypeError: 'str' object is not callable
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 前天 19:07 | 显示全部楼层
问题分析与解决方案

该代码存在两个主要问题:

1. 错误调用name属性
在最后测试代码时执行了 s.name(),但name是属性而不是方法。Shape类中的name是字符串类型属性,应该直接访问而非调用

2. 三角形类参数设计问题
Triangle类的area方法需要额外参数,这与父类Shape中定义的area()无参数形式冲突,破坏了多态性

修正后的代码:


  1. class Shape():
  2.     def __init__(self,name):
  3.         self.name = name  # name是实例属性
  4.     def area(self):
  5.         pass

  6. class Square(Shape):
  7.     def __init__(self, length):
  8.         super().__init__("正方形")
  9.         self.length = length
  10.     def area(self):
  11.         return self.length ** 2

  12. class Circle(Shape):
  13.     def __init__(self, radius):
  14.         super().__init__("圆形")
  15.         self.radius = radius
  16.     def area(self):
  17.         return 3.14 * self.radius ** 2

  18. class Triangle(Shape):
  19.     def __init__(self, base, height):
  20.         super().__init__("三角形")
  21.         self.base = base   # 应该在初始化时接收参数
  22.         self.height = height
  23.     def area(self):
  24.         return self.base * self.height / 2  # 保持与父类参数一致

  25. # 正确调用方式
  26. s = Square(5)
  27. print(s.name)  # 直接访问属性,去掉括号

  28. t = Triangle(3, 4)  # 初始化时传入参数
  29. c = Circle(6)

  30. print(s.area())  # 25
  31. print(t.area())  # 6.0
  32. print(c.area())  # 113.04
复制代码


主要修改点:
1. 将Triangle的参数接收改到__init__方法中
2. 使所有子类的area()保持无参数形式
3. 修正name属性的访问方式(去掉调用括号)
4. 添加了实际输出结果的print语句

建议再仔细检查类的方法参数设计,保持继承体系中方法签名的统一性。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 前天 19:10 From FishC Mobile | 显示全部楼层
name是属性,不是方法,不需要加括号,交互模式直接s.name就行,或者print(s.name)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 前天 19:23 From FishC Mobile | 显示全部楼层
有没有一种可能,s.name 是一个字符串,像"abc"一样,而"abc"()会报错
去掉括号
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-20 13:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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