為什么C#不允許靜態(tài)方法實(shí)現(xiàn)接口?為什么C#是這樣設(shè)計(jì)的?據(jù)我所知,接口只描述行為,并用于描述實(shí)現(xiàn)某些行為的類的契約義務(wù)。如果類希望在共享方法中實(shí)現(xiàn)該行為,那么它們?yōu)槭裁床粦?yīng)該實(shí)現(xiàn)呢?下面是我所想到的一個(gè)例子:// 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;
}
....
}
為什么C#不允許靜態(tài)方法實(shí)現(xiàn)接口?
慕田峪7331174
2019-07-10 10:19:59