Thu*_*ter 2 c# interface .net-4.0
我是C#新手,我正在尝试实现一个界面.我知道我不能将访问modiefiers放到接口方法上,那么如何在下面的'TestClass2'的公共静态'Create'方法中访问'TestValue'?我得到的错误是......
'TestClass1'不包含'TestValue'的定义,并且没有可以找到接受类型'TestClass1'的第一个参数的扩展方法'TestValue'
public interface IParent
{
string TestValue { get; }
}
public class TestClass1 : IParent
{
string IParent.TestValue
{
get { return "hello"; }
}
}
public class TestClass2
{
private string _testValue;
public static TestClass2 Create(TestClass1 input)
{
TestClass2 output = new TestClass2();
output._testValue = input.TestValue;
return output;
}
}
Run Code Online (Sandbox Code Playgroud)
在具体实现中添加public访问修饰符:
public class TestClass1 : IParent
{
private TestClass1 _testValue;
public string TestValue
{
get { return "hello"; }
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:当您实际编写显式接口实现时,我建议您查看以下SO问题:C#接口.隐式实现与显式实现