当一个对象被强制转换为基类时,它如何记住它的真正含义? [英] When an object is cast to a base class, how does it remember what it really is?

查看:64
本文介绍了当一个对象被强制转换为基类时,它如何记住它的真正含义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个初学者的问题,但是我对了解这里发生的事情很感兴趣。我的问题是,当您向下投射物体时,幕后发生了什么?它是否维护有关原始内容的某种元数据?这是我的意思:

This is a beginner's question, but I am interested in learning what's going on here. My question is, what goes on behind the scenes when you down-cast an object? Does it maintain some sort of metadata about what it originally was? Here's what I mean:

假设我有一个名为 ClockIn的方法,该方法接受类型为 Employee的参数:

Suppose I have a method called "ClockIn" that accepts a parameter of type "Employee":

public static void ClockIn(Employee employee)
{
   var manager = employee as Manager;
   if (manager != null)
   {
      manager.OpenSafe();
   }
}

因此,假设Manager是Employee的子类类型,并且具有 OpenSafe方法:

So, assume that Manager is a subclass of the Employee type and that it has the "OpenSafe" method:

public class Manager : Employee 
{
   public void OpenSafe()
   { 
      ... 
   }
}

如果 ClockIn方法发现已传入Manager,则调用OpenSafe方法。例如:

The "ClockIn" method, if it finds that a Manager has been passed in, calls the OpenSafe method. Such as:

var bob = new Manager();
ClockIn(bob);

在这里,我将类型为Manager的实例传递给接受基类Employee的方法。在调用OpenSafe之前,我需要将ClockIn方法中的实例强制转换为Manager。

Here, I've passed in an instance of type Manager into a method that accepts the base class Employee. I need to cast the instance inside the ClockIn method to Manager before I can call OpenSafe.

问题是,即使我已将鲍勃(Bob)任命为雇员,是否存在一些元数据可以记住鲍勃是经理?代码如何知道他确实可以被任命为经理?

The question is, is there some metadata that remembers that "bob" is a Manager, even though I've passed him in as an Employee? How does the code know that he can indeed be cast to a Manager? Is there something going on in the heap?

推荐答案

要记住的第一件事是强制转换不是 >完全更改原始对象。它仅通过该特定引用更改对象的视图。可以有多个引用指向同一个对象,因此更改对象不是强制类型转换。

The first thing to remember is that casting does not change the original object at all. It only changes your view of the object through that particular reference. More than one reference can be pointing to the same object, so changing the object isn't a reasonable thing to do on a cast.

您可能在实例中执行的操作是将 ClockIn()用作 Employee 类的方法。然后,当您调用

What you might do in your instance is to make ClockIn() a method of the Employee class. Then, when you call

bob.ClockIn();

然后 bob 会知道他确实,然后为其类型调用相应的 ClockIn()方法。这称为 动态方法分派 ,不适用于静态功能与您的示例相同。

then bob will know what type he really is, and call the appropriate ClockIn() method for his type. This is called dynamic method dispatch and is not available for static functions as in your example.

例如:

public class Employee {
    public void ClockIn() {
        ....
    }
}

public class Manager: Employee {
    public void ClockIn() {
        // first, do what all Employees do when clocking in
        Employee.ClockIn();
        // Next, do Manager specific actions
        OpenSafe();
    }
    public void OpenSafe() {
        ....
    }
}

这篇关于当一个对象被强制转换为基类时,它如何记住它的真正含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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