【问题标题】:Changing a member function in a derived class in Python 3在 Python 3 中更改派生类中的成员函数
【发布时间】:2021-03-24 14:39:59
【问题描述】:

shape被定义为基类。类rectanglecircle 是派生自类shape 的类。我正在尝试创建一个新的派生类car,它继承自circlerectangleclass 的任何实例化都将是一个矩形和矩形下方的两个圆。 但是,我不知道如何重新定义set_color(),以便为矩形和轮子circle 设置不同的颜色。提前致谢。

    !pip install calysto

from calysto.graphics import *
from calysto.display import display, clear_output
import time
import math

CANVAS_240 = Canvas(size=(400, 400))

class shape:

  color = None
  x = None
  y = None
 
  def set_color(self, red, green, blue):
    self.color = (red, green, blue)
 
  def move_to(self, x, y):
    self.x = x
    self.y = y

  def move_on_line(self, x_fin, y_fin, count_of_steps):
    delta_x = (x_fin - self.x)/count_of_steps
    delta_y = (y_fin - self.y)/count_of_steps
    while self.x != x_fin and self.y != y_fin:
      shape_has_color = self.color 
      self.set_color(255,255,255)
      self.draw()
      self.color = shape_has_color
      self.move_to(self.x+delta_x,self.y+delta_y)
      self.draw()

  def displace(self, delta_x, delta_y):
    shape_has_color = self.color
    self.set_color(255, 255, 255) # that is white. i.e. the background color
    self.draw()
    self.color = shape_has_color
    self.move_to(self.x+delta_x, self.y+delta_y)
    self.draw()

  def __str__(self):
    return "shape object: color=%s coordinates=%s" % (self.color, (self.x, self.y))

  def __init__(self, x, y):
    self.x = x
    self.y = y

  def displace(self, delta_x, delta_y):
    shape_has_color = self.color
    self.set_color(255, 255, 255) # that is white. i.e. the background color
    self.draw()
    self.color = shape_has_color
    self.move_to(self.x+delta_x, self.y+delta_y)
    self.draw()

class circle(shape):

  radius = None

  def __init__(self, x, y, radius):
    self.x = x
    self.y = y
    self.radius = radius
    self.set_color(255, 0, 0)
     
  def draw(self):
    c = Circle((self.x, self.y), self.radius)
    c.fill(Color(self.color[0], self.color[1], self.color[2]))
    c.noStroke()
    clear_output(wait=True)
    c.draw(CANVAS_240)
    display(CANVAS_240)
    
    
    
class rectangle(shape):

  width = None
  height = None

  def __init__(self, x, y, width, height):
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.set_color(255, 0, 0)
     
  def draw(self):
    rb = (self.x+self.width, self.y)
    ru = (self.x+self.width, self.y+self.height)
    lu = (self.x, self.y+self.height)
    lb = (self.x, self.y)
    r = Polygon([lb,rb,ru,lu])
    r.fill(Color(self.color[0], self.color[1], self.color[2]))
    r.noStroke()
    clear_output(wait=True)
    r.draw(CANVAS_240)
    display(CANVAS_240)

class triangle(shape):
  dx2 = None
  dy2 = None
  dx3 = None
  dy3 = None

  def __init__(self, x,y,dx2,dy2,dx3,dy3):
    self.x = x
    self.y = y
    self.dx2 = dx2
    self.dy2 = dy2
    self.dx3 = dx3
    self.dy3 = dy3
    self.set_color(255,0,0)
  
  def draw(self):
    fi = (self.x,self.y)
    se = (self.x + self.dx2,self.y+self.dy2)
    th = (self.x+self.dx3,self.y+self.dy3)
    t = Polygon([fi,se,th])
    t.fill(Color(self.color[0], self.color[1], self.color[2]))
    t.noStroke()
    clear_output(wait=True)
    t.draw(CANVAS_240)
    display(CANVAS_240)

class car(rectangle,circle):
  width = None
  height = None
  radius = None

  def __init__(self, x, y, width, height, radius):
    rectangle.__init__(self,x,y,width,height)
    circle.__init__(self,x,y,radius)
    self.set_color(255,0,0)


  def draw(self):
    rb = (self.x+self.width, self.y)
    ru = (self.x+self.width, self.y + self.height)
    lb = (self.x,self.y)
    lu = (self.x,self.y+self.height)
    r = Polygon([lb,rb,ru,lu])
    w1 = Circle((self.x, self.y+self.height), self.radius)
    w2 = Circle((self.x+self.width, self.y+self.height), self.radius)
    r.fill(Color(self.color[0], self.color[1], self.color[2]))
    r.noStroke()
    w1.fill(Color(self.color[0], self.color[1], self.color[2]))
    w1.noStroke()
    w2.fill(Color(self.color[0], self.color[1], self.color[2]))
    w2.noStroke()
    clear_output(wait=True)
    r.draw(CANVAS_240)
    w1.draw(CANVAS_240)
    w2.draw(CANVAS_240)
    display(CANVAS_240)

【问题讨论】:

  • set_color 需要显式调用circle.set_colorrectangle.set_color。此外,这看起来不是很好地使用(多)继承,请考虑改用组合。

标签: python python-3.x oop


【解决方案1】:

对于这个问题,如果使用组合而不是继承会更有效。组合允许重用它包含的组件的实现。组合类(新形成的类)不继承组件类接口,但是它可以使用它的实现。在这种情况下,car类的对象是circle类的两个对象和rectangle类的一个对象的组合,但不是其中任何一个的派生对象。因此可以像这样创建它:

class car:
   def __init__(self,body,wheel):
      self.body=body
      self.wheel=wheel
#Here body is an object of **rectangle** class and wheel is an object of **circle** class
      #color of wheel and body can be independently set as
   def set_color(self,body_color,wheel_color):
      self.wheel.set_color(wheel_color)
      self.body.set_color(body_color)
#Accordingly draw method will change

【讨论】:

    猜你喜欢
    • 2011-09-24
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 2014-01-21
    • 1970-01-01
    相关资源
    最近更新 更多