鱼C论坛

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

函数第六章运行出现异常

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

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

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

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

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

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

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


s=Square(5)
c=Circle(6)
t=Triangle(3,4)
s.name
'正方形'
s.length
5
s.area
<bound method Square.area of <__main__.Square object at 0x000002197E99EA50>>
   
s.length
5
s.area
<bound method Square.area of <__main__.Square object at 0x000002197E99EA50>>
c.area
<bound method Circle.area of <__main__.Circle object at 0x000002197E99EBA0>>
t.area
<bound method Triangle.area of <__main__.Triangle object at 0x000002197E99ECF0>>
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 5 天前 | 显示全部楼层
问题分析:

代码存在三个主要问题:

1. Circle类属性拼写错误
构造方法参数是 radiuo(错误拼写),正确应为 radius

2. Triangle类属性拼写错误
初始化时写成了 self.bade = base,正确应为 self.base = base

3. 方法调用缺少括号
所有 area 方法调用时没有加 ()



修正后的代码:


  1. class Shape:
  2.     def __init__(self, name):
  3.         self.name = name
  4.         
  5.     def area(self):  # 修正缩进位置
  6.         pass

  7. class Circle(Shape):
  8.     def __init__(self, radius):  # 修正参数名
  9.         super().__init__("圆形")
  10.         self.radius = radius  # 修正属性名
  11.         
  12.     def area(self):
  13.         return 3.14 * self.radius * self.radius

  14. class Triangle(Shape):
  15.     def __init__(self, base, height):
  16.         super().__init__("三角形")
  17.         self.base = base  # 修正属性名
  18.         self.height = height
  19.         
  20.     def area(self):
  21.         return self.base * self.height / 2

  22. class Square(Shape):
  23.     def __init__(self, length):
  24.         super().__init__("正方形")
  25.         self.length = length
  26.         
  27.     def area(self):
  28.         return self.length * self.length

  29. # 正确调用方式
  30. s = Square(5)
  31. c = Circle(6)
  32. t = Triangle(3,4)

  33. print(s.area())  # 输出25
  34. print(c.area())  # 输出113.04
  35. print(t.area())  # 输出6.0
复制代码


关键修改说明:
  • Shape类的area方法缩进修正到类层级
  • 修正Circle类属性名拼写错误:radiuo → radius
  • 修正Triangle类属性名拼写错误:bade → base
  • 调用方法时添加 () 执行方法


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

使用道具 举报

发表于 5 天前 | 显示全部楼层
本帖最后由 jackz007 于 2025-4-17 18:28 编辑
  1. class Shape:
  2.     def __init__(self,name):
  3.         self.name=name
  4.     def area(self):                           # 缩进错误
  5.         pass

  6. class Circle(Shape):
  7.     def __init__(self, radius):
  8.         super().__init__("圆形")
  9.         self . radius = radius
  10.     def area(self):
  11.         return 3.14*self.radius*self.radius

  12.    
  13. class Triangle(Shape):
  14.     def __init__(self, base,height):
  15.         super().__init__("三角形")
  16.         self.base=base                        # 属性 base 写错了
  17.         self.height=height
  18.     def area(self):
  19.         return self.base*self.height/2

  20. class Square(Shape):
  21.     def __init__(self,length):
  22.         super().__init__("正方形")
  23.         self.length=length
  24.     def area(self):
  25.         return self.length*self.length

  26. s=Square(5)
  27. c=Circle(6)
  28. t=Triangle(3,4)
  29. print(s . name)
  30. print(s . length)
  31. print(s . area())                              # area 是方法,不是属性
  32. print(c . name)
  33. print(c . radius)
  34. print(c . area())                              # area 是方法,不是属性
  35. print(t . name)
  36. print(t . base)
  37. print(t . height)
  38. print(t . area())                              # area 是方法,不是属性
复制代码


        添加 __repr__() 方法可以直接打印对象获取信息
  1. class Shape:
  2.     def __init__(self,name):
  3.         self.name=name
  4.     def area(self):
  5.         pass

  6. class Circle(Shape):
  7.     def __init__(self, radius):
  8.         super().__init__("圆形")
  9.         self . radius = radius
  10.     def area(self):
  11.         return 3.14*self.radius*self.radius
  12.     def __repr__(self):
  13.         return f'shape : {self . name} , radius : {self . radius} , area : {3.14 * self . radius * self . radius}'
  14.    
  15. class Triangle(Shape):
  16.     def __init__(self, base,height):
  17.         super().__init__("三角形")
  18.         self.base=base
  19.         self.height=height
  20.     def area(self):
  21.         return self.base*self.height/2
  22.     def __repr__(self):
  23.         return f'shape : {self . name} , base : {self . base} , height : {self . height} , area : {self . base * self . height / 2}'

  24. class Square(Shape):
  25.     def __init__(self,length):
  26.         super().__init__("正方形")
  27.         self.length=length
  28.     def area(self):
  29.         return self.length*self.length
  30.     def __repr__(self):
  31.         return f'shape : {self . name} , length : {self . length} , area : {self . length * self . length}'

  32. s=Square(5)
  33. c=Circle(6)
  34. t=Triangle(3,4)
  35. print(s)
  36. print(c)
  37. print(t)
复制代码


        运行实况:
  1. D:\[exercise]\Python>python x.py
  2. shape : 正方形 , length : 5 , area : 25
  3. shape : 圆形 , radius : 6 , area : 113.03999999999999
  4. shape : 三角形 , base : 3 , height : 4 , area : 6.0

  5. D:\[exercise]\Python>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 5 天前 | 显示全部楼层

  1. class Shape:
  2.     def __init__(self,name):
  3.         self.name=name
  4.     def area(self): #缩进
  5.         pass

  6. class Circle(Shape):
  7.     def __init__(self, radius):#不算'错误'的错误,半径单词为radius
  8.         super().__init__("圆形")
  9.         self.radius=radius
  10.     def area(self):
  11.         return 3.14*self.radius*self.radius

  12.    
  13. class Triangle(Shape):
  14.     def __init__(self, base,height):
  15.         super().__init__("三角形")
  16.         self.base=base#bade->base
  17.         self.height=height
  18.     def area(self):
  19.         return self.base*self.height/2

  20.    
  21. class Square(Shape):
  22.     def __init__(self,length):
  23.         super().__init__("正方形")
  24.         self.length=length
  25.     def area(self):
  26.         return self.length*self.length
复制代码


后面 area 应当加上 () 表示调用.

s.area()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 06:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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