【问题标题】:.NET Class Interface, Inheritance and Library: error does not implement interface member.NET 类接口、继承和库:错误未实现接口成员
【发布时间】:2011-04-20 10:56:13
【问题描述】:

我想这样做(我在 silverlight 上但没有什么特别的,所以想在 winform 和 wpf 上也这样做)

namespace MyComponents
{
    public class IMyManager : ILibManager
    {
        void SetModel(ILibModel model);
    }
}

但得到这个错误

错误 2“MyComponents.IMymanager”未实现接口成员“lib.manager.ILibManager.SetModel(lib.model.ILibModel)”。 “MyComponents.IMymanager.SetModel(lib.model.ILibModel)”不能实现接口成员,因为它不是公共的。 C:...\MyComponents\MyComponents\IMymanager.cs 17 18 我的组件

为什么?这是Lib中的代码

using lib.model;

using System;
using System.Collections.Generic;
using System.Text;

namespace lib.manager
{
    public interface ILibManager
    {
        public void SetModel(ILibModel model);
    }
}

using lib.model;

using System;
using System.Net;
using System.Windows;


namespace lib.manager
{
    public class Manager: IManager
    {
        // Constructor
        public Manager() { 

        }

        public void SetModel(ILibModel model) {

        }

    }
}

namespace lib.model
{
    public interface ILibModel
    {

    }
}


namespace lib.model
{
    public class Model : ILibModel
    {

    }
}

【问题讨论】:

    标签: c# .net wpf winforms silverlight


    【解决方案1】:

    我相信你这里有两个错误,不是吗?应该有一个错误说 SetModel 应该有一个主体,因为 IMyManager 不是接口或抽象类!

    所以,我相信您应该为该方法提供一个主体,然后它必须是“公共的”,因为它是接口实现的一部分。您还应该将 IMyManager 重命名为“MyManager”,因为它不是一个界面。你应该有这样的课程:

    public class MyManager : ILibManager
    {
        public void SetModel(ILibModel model)
        {
            // implementation of SetModel
        }
    }
    

    希望这会有所帮助:)

    【讨论】:

    • 好的,我在那里犯了一个错误,但这还不是全部。我还在接口中声明了 public void。
    【解决方案2】:

    试试这个:

    namespace MyComponents
    {
        public class MyManager : ILibManager
        {
            public void SetModel(ILibModel model)
            {
               // ...
            }
        }
    }
    

    符合接口(契约!)的类必须以公共方式实现它。

    【讨论】:

      【解决方案3】:

      您也可以尝试显式实现,例如:

      public class MyManager : ILibManager
      {
          void ILibManager:SetModel(ILibModel model)
          {
              // ...
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        • 2013-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-04
        • 1970-01-01
        相关资源
        最近更新 更多