2016-04-17 10 views
-2

私は現在コンピュータサイエンスコースの授業を行っていますが、私はそれ以上の進歩を妨げるロードブロックに遭遇しました。私はInfoCardインターフェイスを返そうとしていますが、どうすればよいか分かりません。どのように私はインターフェイスのInfocardのインスタンスを返すだろうか?

+0

あなたの質問は曖昧です、もっと詳しく... –

+0

あなたは実装を選択してそれを送り返します:) –

答えて

-1

次のコードが便利です。

public IInfoCard CreateNewInfoCard(string category) 
{ 
    return new InfoCard(); 
} 

public interface IInfoCard 
{ 
    int foo { get; set; } 
} 

public interface IInfoCardFactory : IInfoCard 
{ 
    IInfoCard CreateNewInfoCard(string category); 
    IInfoCard CreateInfoCard(string initialDetails); 
    string[] CategoriesSupported { get; } 
    string GetDescription(string category); 
} 

public class InfoCard : IInfoCardFactory 
{ 
    public IInfoCard CreateNewInfoCard(string category) 
    { 
     throw new NotImplementedException(); 
    } 

    public IInfoCard CreateInfoCard(string initialDetails) 
    { 
     throw new NotImplementedException(); 
    } 

    public string[] CategoriesSupported 
    { 
     get { throw new NotImplementedException(); } 
    } 

    public string GetDescription(string category) 
    { 
     throw new NotImplementedException(); 
    } 

    public int foo 
    { 
     get 
     { 
      throw new NotImplementedException(); 
     } 
     set 
     { 
      throw new NotImplementedException(); 
     } 
    } 
} 
0

あなたは、あなたの関数の戻り値の型としてのインターフェイスを持つことができますが、あなたのクラスの実装を返すMOSTおよびインタフェース戻り値の型の利点は、あなたのインターフェイス 例のいずれかのinplementationを返すことができます:

interface ISampleInterface 
{ 
    string SampleMethod(); 
} 

class ImplementationClass1 : ISampleInterface 
{ 
    // Explicit interface member implementation: 
    void ISampleInterface.SampleMethod() 
    { 
     // Method implementation. 
     return "implementation1"; 
    } 
} 

class ImplementationClass2 : ISampleInterface 
{ 
    // Explicit interface member implementation: 
    void ISampleInterface.SampleMethod() 
    { 
     // Method implementation. 
     return "implementation2"; 
    } 
} 

あなたは関数の戻り値の型として、次のようにそのインタフェースを使用することができます:コードを使用すると、別のimplemを返すことができることを意味していること

public ISampleInterface MyFunction(bool t) 
{ 
    return t : new ImplementationClass1() ? ImplementationClass2(); 
} 

あなたの選択によるあなたのインタフェースの実装

関連する問題