如何在抽象类和接口中调用具有相同名称的方法。 [英] How to call the method which is having same name in abstract class and interface.

查看:85
本文介绍了如何在抽象类和接口中调用具有相同名称的方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调用接口的MyFunction。我该怎么做?

我还想知道MyInterface.MyFunction()做什么吗?



I want to call MyFunction of interface. How will i do that ?
Also i want to know what MyInterface.MyFunction() do ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication2
{
    interface MyInterface
    {
        void MyFunction();
    }

    abstract public class MyAbstractClass
    {
        abstract public void MyFunction();
    }

    public class MyClass : MyAbstractClass, MyInterface
    {
        public override void MyFunction()
        {
            Console.WriteLine("A");
        }

        void MyInterface.MyFunction()
        {
            Console.WriteLine("B");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyClass mc = new MyClass();
            mc.MyFunction();

        }
    }
}

推荐答案

只能调用明确实现的成员使用接口的实例。它们不构成Class的API的一部分。



Explicitly implemented members can only be called with an instance of the interface. They do not form part of the Class's API.

MyInterface i = new MyClass();
i.MyFunction();


这篇关于如何在抽象类和接口中调用具有相同名称的方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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