C# 中的纯虚方法?

发布于 2024-10-16 09:25:04 字数 1062 浏览 3 评论 0原文

我被告知要使我的类抽象:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

随波逐流 2024-10-23 09:25:04

我的猜测是,告诉你编写“纯虚拟”方法的人是 C++ 程序员而不是 C# 程序员...但等效的是抽象方法:

public abstract void TurnRight();

强制具体子类使用 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:

public abstract void TurnRight();

That forces concrete subclasses to override TurnRight with a real implementation.

路弥 2024-10-23 09:25:04

“纯虚拟”是C++术语。 C# 的等价物是抽象方法。

"Pure virtual" is C++ terminology. The C# equivalent is an abstract method.

旧夏天 2024-10-23 09:25:04

它们可能意味着这些方法应该被标记为“抽象”。

 public abstract void TurnRight();

然后,您需要在子类中实现它们,而不是空的虚拟方法,子类不必重写它。

They probably mean that the methods should be marked abstract.

 public abstract void TurnRight();

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.

撩发小公举 2024-10-23 09:25:04

纯虚函数是 C++ 的术语,但在 C# 中,如果在派生类中实现的函数并且该派生类不是抽象类。

abstract class PureVirtual
{
    public abstract void PureVirtualMethod();
}

class Derivered : PureVirtual
{
    public override void PureVirtualMethod()
    {
        Console.WriteLine("I'm pure virtual function");
    }
}

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.

abstract class PureVirtual
{
    public abstract void PureVirtualMethod();
}

class Derivered : PureVirtual
{
    public override void PureVirtualMethod()
    {
        Console.WriteLine("I'm pure virtual function");
    }
}
梦屿孤独相伴 2024-10-23 09:25:04

或者甚至你可以使用接口,但需要一些小限制:

public interface IControllable
{
    void Move(int step);
    void Backward(int step);
}

public interface ITurnable
{
   void TurnLeft(float angle);
   void TurnRight(float angle);
}

public class Airplane : IControllable, ITurnable
{
   void Move(int step)
   {
       // TODO: Implement code here...
   }
   void Backward(int step)
   {
       // TODO: Implement code here...
   }
   void TurnLeft(float angle)
   {
       // TODO: Implement code here...
   }
   void TurnRight(float angle)
   {
       // TODO: Implement code here...
   }
}

但是,你必须实现所有IControllableITurnable的函数声明> 已经赋值,否则会出现编译错误。如果您想要可选的虚拟实现,则必须使用抽象类来实现虚拟方法,使用接口来实现纯虚拟方法。

其实interface函数和abstract函数是有区别的,interface只声明了函数,所有< code>interface 函数必须全部公开,因此没有诸如 privateprotected 等花哨的类属性,因此速度非常快,而 abstract 函数是没有实现的实际类方法,并且强制在派生类中实现,因此您可以将 privateprotected< /code> 并使用抽象函数访问成员变量,并且大多数时候速度较慢,因为类继承关系是在运行时分析的。 (又名虚函数表)

Or even you can go interface, thought some little limitation is required:

public interface IControllable
{
    void Move(int step);
    void Backward(int step);
}

public interface ITurnable
{
   void TurnLeft(float angle);
   void TurnRight(float angle);
}

public class Airplane : IControllable, ITurnable
{
   void Move(int step)
   {
       // TODO: Implement code here...
   }
   void Backward(int step)
   {
       // TODO: Implement code here...
   }
   void TurnLeft(float angle)
   {
       // TODO: Implement code here...
   }
   void TurnRight(float angle)
   {
       // TODO: Implement code here...
   }
}

However, you will have to implement all the function declaration of both IControllable and ITurnable have assigned, otherwise an compiler error will occur. If you want optional virutal implementation, you would have to go abstract class for virtual method with interface for pure virtual method.

In fact, there is a difference between interface function and abstract function, that interface only declares function, all interface function have to go all public so there's no fancy class property such as private or protected so it's very fast, while abstract function are actual class method without implementation and forces the implementation in the derived class, so you can put private, protected and access member variables with abstract functions, and most of the time it's slower because the class inheritance relations are analyzed in run time. (aka vtable)

琴流音 2024-10-23 09:25:04

然后,您在 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.

萌酱 2024-10-23 09:25:04

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文