C#no找到适合覆盖的方法

时间:2012-10-31 04:37:37

标签: c# abstract

这是我的基类

public abstract class IEnvelopeFactory {

        public Queue<IEnvelopeFactory> m_Queue;
        //public Queue<IEnvelopeFactory> m_Queue<IEnvelopeFactory>;

        public IEnvelopeFactory(){

        }

        ~IEnvelopeFactory(){

        }

        public virtual void Dispose(){

        }

        /// <summary>
        /// Parsing
        /// </summary>
        /// <param name="input"></param>
        /// <param name="envelope"></param>
        public abstract bool Parse(string input, out Envelope envelope);

        /// <summary>
        /// Formatting
        /// </summary>
        /// <param name="env"></param>
        /// <param name="envStr"></param>
        public abstract bool Format(Envelope env, out string envStr);

    }

和Child Class如下

public class XMLTYPE4Factory : IEnvelopeFactory
    {

        public XMLTYPE4 m_XMLTYPE4;

        public XMLTYPE4Factory()
        {

        }

        ~XMLTYPE4Factory()
        {

        }

        public override void Dispose()
        {

        }

        /// <summary>
        /// Parsing
        /// </summary>
        /// <param name="input"></param>
        public override Envelope Parse(string input)
        {

            return null;
        }

        /// <summary>
        /// Formatting
        /// </summary>
        /// <param name="env"></param>
        public override string Format(Envelope env)
        {

            return "";
        }

    }

我收到以下错误

'XMLTYPE4Factory' does not implement inherited abstract member 
'IEnvelopeFactory.Format(CCN.MSG.ENV.Envelope, out string)' 


'XMLTYPE4Factory' does not implement inherited abstract member 
'IEnvelopeFactory.Parse(string, out CCN.MSG.ENV.Envelope)'

3 个答案:

答案 0 :(得分:1)

方法签名不匹配:

public abstract bool Parse(string input, out Envelope envelope);
public override Envelope Parse(string input)

另外,如果一个类名不是一个接口,你就不应该为它添加前缀。

答案 1 :(得分:1)

是的,签名与您在基类中定义的不匹配。格式是否应该返回一个字符串并且还有一个字符串作为输出参数?

C#接口中的标准命名约定也以I开头。您应该从基类中删除I前缀。

public class XMLTYPE4Factory : IEnvelopeFactory {

    public XMLTYPE4 m_XMLTYPE4;

    public XMLTYPE4Factory()
    {

    }

    ~XMLTYPE4Factory()
    {

    }

    public override void Dispose()
    {

    }

    /// <summary>
    /// Parsing
    /// </summary>
    /// <param name="input"></param>
    public override Envelope Parse(string input, out string envStr)
    {
envStr= null;
        return null;
    }

    /// <summary>
    /// Formatting
    /// </summary>
    /// <param name="env"></param>
    public override string Format(Envelope env, out string envStr)
    {
        envStr = null;
        return "";
    }

}

答案 2 :(得分:1)

您尚未使用正确的参数实现方法

Envelope env;
string s;

public override Envelope Parse(string input,out Envelope env)
{
    env = new Envelope();
    return env;
}

public override string Format(Envelope env,out string s)
{
    s="somestring";
    return s;
}