为什么我使用explict接口实现时出现编译错误 [英] why i get compilation error when i use explict interface implemntion
问题描述
interface IXYZ
{
string 姓名{设置; }
}
class 记录器:IXYZ
{
public string IXYZ.Name { get ; set ; }
private void GenerateAutoName()
{
Name = 名称; // 编译错误!
}
}
可以找到解释这里 [ ^ ]。这是可能对您有帮助的代码示例。请注意Main方法中的注释。
// explicit1.cs
interface IDimensions
{
float 长度();
float Width();
}
class 框:IDimensions
{
float lengthInches;
float widthInches;
public Box( float length, float width)
{
lengthInches = length;
widthInches = width;
}
// 显式接口成员实现:
float IDimensions.Length()
{
return lengthInches;
}
// 显式接口成员实现:
float IDimensions.Width()
{
return widthInches;
}
public static void Main()
{
// 声明一个类实例myBox:
Box myBox = new Box( 30 .0f, 20 .0f);
// 声明接口实例myDimensions:
IDimensions myDimensions =( IDimensions)myBox;
// 打印出方框尺寸:
/ * 以下注释行会产生编译
错误,因为它们试图从一个显式实现的
接口成员访问类实例:* /
// System.Console.WriteLine(长度:{0 },myBox.Length());
// System.Console.WriteLine( 宽度:{0},myBox.Width());
/ * 打印通过从接口实例调用方法
来输出框的尺寸:* /
System.Console.WriteLine( 加长h:{0},myDimensions.Length());
System.Console.WriteLine( 宽度:{0},myDimensions.Width( ));
}
}
解决方案是:
interface IXYZ
{
string 名称{设置跨度>; }
}
class 记录器:IXYZ
{
string IXYZ.Name { set {((IXYZ) this )。Name = 值跨度>; }
private void GenerateAutoName()
{
(( IXYZ) this )。Name = 名称跨度>; // 编译错误!
}
}
1)首先,如果它是显式接口实现,那么你必须在设置它时强制转换该成员,否则会出现编译错误。
((IXYZ)这个.Name 是正确的。
2)那么下一个问题是,你应该设置属性,不能保持原样;除非类是抽象的,所以将其设为 set {} 。
3)接口属性默认为public,删除名称的显式 public 修饰符。
4)你不能得到;如果未在界面中声明,请在您的班级中使用。
5)您可以参考IXYZ访问该属性,而不是使用Logger类参考。
请参阅下面的代码片段..
class 测试
{
public void TestMethod()
{
IXYZ xyzRef = < span class =code-keyword> new Logger();
xyzRef.Name = Hello; // 可访问名称
Logger logRef = new Logger();
// logRef.Name不可用...
}
}
希望它有所帮助。 :)
interface IXYZ
{
string Name { set; }
}
class Logger : IXYZ
{
public string IXYZ.Name { get; set; }
private void GenerateAutoName()
{
Name = "Name"; // compilation error!
}
}
The explanation can be found here[^]. This is the code sample that may help you. Note the comments in the Main method.
// explicit1.cs interface IDimensions { float Length(); float Width(); } class Box : IDimensions { float lengthInches; float widthInches; public Box(float length, float width) { lengthInches = length; widthInches = width; } // Explicit interface member implementation: float IDimensions.Length() { return lengthInches; } // Explicit interface member implementation: float IDimensions.Width() { return widthInches; } public static void Main() { // Declare a class instance "myBox": Box myBox = new Box(30.0f, 20.0f); // Declare an interface instance "myDimensions": IDimensions myDimensions = (IDimensions) myBox; // Print out the dimensions of the box: /* The following commented lines would produce compilation errors because they try to access an explicitly implemented interface member from a class instance: */ //System.Console.WriteLine("Length: {0}", myBox.Length()); //System.Console.WriteLine("Width: {0}", myBox.Width()); /* Print out the dimensions of the box by calling the methods from an instance of the interface: */ System.Console.WriteLine("Length: {0}", myDimensions.Length()); System.Console.WriteLine("Width: {0}", myDimensions.Width()); } }
Solution is:
interface IXYZ { string Name { set; } } class Logger : IXYZ { string IXYZ.Name { set { ((IXYZ) this).Name = value; }} private void GenerateAutoName() { ((IXYZ) this).Name = "Name"; // compilation error! } }
1)First, if it is explicit interface implementation, then you have to cast that member while setting it, where you were getting compilation error.
((IXYZ)this).Name is correct.
2)Then the next problem is, you should have set property implemented, can not keep it as set; unless class is abstract, so make it as set{}.
3) Interface properies are by default public, remove explicit public modifier of Name.
4) You can not have get; in your class if it is not declared in interface.
5) You will be able to access that property with reference of IXYZ and not with Logger class reference.
Please see below code snippet..
class Test { public void TestMethod() { IXYZ xyzRef = new Logger(); xyzRef.Name = "Hello"; //Name accessible Logger logRef = new Logger(); // logRef.Name Not available... } }
Hope it helps. :)
这篇关于为什么我使用explict接口实现时出现编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!