接口sntax和从main调用 [英] interfaces sntax and calling from main

查看:62
本文介绍了接口sntax和从main调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

告诉我如何从main调用接口...是否可以通过使用对象调用接口,如调用类....给我简单的示例,这很容易理解???

tell me how interface can b called from main...... is it possible to call interface like calling classes by using objects.... give me simple exampless which are easy to understand???

推荐答案

接口是类的视图,类型。



你告诉编译器一个类实现了接口。然后,编译器将监视该类是否实现了属于该接口的所有方法。如果缺少一个方法或签名错误,编译器将不接受它。



所以你确定你可以使用你的类,就好像它是一个实例它实现的接口(即使接口无法实例化,实现它的类也可以)。
An interface is a view to a class, sort of.

You tell the compiler that a class implements an interface. Compiler will then monitor that the class implements all methods that belong into that interface. If one method is missing or with wrong signature, compiler will not accept it.

So you're sure you can use your class as if it was an instance of the interface it implements (even though interfaces cannot be instantiated, classes that implement it can).
interface IExampleInterface
{
    public string TellMeSomething();
}


public class ExampleClass : IExampleInterface
{
    public string TellMeSomething()
    {
        return("That's Something!");
    }
}


int Main(whatever)
{
    IExampleInterface exampleInstance = new ExampleClass();
    string result = exampleInstance.TellMeSomething();
}


这篇关于接口sntax和从main调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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