有石头,海藻和地方3类。但是在创建类的对象时,Seaweed 写道,没有 X、Y 属性。海藻新的init属性如何使用 ,所以继承的地方属性
class Place:
color = (52,152,219)
def __init__(self, x,y):
self.x = x
self.y = y
def aseta(self):
square = pygame.Rect(self.x*10, self.y*10, 10, 10)
pygame.draw.rect(screen, self.color, square)
class Stone(Place):
color = (0,0,0)
class Seaweed(Place):
color = (2,124,2)
def __init__(self, lifePeriod , age, reproductivePeriod ):
self.lifePeriod = lifePeriod;
self.age = age;
self.reproductivePeriod = reproductivePeriod;
def aging(self):
self.age += 1 ;
# def reproduction():
# def dying():
pond = [[ 0 for x in range(width)] for y in range(width)]
for y in range(height):
for x in range(width):
pond[x][y] = Place(x,y);
pond[x][y].aseta()
pygame.display.flip();
num = 0
while num <= stone_count:
x = random.randint(1,width-2)
y = random.randint(1,height-2)
if isinstance(pond[x][y], Place):
pond[x][y] = Stone(x,y)
pond[x][y].aseta();
num += 1
pygame.display.flip();
seawedNum = 0
while seawedNum <= seawed_count:
x = random.randint(1,width-2)
y = random.randint(1,height-2)
if isinstance(pond[x][y], Place):
pond[x][y] = Seaweed(1,1,1)
pond[x][y].aseta();
seawedNum += 1
pygame.display.flip();
例如,在python中,在子类构造函数
Seaweed
中,您需要调用祖先构造函数。毕竟,是他为对象添加了属性。构造函数的任务,原则上是初始化对象,使其具有满足对象完整性条件的正确状态。属性是一种特殊情况。并且调用祖先构造函数是强制性的。