简体   繁体   English

C#接口和类继承

[英]C# interface and class inheritance

I have a problem with using interface methods in my object.我在对象中使用接口方法时遇到问题。 I'l giva a quick example without all the inplementations.我将举一个没有所有实现的简单示例。

public class Item{}
public interface IFruit
{
      void MethodExample();
}

public class Apple : Item, IFruit
{
    public void IFruit.MethodExample(){}
}

// put this in a run method somewhere
var example_item = new Apple();

//Here comes the problem.
example_item.MethodExample();
// this will return an error saying that it cant find the method.

Anyway to do this?无论如何要做到这一点? I know for a fact that it implements the i_fruit.我知道它实现了 i_fruit 的事实。 And has the method.并且有方法。 Yet i cant access it?然而我无法访问它?

First of all, please read c# naming conventions.首先,请阅读 c# 命名约定。 Secondly, you have implemented i_fruit interface explicitly, you should cast your example_item into i_fruit or more common way is to implement i_fruit interface implicitly.其次,您已经显式实现了i_fruit接口,您应该将example_item转换为i_fruit或更常见的方法是隐式实现i_fruit接口。 Please read: https://blogs.msdn.microsoft.com/mhop/2006/12/13/implicit-and-explicit-interface-implementations/请阅读: https : //blogs.msdn.microsoft.com/mhop/2006/12/13/implicit-and-explicit-interface-implementations/

Implicit implementation example:隐式实现示例:

public class Apple : Item, IFruit
{
   public MethodExample(){}
}

On the other hand if you want to stick to explicit implementation, then you should change your code into this:另一方面,如果您想坚持显式实现,那么您应该将代码更改为:

IFruit example_item;
example_item = new Apple();

the syntax in your provided example does not exactly look like C#, but here is a simple example, that looks similar to yours.您提供的示例中的语法并不完全像 C#,但这里有一个简单的示例,它看起来与您的相似。 The class Item does not have an ExampleMethod, but Apple does, because it implements the interface IFruit. Item 类没有 ExampleMethod,但 Apple 有,因为它实现了 IFruit 接口。 You can, however, use the as keyword to cast the object into something else temporarily, and thereby get access to the ExampleMethod.但是,您可以使用as关键字将对象临时转换为其他内容,从而访问 ExampleMethod。 A common way to deal with this type of situation is seen in the example with exampleFruit .exampleFruit的示例中可以看到处理此类情况的常用方法。 Hope this helps.希望这可以帮助。

using System;

namespace StackOverflowInterfaces
{
    class Item { }
    interface IFruit
    {
        void ExampleMethod();
    }

    class Apple : Item, IFruit
    {
        public void ExampleMethod()
        {
            throw new NotImplementedException();
        }
    }

    class MainClass
    {
        public static void Main()
        {
            Item exampleItem = new Apple();
            // exampleItem.ExampleMethod(); -- DOES NOT WORK, because Item does not implement IFruit
            (exampleItem as IFruit).ExampleMethod();
            (exampleItem as Apple).ExampleMethod();

            IFruit exampleFruit = new Apple();
            exampleFruit.ExampleMethod();

            Apple exampleApple = new Apple();
            exampleApple.ExampleMethod();

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM