Python3.x 实现类的继承
KiKi设计类继承
http://www.nowcoder.com/questionTerminal/699ba050e2704591ae3e62401a856b0e
定义基类 实现长方形和圆形的继承 实现正方形对长方形的继承
除基类以外 每个类重写构造方法和GetArea()函数,来实现面积的计算和打印数据
if s==int(s): print(int(s)) else: print(s)
此代码段实现圆面积整形数据和浮点数据的分别输出
main()函数实现类的实例化、赋值和调用类方法输出
class shape: def _init_(self,x,y): self.x=x self.y=y class Rectangle(shape): def _init_(self,l,h): self.l=l self.h=h def GetArea(self): print(self.h*self.l) class Circle(shape): def _init_(self,r): self.r=r def GetArea(self): s=self.r*self.r*3.14 if s==int(s): print(int(s)) else: print(s) class Square(Rectangle): def _init_(self,d): self.d=d def GetArea(self): print(self.d**2) def main(): l,h=map(eval,input().split()) r=eval(input()) d=eval(input()) S1=Rectangle() S1.l=l S1.h=h S1.GetArea() S2=Circle() S2.r=r S2.GetArea() S3=Square() S3.d=d S3.GetArea() main()