C# 中的纯虚方法?
我被告知要使我的类抽象:
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
}
I've been told to make my class abstract:
public abstract class Airplane_Abstract
And to make a method called 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));
}
And that 4 other methods should be "pure virtual methods."
What is that exactly?
They all look like this right now:
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我的猜测是,告诉你编写“纯虚拟”方法的人是 C++ 程序员而不是 C# 程序员...但等效的是抽象方法:
强制具体子类使用
TurnRight
重写真正的实施。My guess is that whoever told you to write a "pure virtual" method was a C++ programmer rather than a C# programmer... but the equivalent is an abstract method:
That forces concrete subclasses to override
TurnRight
with a real implementation.“纯虚拟”是C++术语。 C# 的等价物是抽象方法。
"Pure virtual" is C++ terminology. The C# equivalent is an abstract method.
它们可能意味着这些方法应该被标记为“抽象”。
然后,您需要在子类中实现它们,而不是空的虚拟方法,子类不必重写它。
They probably mean that the methods should be marked
abstract
.You will then need to implement them in the subclasses, as opposed to an empty virtual method, where the subclass would not have to override it.
纯虚函数是 C++ 的术语,但在 C# 中,如果在派生类中实现的函数并且该派生类不是抽象类。
A pure virtual function is terminology of C++ but in C# if a function which is implemented in Derived class and that derived class is not an Abstract class.
或者甚至你可以使用接口,但需要一些小限制:
但是,你必须实现所有
IControllable
和ITurnable
的函数声明> 已经赋值,否则会出现编译错误。如果您想要可选的虚拟实现,则必须使用抽象类
来实现虚拟方法,使用接口
来实现纯虚拟方法。其实
interface
函数和abstract
函数是有区别的,interface
只声明了函数,所有< code>interface 函数必须全部公开,因此没有诸如private
或protected
等花哨的类属性,因此速度非常快,而abstract 函数是没有实现的实际类方法,并且强制在派生类中实现,因此您可以将
private
、protected< /code> 并使用
抽象
函数访问成员变量,并且大多数时候速度较慢,因为类继承关系是在运行时分析的。 (又名虚函数表)Or even you can go interface, thought some little limitation is required:
However, you will have to implement all the function declaration of both
IControllable
andITurnable
have assigned, otherwise an compiler error will occur. If you want optional virutal implementation, you would have to goabstract class
for virtual method withinterface
for pure virtual method.In fact, there is a difference between
interface
function andabstract
function, thatinterface
only declares function, allinterface
function have to go all public so there's no fancy class property such asprivate
orprotected
so it's very fast, whileabstract
function are actual class method without implementation and forces the implementation in the derived class, so you can putprivate
,protected
and access member variables withabstract
functions, and most of the time it's slower because the class inheritance relations are analyzed in run time. (aka vtable)然后,您在 Airplane_Abstract 类中没有实现,而是强制该类的使用者“继承者”实现它们。
在继承并实现抽象函数之前,Airplane_Abstract 类不可用。
You then don't have an implementation in the Airplane_Abstract Class but are forcing consumers "inheritors" of the Class to implement them.
The Airplane_Abstract Class is unusable until you inherit and implement the abstract functions.
http://en.wikipedia.org/wiki/Virtual_function
“在面向对象编程中,虚函数或虚方法是一个函数或方法,其行为可以在继承类中被具有相同签名的函数覆盖。”
谷歌永远是你的朋友。
http://en.wikipedia.org/wiki/Virtual_function
"In object-oriented programming, a virtual function or virtual method is a function or method whose behaviour can be overridden within an inheriting class by a function with the same signature."
Google is always your friend.