简体   繁体   English

派生自实现接口的基础 class

[英]Derived from a base class that implements an interface

I get from Effective C# Item 23 that the following code should print: MyDerivedMessage我从Effective C#项目 23 中得到以下代码应该打印: MyDerivedMessage

namespace ConsoleApplication1
{

    interface IMsg
    {
        void message();
    }

    public class MyMessage : IMsg
    {
        public void message()
        {
            Console.WriteLine("MyMessage");
        }
    }

    public class MyDerivedMessage : MyMessage
    {
        public new void message()
        {
            Console.WriteLine("MyDerivedMessage");
        }
    }

    class Test
    {

        static void Main()
        {
            MyDerivedMessage mdm = new MyDerivedMessage();
            IMsg im = mdm as IMsg;
            im.message();
        }
    }
}

Book:书:

public class MyDerivedClass : MyClass
{
    public new void Message()
    {
        Console.WriteLine("MyDerivedClass");
    }
}

The addition of the IMsg keyword changes the behavior of your derived class so that IMsg.Message() now uses the derived class version:添加 IMsg 关键字会更改派生的 class 的行为,因此 IMsg.Message() 现在使用派生的 class 版本:

MyDerivedClass d = new MyDerivedClass();
d.Message(); // prints "MyDerivedClass".
IMsg m = d as IMsg;
m.Message(); // prints " MyDerivedClass "

How come I still get MyMessage printed after I add "new" to MyDerivedMessage::message() ?在将“new”添加到MyDerivedMessage::message()之后,为什么我仍然会打印MyMessage

You are hiding (or shadowing) the method rather than overriding it.您正在隐藏(或隐藏)该方法,而不是覆盖它。 This means that you will only call the method on the derived class when you access it from a variable declared to be that class (or more derived).这意味着当您从声明为 class(或更多派生)的变量访问它时,您只会调用派生 class 上的方法。

Note that this is the default on non-virtual methods, so adding "new" is just being more explicit.请注意,这是非虚拟方法的默认设置,因此添加“新”只是更加明确。

If you don't want this behavior, you need to make the message method virtual in your base class, and override it in your derived class.如果您不希望这种行为,您需要在您的基础 class 中使message方法虚拟,并在派生的 class 中覆盖它。

See also the documentation on new Modifier另请参阅有关new Modifier的文档

It seems like maybe the book has some errata, but your MyDerivedMessage class would also need to implement IMsg.看起来这本书可能有一些勘误表,但您的 MyDerivedMessage class 也需要实现 IMsg。 If this does not occur, then the cast to IMsg will only see MyMessage as having the Message() method when im.message() is called because of the chain created between the Interface and MyDerivedMessage.如果这没有发生,那么由于接口和 MyDerivedMessage 之间创建的链,当调用 im.message() 时,转换为 IMsg 只会看到 MyMessage 具有 Message() 方法。

MyDerivedMessage is-a MyMessage MyMessage is-a IMsg MyDerivedMessage NOT is-a IMsg MyDerivedMessage is-a MyMessage MyMessage is-a IMsg MyDerivedMessage NOT is-a IMsg

public class MyDerivedMessage : MyMessage, IMsg
{ 
    public new void message() 
    { 
        Console.WriteLine("MyDerivedMessage"); 
    } 
}

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

相关问题 当基类实现abtract时,在派生类上指定实现接口 - Specify implement interface on derived class when base class implements it abtract 具有从基类派生的其他属性的接口 - Interface With additional property derived from base class 实现接口的通用基类 - generic base class that implements interface 从显式实现接口的基类重写方法 - Override a method from a base class that explicitly implements an interface 如何创建从实现接口的基类派生的实例列表? - How to create list of instances deriving from a base class that implements interface? 在基类和派生类中实现接口 - Implementing an interface in a base and derived class 实现一个泛型类,其中定义在基本接口类型上,但实现在从该基本接口派生的接口上? - Implement a generic class where the definition is on a base interface type but the implementation is on an interface derived from that base? 从派生类到基类 - from derived class to base class 检查“ this”是否实现了接口,然后从通用基类内部为子类调用其成员? - Check if 'this' implements an interface then call its member from inside generic base class for subclass? 如何处理实现接口并具有基类的类? - How to approach a class which implements an interface and has a base class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM