Reputation: 2773
I'm making a program where there are some balls bouncing around, but now I want to inherit that class and make that new one impossible to set it's velocity
i've tried this but that doesn't do 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 {
get { return Vel; }
set { Vel = Vel; }
}
}
how should I do this?
Upvotes: 2
Views: 1371
Reputation: 172200
I'm making a program where there are some balls bouncing around, but now I want to inherit that class and make that new one impossible to set it's velocity
Your requirement violates the Substitution Principle. If the velocity of a Ball
can be changed, the velocity of any class inheriting from Ball
must also be changeable.
Solution: Instead of inheriting ConstantSpeedBall
from Ball
, create a new base class BallBase
and inherit both ConstantSpeedBall
and ChangingVelocityBall
from it.
Upvotes: 2