书籍:《PYTHON游戏编程入门》(More Python Programming for the Absolute Beginner)
本文为第一章后面的挑战题,欢迎交流
打开GeometryDemo.py程序,并且创建一个继承自Point 的新类,名为Ellipse。它有一个水平半径和垂直半径,而不是像Circle那样只有一个半径。
# ch1 Question 1
# python 3.8.0
# pygame 1.9.6
#第一个基类,Point
# 这里的object是一切类的基类,也可不写,像Size类一样
class Point(object):
x = 0.0
y = 0.0
# 这里的变量需要赋初始值,否则会报错
def __init__(self, x=0.0 , y=0.0):
# 可以给参数设置初始值,但是变量定义那里的赋初值不可省
self.x = x
self.y = y
# 为了区分原本的成员和传入的参数,这里面使用self来标记
print('Point Constructor')
def ToString(self):
return "{X:" + str(self.x) + ",Y:" + str(self.y) + "}"
# 返回一个string
#第二个基类:Size
class Size():
width = 0.0
height = 0.0
def __init__(self, w, h):
self.width = w
self.height = h
# 如果原本的成员和传入的参数不同名,也需要用self来标记,否则就是新变量
print("Size Constructor")
def ToString(self):
return "{WIDTH=" + str(self.width) + \
",HEIGHT=" + str(self.height) + "}"
#子类Circle,继承自Point
class Circle(Point):
# 继承自Point类
radius = 0.0
def __init__(self, x, y, r):
super().__init__(x, y)
# 调用父类的构造函数,只有一个父类的情况下可以使用super()表示父类
#Point.__init__(self, x, y)
# 如果使用父类名字的话,则需要将self参数传进去
# 这里不能使用Point().__init__(x, y),因为它会生成一个新的Point对象
self.radius = r
print('Circle Constructor')
def ToString(self):
return super().ToString() + \
',{RADIUS=' + str(self.radius) + '}'
# 同理,这里也可以使用Point.ToString(self)
#子类Rectangle,继承自Point,和Size
class Rectangle(Point, Size):
def __init__(self, x, y, w, h):
Point.__init__(self, x, y)
#super().__init__(x, y)
Size.__init__(self, w, h)
# 当继承自多个类时,必须使用父类的名称进行调用,否则将按照继承顺序
print('Rectangle Constructor')
def ToString(self):
return Point.ToString(self) + ',' + Size.ToString(self)
class Ellipse(Point):
vertical_radius = 0.0
horizontal_radius = 0.0
def __init__(self, x, y, v, h):
super().__init__(x, y)
self.vertical_radius = v
self.horizontal_radius = h
print('Ellipse Constructor')
def ToString(self):
return super().ToString() + \
',{VERTICAL_RADIUS=' + str(self.vertical_radius) + \
',HORIZONTAL_RADIUS=' + str(self.horizontal_radius) + '}'
p = Point(10,20)
print(p.ToString())
s = Size(80,70)
print(s.ToString())
c = Circle(100,100,50)
print(c.ToString())
r = Rectangle(200,250,40,50)
print(r.ToString())
e = Ellipse(200,300,200,100)
print(e.ToString())
给Rectangle类添加一个名为CalcArea()的方法,它返回Rectangle的面积。计算公式是: Area =Width x Height。测试该方法以确保它能工作。
将上一题的Rectangle类更改为:
class Rectangle(Point, Size):
def __init__(self, x, y, w, h):
Point.__init__(self, x, y)
Size.__init__(self, w, h)
print('Rectangle Constructor')
def ToString(self):
return Point.ToString(self) + ',' + Size.ToString(self)
def CalcArea(self):
return self.width * self.height
给Circle类添加一个名为CalcCircum()的新方法,它返回圆的周长。计算公式是Circumference= 2* Pi* Radius (Pi = 3.14159)。测试该方法以确保它能工作。
将上一题的Circle类更改为:
class Circle(Point):
radius = 0.0
def __init__(self, x, y, r):
super().__init__(x, y)
self.radius = r
print('Circle Constructor')
def ToString(self):
return super().ToString() + \
',{RADIUS=' + str(self.radius) + '}'
def CalcCircum(self):
return 2*math.pi*self.radius
# 需要import math
附上最后的总代码:
# ch1 Question 1-3
# python 3.8.0
# pygame 1.9.6
import math
class Point(object):
x = 0.0
y = 0.0
def __init__(self, x=0.0 , y=0.0):
self.x = x
self.y = y
print('Point Constructor')
def ToString(self):
return "{X:" + str(self.x) + ",Y:" + str(self.y) + "}"
#第二个基类:Size
class Size():
width = 0.0
height = 0.0
def __init__(self, w, h):
self.width = w
self.height = h
print("Size Constructor")
def ToString(self):
return "{WIDTH=" + str(self.width) + \
",HEIGHT=" + str(self.height) + "}"
class Circle(Point):
radius = 0.0
def __init__(self, x, y, r):
super().__init__(x, y)
self.radius = r
print('Circle Constructor')
def ToString(self):
return super().ToString() + \
',{RADIUS=' + str(self.radius) + '}'
def CalcCircum(self):
return 2*math.pi*self.radius
class Rectangle(Point, Size):
def __init__(self, x, y, w, h):
Point.__init__(self, x, y)
#super().__init__(x, y)
Size.__init__(self, w, h)
print('Rectangle Constructor')
def ToString(self):
return Point.ToString(self) + ',' + Size.ToString(self)
def CalcArea(self):
return self.width * self.height
class Ellipse(Point):
vertical_radius = 0.0
horizontal_radius = 0.0
def __init__(self, x, y, v, h):
super().__init__(x, y)
self.vertical_radius = v
self.horizontal_radius = h
print('Ellipse Constructor')
def ToString(self):
return super().ToString() + \
',{VERTICAL_RADIUS=' + str(self.vertical_radius) + \
',HORIZONTAL_RADIUS=' + str(self.horizontal_radius) + '}'
p = Point(10,20)
print(p.ToString())
s = Size(80,70)
print(s.ToString())
c = Circle(100,100,50)
print(c.ToString())
print('CalcCircum of Circle is: ',c.CalcCircum())
r = Rectangle(200,250,40,50)
print(r.ToString())
print('Area of Rectangle is ', r.CalcArea())
e = Ellipse(200,300,200,100)
print(e.ToString())
我正在学习中,如有不当之处请留言指出哦~感谢!