问题描述
嗨
朋友,
任何机构都会帮助我如何在不重载的情况下创建多态覆盖C#.net
感谢你
Renuka Ruke
Hi
Friends,
Any body will help me how to create polymorphism without overloading and overriding in C#.net
Thanking you
Renuka Ruke
推荐答案
interface IOnePolymorphicSetMember {
void SomeFeature();
} //IOnePolymorphicSetMember
interface IAnotherPolymorphicSetMember {
void SomeOtherFeature();
} //IAnotherPolymorphicSetMember
class OnePolymorphicSet {
internal void Add(IOnePolymorphicSetMember element) {
list.Add(element);
} //Add
internal void ShowAll() {
foreach (IOnePolymorphicSetMember element in list)
element.SomeFeature();
} //ShowAll
System.Collections.Generic.IList<IOnePolymorphicSetMember> list =
new System.Collections.Generic.List<IOnePolymorphicSetMember>();
} //class OnePolymorphicSet
class AnotherPolymorphicSet {
internal void Add(IAnotherPolymorphicSetMember element) {
list.Add(element);
} //Add
internal void ShowAll() {
foreach (IAnotherPolymorphicSetMember element in list)
element.SomeOtherFeature();
} //ShowAll
System.Collections.Generic.IList<IAnotherPolymorphicSetMember> list =
new System.Collections.Generic.List<IAnotherPolymorphicSetMember>();
} //class AnotherPolymorphicSet
它可能似乎两个接口和两个类太相似而且缺少一些代码重用,所以可以使用泛型,但事实并非如此:我只是为了演示而设置了类似的代码。在现实生活中,界面非常不同,方法有不同的签名,因此不能再应用抽象。
现在,我们可以演示一些很棒的功能。首先,元素可以是类,也可以不是。然而,作为值类型的结构也可以实现接口并添加到多态集/容器中。方法如下:
It may seem that two interfaces and two classes are well too similar and some code reuse is missing, so generics could be used, but this is not so: I put so similar code just for demonstration. In reality life, interfaces are very different and method have different signatures, so no further abstraction can be applied.
Now, we can demonstrate a couple of wonderful features. First, the elements could be classes or not. Structures, being value types, nevertheless, also can implement interfaces and be added to the polymorphic sets/containers. Here is how:
class OnePolymorphicElement : IOnePolymorphicSetMember {
void IOnePolymorphicSetMember.SomeFeature() {
System.Console.WriteLine("{0}: {1}", this.GetType().FullName, this);
} //IOnePolymorphicSetMember.SomeFeature
//...
} //class OnePolymorphicElement
struct AnotherPolymorphicElement : IAnotherPolymorphicSetMember {
void IAnotherPolymorphicSetMember.SomeOtherFeature() {
System.Console.WriteLine("{0}: {1} ({2})", this.GetType().Name, this.Name, this.Id);
} //IOnePolymorphicSetMember.SomeFeature
internal string Name { get; set; }
internal string Id { get; set; }
//...
} //struct AnotherPolymorphicElement
另一个很棒的特性是实现接口的类和结构可以通过它们的继承关系相关联,或者以任何其他方式相互关联,并且基本上,相同的对象可以参与不同的,可能不相关的多态集。方法如下:
And another wonderful feature is that classes and structures implementing interfaces can be related by they inheritance relationships or not, related to each other in any other way or not, and, essentially, the same object can participate in different, possible unrelated polymorphous sets. Here is how:
struct CombinedPolymorphicElement : IOnePolymorphicSetMember, IAnotherPolymorphicSetMember {
void IOnePolymorphicSetMember.SomeFeature() {
System.Console.WriteLine("{0}: {1}", this.GetType().FullName, this);
} //IOnePolymorphicSetMember.SomeFeature
void IAnotherPolymorphicSetMember.SomeOtherFeature() {
System.Console.WriteLine("{0}: {1} ({2})", this.GetType().Name, this.Name, this.Id);
} //IOnePolymorphicSetMember.SomeFeature
internal string Name { get; set; }
internal string Id { get; set; }
//...
} //struct CombinedPolymorphicElement
定义的结构实例根据我们的代码示例,下面可以同时添加到两个不同的集合/容器中, OnePolymorphicSet.Add
和 AnotherPolymorphicSet.Add
。
请注意,我们也有强制抽象级别:不仅该集合不可知到包含对象的具体运行时类型,但它甚至与这些类型的性质无关:它们可以是引用类型(类)或值类型(结构);实际上,从集合/容器的角度来看,所有对象都是引用。
The instance of the structure defined below can be added to two different sets/containers at the same time, through, according to our code sample, both OnePolymorphicSet.Add
and AnotherPolymorphicSet.Add
.
Note that we also have "enforced" level of abstraction: not only the set is agnostic to a concrete runtime type of the contained objects, but it is even agnostic to the nature of those types: they can be either reference types (classes) or value types (structures); actually, from the point of view of the set/container, all objects are references.
这篇关于没有过载和过载的多态性是可能的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!