C#中的数据绑定概念 [英] Data binding concept in C#

查看:352
本文介绍了C#中的数据绑定概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A级

{

public virtual void WhoAreYou(){Console.WriteLine(我是A); }

}

B级:A

{

公共覆盖无效WhoAreYou(){Console.WriteLine( 我是B); }

}

class C:B

{

public new virtual void WhoAreYou(){Console.WriteLine (我是C); }

}

D类:C

{

公共覆盖无效WhoAreYou(){Console.WriteLine( 我是D); }

}





C c = new D();

c.WhoAreYou(); //我是D

A a =新D();

a.WhoAreYou(); //我是一个B!!!!

任何人都可以解释一下怎么回事?

class A
{
public virtual void WhoAreYou() { Console.WriteLine("I am an A"); }
}
class B : A
{
public override void WhoAreYou() { Console.WriteLine("I am a B"); }
}
class C : B
{
public new virtual void WhoAreYou() { Console.WriteLine("I am a C"); }
}
class D : C
{
public override void WhoAreYou() { Console.WriteLine("I am a D"); }
}


C c = new D();
c.WhoAreYou();// "I am a D"
A a = new D();
a.WhoAreYou();// "I am a B" !!!!
Can any one explain Whats going On?

推荐答案

正在发生的事情被称为最基本的想法 OOP,没有OOP,OOP不是真正的OOP,而只是一个毫无意义的名字。 (对于我们的许多询问者来说,OOP无论如何都是毫无意义的名字。:-()



当你创建一个虚拟函数(可能是抽象的)时,你创建了一个槽在虚拟方法表(VMT)中。如果您不知道VMT是什么以及它是如何工作的,您应该停止阅读这个答案并停止进行任何编程,否则继续使用其中任何一个都会浪费时间这些方法是通过VMT间接调用的,所以当你覆盖一个方法时,VMT中的插槽将替换为新版本的方法。该方法根据运行时类型调用,而不是一个对象的设计时类型(声明类型)。如果你不理解运行时和设计时类型之间的区别......见上文。这就是为什么在第一个 WhoAreYou 打电话给你我是D。如果声明的类型是 B 而不是 C 。



现在,到目前为止,一切似乎都很清楚。直到你宣布身份tical对象 a 具有相同的运行时类型 D as A 。在这里,您需要了解您有两个完全不相关的VMT插槽。让我们看看:

What's going on is called "the most fundamental idea of OOP, without which OOP is a not really OOP but just a meaningless name". (For many our inquirers, OOP is a meaningless name anyway. :-()

When you create a virtual function (which could be abstract), you create a slot in Virtual Method Table (VMT). If you don't know how what is VMT and how it works, you should stop reading this answer and also stop doing any programming, as otherwise continuing with either of the two would be a waste of time. The methods are called via VMT indirectly, so when you override a method, the slot in VMT is replaced with a new version of the method. The method is called according to the run-time type, not a design-time type (declared type) of an object. If you don't understand the distinction between run-time and design-time types… see above. That's why in first WhoAreYou call you have "I am a D". It would be the same if the declared type was B instead of C.

Now, so far, everything seems to be clear. Until you declare the identical object a with the same run-time type D as A. Here you need to understand that you have two totally unrelated VMT slots. Let's see:
"WhoAreYou"-slot 1: A.WhoAreYou —> B.WhoAreYou
"WhoAreYou"-slot 2: C.WhoAreYou —> D.WhoAreYou





他们是无关的,只有相同的名字。发生这种情况是因为在 C 中你没有覆盖slot1-WhoAreYou 并带有覆盖,但重新引入同名的全新方法,同样是虚拟方法。这两个名称针对两个不同的无关插槽 互相隐藏 。如果您的编译时声明是 A B ,则隐含插槽1,如果 C D - 插槽2.



第二种情况,slot1-WhoAreYou 为您提供 A.WhoAreYou 的最新重写版本,该版本由 B定义。 WhoAreYou ,因此我是B。



作为一名有意识的开发人员,您需要了解虚拟方法的工作原理。换句话说,永远记住你是谁。 :-)



这有助于理解OOP级别的内容:

http://en.wikipedia.org/wiki/Dynamic_dispatch [ ^ ],

http://en.wikipedia。 org / wiki / Virtual_table [ ^ ]。



-SA



They are unrelated, only have the same name. This happened because in C you did not override "slot1"-WhoAreYou with "override", but reintroduced a totally new method under the same name, again, a virtual one. These two names addressing two different unrelated slots hide each other. If your compile-time declaration is A or B, slot 1 is implied, if C or D — slot 2.

In second case, "slot1"-WhoAreYou gives you the latest overridden version of A.WhoAreYou, which was defined by B.WhoAreYou, hence "I am a B".

You need to learn how virtual methods work very deeply, as a conscious developer. In other words, always remember who are you. :-)

This can help to understand things on the OOP level:
http://en.wikipedia.org/wiki/Dynamic_dispatch[^],
http://en.wikipedia.org/wiki/Virtual_table[^].

—SA


这篇关于C#中的数据绑定概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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