gpt4 book ai didi

c# - 将派生类的对象存储为基类变量。这个的 OOP 术语是什么?

转载 作者:行者123 更新时间:2023-11-30 19:29:49 25 4
gpt4 key购买 nike

在 C# 中,我们经常使用这样的初始化语法,即基类的实例使用其继承的子类进行初始化:

Control BTN = new Button();

现在我的问题就是,这种初始化的正式名称是什么?

最佳答案

根据 C# 规范,它被称为带有隐式引用转换简单赋值

7.16.1 Simple assignment

In a simple assignment, the right operand must be an expression of a type that is implicitly convertible to the type of the left operand.

...

The run-time processing of a simple assignment of the form x = y consists of the following steps:

  • ...
  • y is evaluated and, if required, converted to the type of x through an implicit conversion (§6.1).

6.1.6 Implicit reference conversions

The implicit reference conversions are:

  • ...
  • From any class-type S to any class-type T, provided S is derived from T.

关于c# - 将派生类的对象存储为基类变量。这个的 OOP 术语是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10963070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com