Skip to main content
 首页 » 编程设计

python之如何使用 turtle 父类(super class)

2024年11月01日9rubylouvre

我正在使用 turtle 图形制作太阳系复制品。 我想制作一颗恒星和一颗行星,它们都继承自同一个 turtle 父类(super class)“solar_element”,但我遇到了问题。

我可以毫无问题地使用两种不同的 turtle 类来制作恒星和行星:

from turtle import Turtle 
 
class star(Turtle): 
   def __init__(self, Name, Radius, Mass, Colour): 
    Turtle.__init__(self, shape = "circle") 
    self.Name = Name 
    self.Radius = Radius 
    self.Mass = Mass 
    self.color(Colour) 
    self.shapesize(self.Radius/50) 
 
class planet(Turtle): 
 def __init__(self, Name, Radius, Mass, Colour, Dist, velX, velY): 
    Turtle.__init__(self, shape= "circle") 
    self.Name = Name 
    self.Radius = Radius 
    self.Mass = Mass 
    self.color(Colour) 
    self.Dist = Dist 
    self.velX = velX 
    self.velY = velY 
    self.x_pos = sun.Radius + self.Dist + self.Radius 
    self.y_pos = 0 
    self.shapesize(self.Radius/50) 
 
sun = star('myStar', 500.0, 15000.0, "yellow"); 
earth = planet('P1', 150.0, 1000.0, "green", 0.25, 0.5, 2.0); 

但是当我试图让它们从一个父类(super class)继承时,像这样:

from turtle import Turtle 
 
class solar_element(Turtle): 
 def __init__(self, Name, Radius, Mass, Colour): 
    self.Name = Name 
    self.Radius = Radius 
    self.Mass = Mass 
    self.Colour = Colour 
    self.color(self.Colour) 
    self.shapesize(self.Radius/50) 
 
class star(solar_element): 
 def __init__(self, Name, Radius, Mass, Colour): 
    solar_element.__init__(self, Name, Radius, Mass, Colour) 
    Turtle.__init__(self, shape = "circle") 
 
class planet(solar_element): 
 def __init__(self, Name, Radius, Mass, Colour, Dist, velX, velY): 
    solar_element.__init__(self, Name, Radius, Mass, Colour) 
    Turtle.__init__(self, shape = "circle") 
    self.Dist = Dist 
    self.velX = velX 
    self.velY = velY 
    self.x_pos = sun.Radius + self.Dist + self.Radius 
    self.y_pos = 0 
 
sun = star('myStar', 500.0, 15000.0, "yellow"); 
earth = planet('P1', 150.0, 1000.0, "green", 0.25, 0.5, 2.0); 

我收到以下错误:

Traceback (most recent call last): 
  File "C:\Users\Kev\Dropbox\OOP\testing_classes.py", line 62, in <module> 
    sun = star('myStar', 500.0, 15000.0, "yellow"); 
  File "C:\Users\Kev\Dropbox\OOP\testing_classes.py", line 47, in __init__ 
    solar_element.__init__(self, Name, Radius, Mass, Colour) 
  File "C:\Users\Kev\Dropbox\OOP\testing_classes.py", line 42, in __init__ 
    self.color(self.Colour) 
  File "C:\Python33\lib\turtle.py", line 2208, in color 
    pcolor = self._colorstr(pcolor) 
  File "C:\Python33\lib\turtle.py", line 2688, in _colorstr 
    return self.screen._colorstr(args) 
AttributeError: 'star' object has no attribute 'screen' 

我意识到我可以坚持使用两个类,但我想使用父类(super class),因为我仍在努力学习 python。

请您参考如下方法:

在 star 和 planet 类中调用 solar_element 构造函数之前,首先调用 Turtle 构造函数。

因此,将 star 类的 __init__ 方法中的代码更改为:

Turtle.__init__(self, shape = "circle") 
solar_element.__init__(self, Name, Radius, Mass, Colour) 

并将 planet 类的 __init__ 方法中的代码更改为:

Turtle.__init__(self, shape = "circle") 
solar_element.__init__(self, Name, Radius, Mass, Colour) 

首先,我们通常需要在 __init__ 方法中调用基类的构造函数来覆盖它。 事实上,当我们想在 __init__ 方法中使用类的一部分时,我们首先调用它,这里例如一个 color 方法, 在任何人都可以使用它之前,此方法需要一些准备工作。 这里例如 Turtle 的 color 方法的 screen 需要先设置才能使用这个方法。

对您的代码进行一些重构:

  • 类名通常应使用 CapWords 约定
  • 你不需要 ; 在行尾。
  • 变量名应小写,并根据需要用下划线分隔单词,以提高可读性。
  • 使用 super(YourInheritedClass, self).__init__() 而不是 ParentClass.__init__(self)

代码:

from turtle import Turtle 
 
class SolarElement(Turtle): 
    def __init__(self, name, radius, mass, colour, shape='circle'): 
        # or Turtle.__init__(self, shape=shape) 
        super(SolarElement, self).__init__(shape=shape) 
        self.name = name 
        self.radius = radius 
        self.mass = mass 
        self.colour = colour 
        self.color(self.colour) 
        self.shapesize(self.radius / 50) 
 
 
class Star(SolarElement): 
    def __init__(self, name, radius, mass, colour, 
                 shape='circle'): 
        SolarElement.__init__(self, name, radius, mass, colour, 
                              shape=shape) 
 
 
class Planet(SolarElement): 
    def __init__(self, name, radius, mass, colour, dist, vel_x, vel_y, 
                 shape='circle'): 
        SolarElement.__init__(self, name, radius, mass, colour, 
                              shape=shape) 
        self.dist = dist 
        self.vel_x = vel_x 
        self.vel_y = vel_y 
        self.x_pos = sun.radius + self.dist + self.radius 
        self.y_pos = 0 
 
sun = Star('myStar', 500.0, 15000.0, "yellow") 
earth = Planet('P1', 150.0, 1000.0, "green", 0.25, 0.5, 2.0)