为什么C#这样设计?
据我所知,接口只描述行为,并且用于描述实现某些行为的接口的类的合同义务.
如果类希望在共享方法中实现该行为,为什么不应该这样做呢?
这是我想到的一个例子:
// These items will be displayed in a list on the screen.
public interface IListItem {
string ScreenName();
...
}
public class Animal: IListItem {
// All animals will be called "Animal".
public static string ScreenName() {
return "Animal";
}
....
}
public class Person: IListItem {
private string name;
// All persons will be called by their individual names.
public string ScreenName() {
return name;
}
....
}
Run Code Online (Sandbox Code Playgroud) 如何从扩展到接口的类实现方法?
我有这个界面:
public Interface myInterface
{
public static int myMethod();
}
Run Code Online (Sandbox Code Playgroud)
而这堂课:
public class MyClass : myInterface
{
// And I want here to implement the method form myInterface and i don't know how
}
Run Code Online (Sandbox Code Playgroud)