【发布时间】:2011-06-24 08:31:07
【问题描述】:
有人告诉我要把我的班级抽象化:
public abstract class Airplane_Abstract
并创建一个名为 move virtual 的方法
public virtual void Move()
{
//use the property to ensure that there is a valid position object
double radians = PlanePosition.Direction * (Math.PI / 180.0);
// change the x location by the x vector of the speed
PlanePosition.X_Coordinate += (int)(PlanePosition.Speed * Math.Cos(radians));
// change the y location by the y vector of the speed
PlanePosition.Y_Coordinate += (int)(PlanePosition.Speed * Math.Sin(radians));
}
另外 4 种方法应该是“纯虚拟方法”。 那究竟是什么?
它们现在看起来都是这样的:
public virtual void TurnRight()
{
// turn right relative to the airplane
if (PlanePosition.Direction >= 0 && PlanePosition.Direction < Position.MAX_COMPASS_DIRECTION)
PlanePosition.Direction += 1;
else
PlanePosition.Direction = Position.MIN_COMPASS_DIRECTION; //due north
}
【问题讨论】:
-
如果有兴趣,关于纯虚函数和非纯虚函数的讨论(优缺点),我会自私地重定向到我的a blog post。 :)
标签: c# abstract-class pure-virtual