摘要和界面C# [英] Abstract and interface C#

查看:100
本文介绍了摘要和界面C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下工作?

公共抽象类AbsClass

{

public void ShowData(){Console.WriteLine(some some text );}

}



公共接口IInterface

{

public void ShowData();

}

class otherclassName:AbsClass,IInterface

{

Static Void Main()

{

}

}



以上课程我没有实施接口,但它没有编译或运行时错误。



亲爱的c#techies,请你解释一下。我在这里缺少什么。



我尝试过:



i已尝试过相同的解释,但我不明白基本。

请解释。

why following is working?
public abstract class AbsClass
{
public void ShowData(){Console.WriteLine("some text");}
}

public interface IInterface
{
public void ShowData();
}
class otherclassName :AbsClass,IInterface
{
Static Void Main()
{
}
}

above class i have not implemented the interface ,but it is working without compile or Runtime error.

My dear c# techies, could you please explain. what i'm missing here.

What I have tried:

i have tried the same thing which has explained,but im not understand the basic.
please explain.

推荐答案

假设你纠正了汇编:

Assuming you correct that so it compiles:
public abstract class AbsClass
    {
    public void ShowData() { Console.WriteLine("some text"); }
    }
public interface IInterface
    {
    void ShowData();
    }
class otherclassName : AbsClass, IInterface
    {
    static void Main()
        {
        }
    }





无关紧要:通过抽象类定义,在类中有一个带有所需签名的方法的实现。这足以让界面快乐。

如果这是一个问题,你可以明确地为界面声明一个方法:



It doesn't matter: there is an implementation of a method with the required signature in the class via the abstract class definition. That is enough for the interface to be happy.
If it's a problem, you can explicitly declare a method for the Interface:

class otherclassName : AbsClass, IInterface
    {
    void IInterface.ShowData()
        {
        ...
        }
    ...
    }

系统将根据您的需要为您排序你从中调用类的变量类型。

And the system will sort it out for you based on the type of variable you call the class from.


请参阅本文中引用的过去的答案:为什么我们需要为接口创建实例而不是实现?



-SA
Please see my past answers referenced in this one: Why we need to create instance for interface not for implementation?.

—SA


这篇关于摘要和界面C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆