鱼C论坛

 找回密码
 立即注册
查看: 1244|回复: 6

关于python的类与对象。

[复制链接]
发表于 2020-4-1 10:21:34 | 显示全部楼层 |阅读模式

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

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

x
请依范例计算直径的3公分, 口径1.2 公分铜钱的面积, 范例如下:

class Circle:

    def __init__(self,radius):

        self.__radius=radius

    def getRadius(self):

        return self.__radius

    def setRadius(self,r):

        self.__radius=r

    def area(self):

        return 3.14*self.__radius**2

    def cir(self):

        return 2*3.14*self.__radius

    def __str__(self):

        return '半径是:'+str(self.__radius)

   

class Rectangle:

    def __init__(self,length,width):

        self.__length=length

        self.__width=width

    def getLength(self):

        return self.__length

    def getWidth(self):

        return self.__width

    def setLength(self,length):

        self.__length=length

    def setWidth(self,width):

        self.__width=width

    def area(self):

        return self.__length*self.__width

    def cir(self):

        return 2*(self.__length+self.__width)

    def __str__(self):

        return ' 长是:'+str(self.__length) +\

            ' 宽是:'+str(self.__width)   

   

class Ball(Circle):  

    def __init__(self,radius):

        Circle.__init__(self, radius)

    def area(self):

        return 4*3.14*self.getRadius()**2

    def volume(self):

        return  4/3*3.14*self.getRadius()**3

    def __str__(self):

        return Circle.__str__(self)

class Cuboid(Rectangle):

    def __init__(self,length,width,height):

        super().__init__(length,width)

        self.__height=height

    def getHeight(self):

        return self.__height

    def setHeight(self,height):

        self.__height=height

    def area(self):

        return 2*(self.getWidth()*self.getLength()+\

                  self.getLength()*self.__height\

                  +self.getWidth()*self.__height)

    def volume(self):

        return self.getWidth()*self.getLength()*self.__height

    def __str__(self):

        return super().__str__()+' 高是:'+str(self.__height)

   

class Copperplate(Circle,Rectangle):

    def __init__(self,radius,length,width):

        super().__init__(radius)

        Rectangle.__init__(self, length, width)

    def area(self):

        return Circle.area(self)-Rectangle.area(self)

    def __str__(self):

        return Circle.__str__(self)+Rectangle.__str__(self)

   

c=Circle(5)

print("半径为{0}的圆的面积是{1},周长是{2:.2f}".\

          format(c.getRadius(),c.area(),c.cir()))

c.setRadius(9)

print("半径为{0}的圆的面积是{1},周长是{2:.2f}".\

          format(c.getRadius(),c.area(),c.cir()))

r=Rectangle(4,5)

print("长为{0},宽为{1}的矩形的面积是{2},周长是{3}".\

          format(r.getLength(),r.getWidth(),r.area(),r.cir()))

r.setLength(7)

r.setWidth(8)

print("长为{0},宽为{1}的矩形的面积是{2},周长是{3}".\

        format(r.getLength(),r.getWidth(),r.area(),r.cir()))

b=Ball(5)

print("半径为{0}的球的表面积面积是{1:.2f},体积是{2:.2f}".\

        format(b.getRadius(),b.area(),b.volume()))

b.setRadius(9)

print("半径为{0}的球的表面积面积是{1:.2f},体积是{2:.2f}".\

        format(b.getRadius(),b.area(),b.volume()))

y=Cuboid(3,4,5)

print("长为{0},宽为{1},高为{2}的长方体的表面积是{3},体积是{4}".\

        format(y.getLength(),y.getWidth(),y.getHeight(),\

                  y.area(),y.volume()))

y.setLength(7)

y.setWidth(8)

y.setHeight(9)

print("长为{0},宽为{1},高为{2}的长方体的表面积是{3},体积是{4}".\

        format(y.getLength(),y.getWidth(),y.getHeight(),\

                  y.area(),y.volume()))

p=Copperplate(9,2,3)

print("半径为{0},长为{1},宽为{2}的铜钱的面积是{3:.2f}".\

        format(p.getRadius(),p.getLength(),p.getWidth(),p.area()))

p.setRadius(7)

p.setLength(3)

p.setWidth(4)

print("半径为{0},长为{1},宽为{2}的铜钱的面积是{3:.2f}".\

        format(p.getRadius(),p.getLength(),p.getWidth(),p.area()))

print(c)

print(r)

print(b)

print(y)

print(p)

显示语法有错,怎么改能让它运行出来呢?

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-1 10:27:21 | 显示全部楼层
  1. class Circle:

  2.     def __init__(self,radius):

  3.         self.__radius=radius

  4.     def getRadius(self):

  5.         return self.__radius

  6.     def setRadius(self,r):

  7.         self.__radius=r

  8.     def area(self):

  9.         return 3.14*self.__radius**2

  10.     def cir(self):

  11.         return 2*3.14*self.__radius

  12.     def __str__(self):

  13.         return '半径是:'+str(self.__radius)

  14.    

  15. class Rectangle:

  16.     def __init__(self,length,width):

  17.         self.__length=length

  18.         self.__width=width

  19.     def getLength(self):

  20.         return self.__length

  21.     def getWidth(self):

  22.         return self.__width

  23.     def setLength(self,length):

  24.         self.__length=length

  25.     def setWidth(self,width):

  26.         self.__width=width

  27.     def area(self):

  28.         return self.__length*self.__width

  29.     def cir(self):

  30.         return 2*(self.__length+self.__width)

  31.     def __str__(self):

  32.         return ' 长是:'+str(self.__length) +' 宽是:'+str(self.__width)   

  33.    

  34. class Ball(Circle):  

  35.     def __init__(self,radius):

  36.         Circle.__init__(self, radius)

  37.     def area(self):

  38.         return 4*3.14*self.getRadius()**2

  39.     def volume(self):

  40.         return  4/3*3.14*self.getRadius()**3

  41.     def __str__(self):

  42.         return Circle.__str__(self)

  43. class Cuboid(Rectangle):

  44.     def __init__(self,length,width,height):

  45.         super().__init__(length,width)

  46.         self.__height=height

  47.     def getHeight(self):

  48.         return self.__height

  49.     def setHeight(self,height):

  50.         self.__height=height

  51.     def area(self):

  52.         return 2*(self.getWidth()*self.getLength()+\

  53.                   self.getLength()*self.__height\

  54.                   +self.getWidth()*self.__height)

  55.     def volume(self):

  56.         return self.getWidth()*self.getLength()*self.__height

  57.     def __str__(self):

  58.         return super().__str__()+' 高是:'+str(self.__height)

  59.    

  60. class Copperplate(Circle,Rectangle):

  61.     def __init__(self,radius,length,width):

  62.         super().__init__(radius)

  63.         Rectangle.__init__(self, length, width)

  64.     def area(self):

  65.         return Circle.area(self)-Rectangle.area(self)

  66.     def __str__(self):

  67.         return Circle.__str__(self)+Rectangle.__str__(self)

  68.    

  69. c=Circle(5)

  70. print("半径为{0}的圆的面积是{1},周长是{2:.2f}".\

  71.           format(c.getRadius(),c.area(),c.cir()))

  72. c.setRadius(9)

  73. print("半径为{0}的圆的面积是{1},周长是{2:.2f}".\

  74.           format(c.getRadius(),c.area(),c.cir()))

  75. r=Rectangle(4,5)

  76. print("长为{0},宽为{1}的矩形的面积是{2},周长是{3}".\

  77.           format(r.getLength(),r.getWidth(),r.area(),r.cir()))

  78. r.setLength(7)

  79. r.setWidth(8)

  80. print("长为{0},宽为{1}的矩形的面积是{2},周长是{3}".\

  81.         format(r.getLength(),r.getWidth(),r.area(),r.cir()))

  82. b=Ball(5)

  83. print("半径为{0}的球的表面积面积是{1:.2f},体积是{2:.2f}".\

  84.         format(b.getRadius(),b.area(),b.volume()))

  85. b.setRadius(9)

  86. print("半径为{0}的球的表面积面积是{1:.2f},体积是{2:.2f}".\

  87.         format(b.getRadius(),b.area(),b.volume()))

  88. y=Cuboid(3,4,5)

  89. print("长为{0},宽为{1},高为{2}的长方体的表面积是{3},体积是{4}".\

  90.         format(y.getLength(),y.getWidth(),y.getHeight(),\

  91.                   y.area(),y.volume()))

  92. y.setLength(7)

  93. y.setWidth(8)

  94. y.setHeight(9)

  95. print("长为{0},宽为{1},高为{2}的长方体的表面积是{3},体积是{4}".\

  96.         format(y.getLength(),y.getWidth(),y.getHeight(),\

  97.                   y.area(),y.volume()))

  98. p=Copperplate(9,2,3)

  99. print("半径为{0},长为{1},宽为{2}的铜钱的面积是{3:.2f}".\

  100.         format(p.getRadius(),p.getLength(),p.getWidth(),p.area()))

  101. p.setRadius(7)

  102. p.setLength(3)

  103. p.setWidth(4)

  104. print("半径为{0},长为{1},宽为{2}的铜钱的面积是{3:.2f}".\

  105.         format(p.getRadius(),p.getLength(),p.getWidth(),p.area()))

  106. print(c)

  107. print(r)

  108. print(b)

  109. print(y)

  110. print(p)
复制代码
改好了,你这个空行让人头疼
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-1 10:44:34 | 显示全部楼层
你这代码,为什么每一行之间都要有一个空行呢?
现在发现的问题就是 class Rectangle 中 def __str__(self) 的return那一行有问题,提示有非法字符,不知道是什么,删掉中间的空行就没问题了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-1 10:45:44 | 显示全部楼层
程序代码应该这样发
在发帖界面,上方工具栏,有这样的一个符号"<>"是发代码用的
  1. class Circle:
  2.     def __init__(self,radius):
  3.         self.__radius=radius
  4.     def getRadius(self):
  5.         return self.__radius
  6.     def setRadius(self,r):
  7.         self.__radius=r
  8.     def area(self):
  9.         return 3.14*self.__radius**2
  10.     def cir(self):
  11.         return 2*3.14*self.__radius
  12.     def __str__(self):
  13.         return '半径是:'+str(self.__radius)

  14. class Rectangle:
  15.     def __init__(self,length,width):
  16.         self.__length=length
  17.         self.__width=width
  18.     def getLength(self):
  19.         return self.__length
  20.     def getWidth(self):
  21.         return self.__width
  22.     def setLength(self,length):
  23.         self.__length=length
  24.     def setWidth(self,width):
  25.         self.__width=width
  26.     def area(self):
  27.         return self.__length*self.__width
  28.     def cir(self):
  29.         return 2*(self.__length+self.__width)
  30.     def __str__(self):
  31.         return ' 长是:'+str(self.__length) +\
  32.         ' 宽是:'+str(self.__width)   

  33. class Ball(Circle):  
  34.     def __init__(self,radius):
  35.         Circle.__init__(self, radius)
  36.     def area(self):
  37.         return 4*3.14*self.getRadius()**2
  38.     def volume(self):
  39.         return  4/3*3.14*self.getRadius()**3
  40.     def __str__(self):
  41.         return Circle.__str__(self)

  42. class Cuboid(Rectangle):
  43.     def __init__(self,length,width,height):
  44.         super().__init__(length,width)
  45.         self.__height=height
  46.     def getHeight(self):
  47.         return self.__height
  48.     def setHeight(self,height):
  49.         self.__height=height
  50.     def area(self):
  51.         return 2*(self.getWidth()*self.getLength()+\
  52.         self.getLength()*self.__height+self.getWidth()*self.__height)
  53.     def volume(self):
  54.         return self.getWidth()*self.getLength()*self.__height
  55.     def __str__(self):
  56.         return super().__str__()+' 高是:'+str(self.__height)

  57. class Copperplate(Circle,Rectangle):
  58.     def __init__(self,radius,length,width):
  59.         super().__init__(radius)
  60.         Rectangle.__init__(self, length, width)
  61.     def area(self):
  62.         return Circle.area(self)-Rectangle.area(self)
  63.     def __str__(self):
  64.         return Circle.__str__(self)+Rectangle.__str__(self)

  65. c=Circle(5)
  66. print("半径为{0}的圆的面积是{1},周长是{2:.2f}".\
  67. format(c.getRadius(),c.area(),c.cir()))
  68. c.setRadius(9)
  69. print("半径为{0}的圆的面积是{1},周长是{2:.2f}".\
  70. format(c.getRadius(),c.area(),c.cir()))
  71. r=Rectangle(4,5)
  72. print("长为{0},宽为{1}的矩形的面积是{2},周长是{3}".\
  73. format(r.getLength(),r.getWidth(),r.area(),r.cir()))
  74. r.setLength(7)
  75. r.setWidth(8)
  76. print("长为{0},宽为{1}的矩形的面积是{2},周长是{3}".\
  77. format(r.getLength(),r.getWidth(),r.area(),r.cir()))
  78. b=Ball(5)
  79. print("半径为{0}的球的表面积面积是{1:.2f},体积是{2:.2f}".\
  80. format(b.getRadius(),b.area(),b.volume()))
  81. b.setRadius(9)
  82. print("半径为{0}的球的表面积面积是{1:.2f},体积是{2:.2f}".\
  83. format(b.getRadius(),b.area(),b.volume()))
  84. y=Cuboid(3,4,5)
  85. print("长为{0},宽为{1},高为{2}的长方体的表面积是{3},体积是{4}".\
  86. format(y.getLength(),y.getWidth(),y.getHeight(),y.area(),y.volume()))
  87. y.setLength(7)
  88. y.setWidth(8)
  89. y.setHeight(9)
  90. print("长为{0},宽为{1},高为{2}的长方体的表面积是{3},体积是{4}".\
  91. format(y.getLength(),y.getWidth(),y.getHeight(),y.area(),y.volume()))
  92. p=Copperplate(9,2,3)
  93. print("半径为{0},长为{1},宽为{2}的铜钱的面积是{3:.2f}".\
  94. format(p.getRadius(),p.getLength(),p.getWidth(),p.area()))
  95. p.setRadius(7)
  96. p.setLength(3)
  97. p.setWidth(4)
  98. print("半径为{0},长为{1},宽为{2}的铜钱的面积是{3:.2f}".\
  99. format(p.getRadius(),p.getLength(),p.getWidth(),p.area()))
  100. print(c)
  101. print(r)
  102. print(b)
  103. print(y)
  104. print(p)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-4-3 16:24:09 | 显示全部楼层
那这个代码如何更改,能运行出直径三公分,口径1.2公分的铜钱面积呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-3 16:48:04 From FishC Mobile | 显示全部楼层
永恒的蓝色梦想 发表于 2020-4-1 10:27
改好了,你这个空行让人头疼

看起来也很头疼
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-10 11:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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