在Python 3中更改派生类中的成员函数
在Python 3中更改派生类中的成员函数,python,python-3.x,oop,Python,Python 3.x,Oop,shape被定义为基类。类矩形和圆形是从类形状派生的类。我正在尝试创建一个新的派生类car,它继承自circle和rectangle。类的任何实例化都将是一个矩形和矩形下方的两个圆。 但是,我不知道如何重新定义set_color(),以便为矩形和圆形设置不同的颜色。提前谢谢 !pip install calysto from calysto.graphics import * from calysto.display import display, clear_output impor
shape
被定义为基类。类矩形
和圆形
是从类形状
派生的类。我正在尝试创建一个新的派生类car
,它继承自circle
和rectangle
。类的任何实例化都将是一个矩形和矩形下方的两个圆。
但是,我不知道如何重新定义set_color()
,以便为矩形和圆形设置不同的颜色。提前谢谢
!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)
对于此问题,如果使用组合而不是继承,效率会更高。组合允许重用其包含的组件的实现。复合类(新形成的类)不继承组件类接口,但可以使用其实现。在这种情况下,car类的对象是圆形类的两个对象和矩形类的一个对象的组合,但不是它们中任何一个的派生对象。因此,它可以像这样创建:
等级车:
def _u初始(自身、车身、车轮):
self.body=身体
自动轮
#这里的主体是**矩形**类的对象,轮子是**圆形**类的对象
#车轮和车身的颜色可单独设置为
def设置颜色(自身、车身颜色、车轮颜色):
self.wheel.set_颜色(wheel_颜色)
self.body.set_color(body_color)
#因此,绘制方法将发生变化
对于此问题,如果使用组合而不是继承,效率会更高。组合允许重用其包含的组件的实现。复合类(新形成的类)不继承组件类接口,但可以使用其实现。在这种情况下,car类的对象是圆形类的两个对象和矩形类的一个对象的组合,但不是它们中任何一个的派生对象。因此,它可以像这样创建:
等级车:
def _u初始(自身、车身、车轮):
self.body=身体
自动轮
#这里的主体是**矩形**类的对象,轮子是**圆形**类的对象
#车轮和车身的颜色可单独设置为
def设置颜色(自身、车身颜色、车轮颜色):
self.wheel.set_颜色(wheel_颜色)
self.body.set_color(body_color)
#因此,绘制方法将发生变化
set\u color
需要显式调用circle.set\u color
和rectangle.set\u color
。此外,这并不是一个很好的使用(多重)继承,而是考虑使用构图。<代码> SETIGHORIX <代码>需要调用<代码>圆。此外,这并不是一个很好的使用(多)继承,考虑使用构图代替。