简体   繁体   English

铸造派生类为基础类

[英]Casting Deriving Class as Base Class

Suppose I have this: 假设我有这个:

using System;

public class Program
{
    public static void Main()
    {
        BaseClass bc = new DerivedClass();
        bc.Method1();
        bc.Method2();
        Console.WriteLine(bc.GetType().FullName);

        // Output
        // Derived - Method1 (override)
       // Base - Method2
       // DerivedClass
    }
}

public class BaseClass
{
    public virtual void Method1()
    {
        Console.WriteLine("Base - Method1");
    }

    public virtual void Method2()
    {
        Console.WriteLine("Base - Method2");
    }
}

public class DerivedClass : BaseClass
{
    public override void Method1()
    {
        Console.WriteLine("Derived - Method1 (override)");
    }

    public new void Method2()
    {
        Console.WriteLine("Derived - Method2 (new)");
    }
}

If the instance variable of the deriving class is cast to the base class and that instance variable is used to call the overridden methods, the method overridden with the override keyword will execute the implementation in the deriving class whereas the one overridden with the new keyword will execute the implementation in the base class. 如果派生类的实例变量被强制转换为基类,并且该实例变量用于调用重写的方法,则用override关键字覆盖的方法将在派生类中执行实现,而被new关键字覆盖的方法将执行在基类中执行实现。

How is the variable bc in the above example cast to the Base Class? 上例中的变量bc如何转换为基类?

I know that the new keyword will override the method implementation in the deriving class and it will be executed when an instance variable of the deriving class is used to call the overridden method, but I don't know what type of Conversion it is.. Doesn't seem to be Implicit nor Explicit, may be Type Conversion but I am confused by the syntax. 我知道new关键字将覆盖派生类中的方法实现,并且当派生类的实例变量用于调用重写的方法时将执行该关键字,但是我不知道它是什么类型的Conversion。似乎既不是隐式也不是显式,可能是类型转换,但是我对语法感到困惑。

Any explanation is appreciated. 任何解释表示赞赏。

I know that the new keyword will override the method implementation in the deriving class 我知道new关键字将覆盖派生类中的方法实现

No. It does not override the base class's method. 不会 。它不会覆盖基类的方法。 It declares a new , independent method, that's just named the same and has the same signature. 它声明了一个新的独立方法,该方法的名称相同且具有相同的签名。 The effect is it hide the same-signature method declared in the base class, effectively complicating the invocation of the same-signature method declared in the base class. 效果是它隐藏了在基类中声明的相同签名方法,从而有效地使在基类中声明的相同签名方法的调用复杂化。

In your example there are no 'type conversions' whatsoever. 在您的示例中,没有任何“类型转换”。 Think of a type cast as providing a specific view of the instance—exposing the specific part of the class's contract to the user. 可以将类型转换视为提供实例的特定视图-将类合同的特定部分暴露给用户。 It's nothing more, nothing less. 仅此而已。

Example: 例:

// instance of DerivedClass exposing its full contract via the 'dc' variable
DerivedClass dc = new DerivedClass();

// the same instance of DerivedClass exposing its contract limited to what's declared in BaseClass
BaseClass bc = dc;

// calling Method2 as newly declared in DerivedClass
dc.Method2();

// calling Method2 as declared in BaseClass—the following two lines are equivalent
bc.Method2();
((BaseClass)dc).Method2();

Practically speaking, there is no conversion. 实际上,没有转换。 There is only the way you look at the object. 您只能通过这种方式查看对象。

  • If you look at it with base-class-glasses, you will see all base-class-methods, which includes the overridden Method1 from the derived class, but it will not include the new Method2 from the derived class, so you will only see Method2 from the base class. 如果您使用基类眼镜进行查看,您将看到所有基类方法,其中包括派生类中重写的Method1,但不包括派生类中的新Method2,因此您只会看到来自基类的Method2。
  • If you look at it with derived-class-glasses, you will see all base-class-methods, which includes the overridden Method1 from the derived class, but it will now also include the new Method2 from the derived class. 如果你看它与派生类的眼镜,你会看到所有的基类的方法,其包括从派生类中重写的方法1,但它现在包括新的方法2,从派生类。 The original virtual Method1 from the base class, will not be visible. 基类的原始虚拟Method1将不可见。

How is the variable bc in the above example cast to the Base Class? 上例中的变量bc如何转换为基类?

It is an implicit cast which is performed when you assign your new DerivedClass instance to a variable of the BaseClass type: 当您将新的DerivedClass实例分配给BaseClass类型的变量时,将执行此隐式转换:

BaseClass bc = new DerivedClass();

Ahh I just found this , and I was wrong with saying that it didn't seem to be an implicit conversion... I must've over-read this. 啊,我刚刚找到了这个 ,我说那似乎不是一个隐式转换,这是我的错。

For reference types, an implicit conversion always exists from a class to any one of its direct or indirect base classes or interfaces. 对于引用类型,从类到其直接或间接基类或接口中的任何一个始终存在隐式转换。 No special syntax is necessary because a derived class always contains all the members of a base class. 不需要特殊的语法,因为派生类始终包含基类的所有成员。

Derived d = new Derived();
Base b = d; // Always OK.

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

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