简体   繁体   English

必须声明一个主体,因为它没有标记为抽象外部或局部

[英]must declare a body because it is not marked abstract extern or partial

I have the code and getting the error shown above. 我有代码,并得到上面显示的错误。 But the Class is defined as abstract, so I shouldn't need a code body. 但是Class被定义为抽象的,所以我不需要代码体。

public abstract class Endian
    {
        public short ToInt16(byte[] value, int startIndex)
        {
            return unchecked((short)FromBytes(value, startIndex, 2));
        }

        public int ToInt32(byte[] value, int startIndex)
        {
            return unchecked((int)FromBytes(value, startIndex, 4));
        }

        public long ToInt64(byte[] value, int startIndex)
        {
            return FromBytes(value, startIndex, 8);
        }

        // This same method can be used by int16, int32 and int64.
        protected virtual long FromBytes(byte[] buffer, int startIndex, int len); // << Error here
    }

The virtual modifier is used to indicate that the method can be overridden in a derived class but needs to be defined in the base class (ie by giving it a method body). virtual修饰符用于指示该方法可以在派生类中重写,但需要在基类中定义(即,给它一个方法主体)。 You want to use the abstract modifier instead of virtual , as the error message indicates, which means that the method must be overridden. 如错误消息所示,您想使用abstract修饰符而不是virtual ,这意味着必须重写该方法。

Methods that are abstract must have the abstract modifier even though the class is already marked abstract in order to clearly differentiate them, and they cannot contain a method body (just a semicolon at the end). 即使类已经被标记为abstract以便清楚地区分,抽象方法也必须具有abstract修饰符,并且它们不能包含方法主体(仅以分号结尾)。 It's just the rule and it is what it is. 这只是规则而已。

To be abstract, a method must be declared as such: 要抽象化,必须这样声明一个方法:

protected abstract long FromBytes(byte[] buffer, int startIndex, int len);

You where declaring it virtual, which means it is a proper method, with an implementation, that can be overridden by derived classes. 您在哪里声明它是虚拟的,这意味着它是具有实现的正确方法,可以被派生类覆盖。

But the Class is defined as abstract, so I shouldn't need a code body. 但是Class被定义为抽象的,所以我不需要代码体。

Why would you expect that? 为什么会这样呢?

protected virtual long FromBytes(byte[] buffer, int startIndex, int len);

Method is still marked as virtual, it expects a body and can be overriden. 方法仍标记为虚拟,它需要一个正文并且可以被覆盖。 You should declare it as abstract if you don't want to add the definition. 如果不想添加定义,则应将其声明为抽象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#“必须声明一个主体,因为它没有被标记为抽象、外部或部分” - C# “must declare a body because it is not marked abstract, extern, or partial” 错误:必须声明一个主体,因为它没有被标记为抽象,外部或局部 - Error: must declare a body because it is not marked abstract, extern, or partial C#必须声明一个主体,因为它没有被标记为抽象,外部或部分 - C# must declare a body because it is not marked abstract, extern, or partial struct Cat(string,string,bool,bool)&#39;必须声明一个主体,因为它没有被标记为abstract,extern或局部 - struct Cat(string, string, bool, bool)' must declare a body because it is not marked abstract, extern, or partial 我没有解决“必须声明一个主体,因为它没有被标记为抽象,外部或部分”的问题吗? - i don't solve “must declare a body because it is not marked abstract, extern, or partial” problem? DispatcherTimer.Start()&#39;必须声明一个主体,因为它在MVC4中未标记为抽象,外部或部分 - DispatcherTimer.Start()' must declare a body because it is not marked abstract, extern, or partial in MVC4 “warp(Vector3)”必须声明一个主体,因为它没有标记为抽象、外部或部分 - 'warp(Vector3)' must declare a body because it is not marked abstract, extern, or partial C# 错误:声明一个主体,因为它没有标记为抽象、外部或部分 - C# errors: declare a body because it is not marked abstract, extern, or partial 必须声明一个主体,因为它没有标记为抽象,外部或部分 - Must declare a body becase it is not marked abstract, extern or partial 获取必须声明一个没有标记为抽象,外部或局部的主体 - get must declare a body it is not marked abstract, extern, or partial
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM