我目前正在为我的计算机科学课程做作业,但是我遇到了阻碍我继续前进的障碍。我试图返回InfoCard界面,我不确定如何。
public interface IInfoCardFactory
{
IInfoCard CreateNewInfoCard(string category);
IInfoCard CreateInfoCard(string initialDetails);
string[] CategoriesSupported { get; }
string GetDescription(string category);
}
public IInfoCard CreateNewInfoCard(string category)
{
......
}
答案 0 :(得分:0)
你可以有一个接口作为你的函数返回类型,但你最常返回类的实现,接口返回类型的优点是你可以返回你的接口的任何实现 例如:
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";
}
}
你可以使用像这样的接口作为函数返回类型:
public ISampleInterface MyFunction(bool t)
{
return t : new ImplementationClass1() ? ImplementationClass2();
}
该代码意味着您可以通过选择返回不同的界面实现
答案 1 :(得分:-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();
}
}
}