[英]Why are interfaces turning into abstract classes c#
老實說,我在這里尋找很好的例子來說明使用接口而不是抽象的原因。 我能看到的主要原因是它們是供課程遵循的不拘一格的公共藍圖。 但是隨着他們添加默認實現,情況就不再如此了。 區分接口和抽象類變得越來越困難。 在 c# 11 中使用抽象 class 接口的真正意義是什么。它只是一個較慢的抽象 class。
甚至我都覺得抽象類和接口太相似了。 我更喜歡使用抽象類,因為可以定義它的方法,直到並且除非使用了多個 inheritance 的概念。
與抽象類的情況一樣,我們不能從多個抽象類繼承,而對於接口,您可以繼承任意數量的接口。
我將通過一段代碼讓您了解這一點。
interface IFirstInterface
{
void myMethod();
}
interface ISecondInterface
{
void myOtherMethod();
}
class Democlass : IFirstInterface, ISecondInterface
{
public void myMethod()
{
System.Console.WriteLine("Some text..");
}
public void myOtherMethod()
{
System.Console.WriteLine("Some other text..");
}
}
class Program
{
static void Main(string[] args)
{
Democlass myObj = new Democlass();
myObj.myMethod();
myObj.myOtherMethod();
}
}
讓我知道你是否覺得這不清楚。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.