为什么它会解决一个名为shadowing / waht problert的概念 [英] Why there is a concept called shadowing/waht probleam it solves

查看:139
本文介绍了为什么它会解决一个名为shadowing / waht problert的概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么有一个名为shadowing / waht的概念,它解决了

Why there is a concept called shadowing/waht probleam it solves

推荐答案

它并没有真正解决任何问题,但它在特定情况下有所帮助。



阴影隐藏基类功能,但不会覆盖它。它只能从正确构建的类的具体实例中调用。

示例:

假设您有类Animal和方法eat()并提供一些基本实现。

你在大象,狗和美洲虎的类中继承了它,但在Amoeba类中暗影它。



现在,当你创建集合时动物和循环,并通过基类调用eat(),每个类使用overriden方法将调用自己的实现。



除了Amoeba不会覆盖,但阴影(隐藏)基本实现因此,在此集合中将调用eat()的实现,而不是Amoeba版本。



Amoeba.eat()也将保留它的功能在基础实现发生变化时 - 而其他类会相应改变。







通常不好主意,不是OO概念。



如果这有帮助,请花些时间接受解决方案。谢谢。
It doesn't really solve anything, but it helps in specific situations.

Shadows "hides" base class functionality, but does not override it. It can be called only from concrete instance of the class properly cast.
EXAMPLE:
Lets say you have a class Animal and method eat() and provide some base implementation.
You inherit it in the classes Elephant, Dog and Jaguar, but Shadow it in a class Amoeba.

Now, when you create collection of animals and loop and call eat() through base class, each class with an overriden method will call its own implementation.

Except Amoeba doesn't override, but shadows (hides) base implementation and thus, in this collection base implementation of eat() will be called, not Amoeba version.

Amoeba.eat() will also, retain its function when the base implementation changes - while other classes will change accordingly.



It is usually bad idea, not OO concept.

If this helps, please take time to accept the solution. Thank you.


Quote:

在阴影中,我们为基类成员提供了一个新的实现

而不覆盖它。我们可以使用关键字shadows来遮蔽派生类中的基类成员,使用



派生类成员的访问级别,返回类型和签名(表示

传递的参数的数据类型和类型的顺序) ,可能与基础不同

类。



在C#中,我们可能使用关键字实现
阴影新。但是,当隐藏在C#中时,访问

级别,派生类的签名,返回类型必须与基础

类相同。

In shadowing, we provide a new implementation to the base class member
without overriding it. We may shadow a base class member in a derived class, by
using the keyword shadows. The access level, return type, and the signature (means
the datatypes of the arguments passed & the order of the types) of the
derived class members which are shadowed, may differ from the base
class.

In C#, we may
achieve shadowing using the keyword new. However, when Hiding in C#, the access
level, the signature, return type of the derived class must be same as the base
class.


首先,让我们将'阴影/隐藏定义为编程语言中的功能,允许名称/标识符在程序的不同位置具有不同的含义/功能/值。



如果没有此功能,计算机语言(如BASIC或JavaScript / EcmaScript)会将所有变量(名称)定义为全局变量。经验表明,这些语言更容易出错并创建难以维护的代码。



'阴影/隐藏是计算机语言设施的一个方面构造范围,语义空间的控制。另一种说法是阴影/隐藏是现代编程语言常见的封装和模块化的一般策略的一部分。



在现代OOP语言中,阴影/隐藏继承中使用:派生类可以定义一个方法,该方法与它继承的基类中的方法具有相同的名称,但该方法可以扩展或替换基类方法的行为。



建议阅读:



ex-Microsoft C#guru Eric Lippert:[ ^ ]。



这里有很好的讨论,很多观点:[ ^ ]。
First, let's define 'shadowing/hiding as the functionality in a programming language that allows names/identifiers to have a different meaning/function/value in different places in your program.

Without this functionality, a computer language, like BASIC, or JavaScript/EcmaScript, defines all variables (names) as global. Such languages, experience has shown, are more prone to bugs and creating code that is difficult to maintain.

'Shadowing/hiding is one aspect of a computer language's facilities for structuring "scope," the control of semantic space. Another way to say that is that shadowing/hiding is part of the general strategy of encapsulation and modularity common to modern programming languages.

In modern OOP languages, shadowing/hiding is used in inheritance: a derived class can define a method with the same name as a method in the base class it inherits from, but that method can extend or replace the behavior of the base class method.

Suggested reading:

ex-Microsoft C# guru Eric Lippert: [^].

Good discussion here, many points of view: [^].


这篇关于为什么它会解决一个名为shadowing / waht problert的概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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