c# - Overriding property setter -
i'm making program there balls bouncing around, want inherit class , make new 1 impossible set it's velocity
i've tried doesn't anyting
public class ball { public ball(double posx, double posy, double rad, vector vel) { pos = new point(posx, posy); rad = rad; vel = vel; } public double rad { get; set; } public point pos { get; set; } public vector vel { get; set; } } public class staticball : ball { public staticball(double posx, double posy, double rad) : base(posx, posy, rad, new vector(0, 0)) { } public vector vel { { return vel; } set { vel = vel; } } }
how should this?
i'm making program there balls bouncing around, want inherit class , make new 1 impossible set it's velocity
your requirement violates substitution principle. if velocity of ball
can changed, velocity of class inheriting ball
must changeable.
solution: instead of inheriting constantspeedball
ball
, create new base class ballbase
, inherit both constantspeedball
, changingvelocityball
it.
Comments
Post a Comment