且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

多态类的使用

更新时间:2022-06-05 04:34:02

http://msdn.microsoft.com/en-us/library/ms173152%28v= vs.80%29.aspx [ ^ ]


检查以下内容:

http://msdn.microsoft.com/en-us/library/ms173152.aspx [ ^ ]

C#中的继承简介,多态性 [
check these out:

http://msdn.microsoft.com/en-us/library/ms173152.aspx[^]

Introduction to inheritance, polymorphism in C#[^]

hope this helps :)


几乎所有C#应用程序都使用多态类,无论它是否显而易见.
例如,一个简单的类,例如字符串 [
Almost any C# App uses polymorphic classes, whether it is obvious or not.
For example, a simple class like string[^] is polymorphic: you have a variety of ways in which you can construct one: From a char pointer (unsafe pointer) or from an array of chars for example (among 8 constructors in total) and many of it''s methods are also polymorphic.

Why? Because it make it easier to get it right. In the bad old days, in order to build a string in eight different ways, we would need to create eight different methods, all with different names:
CreateStringFromCharPointer, CreateStringFromCharArray, CreateStringFromSByteArray, and so forth - only we would probably have used shorter, less intuitively obvious names: CStrChrP, CStrChrA, CStrSBA, and so on. Then we had to remember what acronym went with with datatype, and the names got really nasty if we had 5 or 6 parameters... The potential for errors was enormous - and they might not be spotted until run time, if teh types were similar enough to get past the compiler, or a quick cast to fit what we thought the name meant!

Polymorphism means that the calling mechanism is similar - it has a uniform interface - and this improves the ease of writing, maintainability, and reliability of our code.