简体   繁体   English

C#中的接口实现和继承

[英]Interface implementation and inheritance in C#

I have some code to implement interface in C# 我有一些代码可以在C#中实现接口

public interface Intfc { void xyz();}

public class BaseClass : Intfc
{

    public virtual void xyz()
    {
        Console.WriteLine("In Base Class");
    }
}

public class Derived : BaseClass
{
    public override void xyz()
    {
        Console.WriteLine("In Derived Class");
    }
}

static void Main(string[] args)
    {
        Derived mc = new Derived();
        mc.xyz(); //In Derived Class
        ((BaseClass)mc).xyz(); //In Base Class
        ((Intfc)mc).xyz(); //In Derived Class

        Console.ReadKey();

    }

I need the console output as provided in comments in Main() . 我需要Main()注释中提供的控制台输出。 Actual results are 实际结果是

In Derived Class
In Derived Class
In Derived Class

How can I achieve the desired results. 我怎样才能达到预期的效果。

You need to overwrite the method on the Derived class instead of using the virtual / override operators use the new operator. 您需要覆盖Derived类上的方法,而不是使用virtual / override运算符使用new运算符。

http://msdn.microsoft.com/en-us/library/51y09td4(VS.71).aspx#vclrfnew_newmodifier http://msdn.microsoft.com/en-us/library/51y09td4(VS.71).aspx#vclrfnew_newmodifier

try 尝试

public interface Intfc { void xyz();}

public class BaseClass : Intfc
{
    public void xyz()
    {
        Console.WriteLine("In Base Class");
    }
}

public class Derived : BaseClass
{
    new public void xyz()
    {
        Console.WriteLine("In Derived Class");
    }
}

static void Main(string[] args)
{
    Derived mc = new Derived();
    mc.xyz(); //In Derived Class
    ((BaseClass)mc).xyz(); //In Base Class
    ((Intfc)mc).xyz(); //In Derived Class

    Console.ReadKey();

}

Don't use virtual methods with the override modifier, but reimplement the interface on the Derived class. 不要将virtual方法与override修饰符一起使用,而是在Derived类上重新实现接口。

The code below exhibits the required behaviour, but I'd consider this approach slightly confusing and opaque to end users. 下面的代码展示了必需的行为,但是我认为这种方法对于最终用户而言有些混乱和不透明。

public interface Intfc { void xyz();}

public class BaseClass : Intfc
{

    public void xyz()
    {
        Console.WriteLine("In Base Class");
    }
}

public class Derived : BaseClass,Intfc
{
    public void xyz()
    {
        Console.WriteLine("In Derived Class");
    }
}

static void Main()
{
    Derived mc = new Derived();
    mc.xyz(); //In Derived Class
    ((BaseClass)mc).xyz(); //In Base Class
    ((Intfc)mc).xyz(); //In Derived Class



}

You can't do this, mc is of type Derived and it will always call the xyz in this class. 您无法执行此操作, mcDerived类型的,它将始终在此类中调用xyz It's how inheritance works. 这就是继承的原理。 You need an instance of BaseClass: 您需要一个BaseClass实例:

var bc = new BaseClass();
bc.xyz(); // In Base Class

You will need a var b = new BaseClass() . 您将需要一个var b = new BaseClass() Then b.xyz() instead of ((BaseClass)mc).xyz() . 然后b.xyz()代替((BaseClass)mc).xyz()

You are miss understanding the behavior of inherit. 您很想了解继承的行为。

When You create a object of given type that override some virtual method You will alway get result from that override method. 当您创建覆盖某个虚拟方法的给定类型的对象时,您总是会从该覆盖方法中获得结果。 The casting do not change the object structure, once create object can change his state after cast operation. 强制转换不会更改对象的结构,一旦创建对象即可在强制转换操作后更改其状态。

static void Main(string[] args)
    {
        Intfc mc1 = new Derived();
        Intfc mc2 = new BaseClass();

          mc1.xyz();
          mc2.xyz();

        Console.ReadKey();

    }

This will product the 这将产生

In Base Class
In Derived Class

But because there is always a but. 但是因为总有一个but。

If You overwrite that method You will get the result what You are up to, to overwrite method just use the operator new 如果您覆盖该方法,则将得到您想要的结果,要覆盖该方法,只需使用运算符new

public class Derived : BaseClass
{
    new public void xyz()
    {
        Console.WriteLine("In Derived Class");
    }
}

public class BaseClass : Intfc
{

    public void xyz()
    {
        Console.WriteLine("In Base Class");
    }
}

This will use new implementation for DeriveClass and old one for BaseClass. 这将对DeriveClass使用新的实现,对BaseClass使用旧的实现。

Links: 链接:

What override is ? 什么是覆写?

Modifier new in method signature 方法签名中的修饰符新功能

What modifier new is ? 什么是新修饰词?

When you added the modifier override to the inherited virtual method xyz in mc , you simply overrode the the inherited virtual method in the instance referred to by mc. 在mc中将修饰符override添加到继承的虚拟方法xyz时,您只需在mc引用的实例中覆盖继承的虚拟方法。 Its as if you erased the Base.xyz and written Derived.xyz in its place in memory of the mc instance. 就像您擦除了Base.xyz并将Derived.xyz写入mc实例的内存中一样。 So the instance referred to by mc has no record of the virtual xyz in its implementation and therefore no way of getting to it through the mc referrence. 因此,由mc引用的实例在其实现中没有虚拟xyz的记录,因此无法通过mc引用获取该实例。 The only way to access the base's virtual method is through an instance of the base as demonstrated below: 访问基础虚拟方法的唯一方法是通过基础实例,如下所示:

 var b = new BaseClass();    
 b.xyz();//  instead of ((BaseClass)mc).xyz()

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

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