2011-04-20 84 views
0

我想這樣做(我對Silverlight的,但沒有具體的所以要做到這一點也WinForm的和WPF).NET類接口,繼承和圖書館:誤差不實現接口成員

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 MyComponents

爲什麼?這是庫

代碼
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 
    { 

    } 
} 

回答

2

我相信你在這裏有兩個錯誤,不是嗎?應該有一個錯誤,說SetModel應該有一個主體,因爲IMyManager不是一個接口或一個抽象類!

所以,我相信你應該有一個方法的機構,然後它必須是「公開」的,因爲它是接口實現的一部分。而且您還應該將IMyManager重命名爲「MyManager」,因爲它不是接口。你應該有你的類是這樣的:

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

希望這有助於:)

+0

好,我犯了一個錯誤有但那一點都不高。我也在界面中聲明瞭public void。 – user310291 2011-04-20 16:10:49

1

試試這個:(!合同)

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

符合接口的類必須在公開的方式實現它。

1

您也可以嘗試明確的實現,如:

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