我還沒有看到接口實現爲類成員之前。有人可以解釋發生了什麼嗎?你允許實例化一個接口嗎?這些接口所需的方法在哪裏實現?爲什麼這種接口在課堂上以這種方式實現?
public class MyClass
{
private readonly ITest1 interface1;
private readonly ITest2 interface2;
private readonly ITest3 interface3;
public MyClass(ITest1 interface1, ITest2 interface2, ITest3 interface3)
{
this.interface1 = interface1;
this.interface2 = interface2;
this.interface3 = interface3;
}
public void TestMethod()
{
var lines = interface1.GetData();
var file = interface2.Parse(lines);
interface3.Copy(file);
}
}
什麼是比我有常用的接口方式的不同:
public class Person : IEquatable<Dog>
{
public int Age { get; set; }
public bool Equals(Dog d)
{
if (d.Age == this.Age)
return true;
else
return false;
}
}
public class Dog
{
public int Age { get; set; }
}
無意冒犯,但我覺得你真的需要去通過C#的一些教程運行,具體怎麼接口工作。 – DavidG
我還沒找到解釋第一個例子的東西。我發現的所有東西都與第二個例子類似。 – eek142
他們沒有實例化接口,他們使用實例(像往常一樣),但它響應給定的接口 - 這意味着他們實現了它定義的方法 - 但實現不在接口 –