多态类的使用 [英] use of polymorphic classes
本文介绍了多态类的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当您可以设计和使用多态类时,请问一个或两个实例,并解释为什么我们以此方式设计它们吗?
Could u please name one or two instances when you can design and use polymorphic classes and explain why we designed them in that manner?
推荐答案
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.
这篇关于多态类的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文