开发者

How is Polymorphism and Abstract class implemented/used in the .NET Framework or ASP.NET

I am not able to understand the usage of Polymorphism and Abstract classes in the .NET Framework or ASP.NET. I know what Polymorphism means after reading various articles on the internet, however, they are generic (or it may seem to me). But I am not able to co-relate that to something concrete in .NET. For eg, why is a particular class in the .NET Framework marked as ABSTRACT. If someone can explain that, I would be in a better position to understand its need. Likewis开发者_如何学编程e for Polymorphism.

I hope I am clear in what I am asking and would love to get some really good explanation.

Thank you


The XmlReader class is an excellent example of polymorphism. One programs against XmlReader, which is an abstract class, but the actual instances you use are of concrete derived classes.

And it doesn't matter which derived class is being used. You usually don't even know which class is being used, since you're meant to use the factory method 'XmlReader.Create()' to create your instances.


Let's expand on the example that John suggests for you. The class XmlReader is marked as abstract - in C# you explicitly declare the class as abstract as in: public abstract class XmlReader : IDisposable {} The class may also have methods or properties declared as abstract as in public abstract void Close(); What the abstract class is declaring in the properties and methods is the interface that all inherited classes must implement. It shows the desire of the library designer to lead and direct the development of the interface of. The derived classesSystem.Xml.XmlNodeReader, System.Xml.XmlTextReader, System.Xml.XmlValidatingReader all must have all the methods declared for the XmlReader class.


The time to use an abstract class is when you have two or more classes sharing significant characteristics, but the base class wouldn't make sense by itself. There are different types of polymorphism, but the kind I think you're referring to is good for when you have two types with methods that do the same task, but in a different way depending on their type.

For example, suppose you have a base class called animal. It communicates a lot of information already: All animals have properties like size, shape, warm/cold bloodedness, and number of legs. All animals have certain behaviors, like moving and eating, although they don't always do them the same way.

However, it doesn't make sense to say, "There's an animal in the yard," or "Please buy me an animal at the pet store." You'd immediately be asked, "What kind of animal?" Your listener can't tell how to react until he knows whether it's a parakeet or a grizzly bear.

So an animal abstract class would contain methods and properties all animals share, while the derived classes dog, cat, and sparrow would implement them, plus providing some of their own. Where the polymorphism comes in is when two animals do the same thing, but they do it in different ways. Here's an example:

abstract class Animal
{
    public abstract void Move(int distance);
}
class Dog : Animal
{
    public override void Move(int distance)
    {
        Console.WriteLine("Walking " + distance + " feet.");
    }
} 
class Sparrow : Animal
{
    public override void Move(int distance)
    {
        Console.WriteLine("Flying " + distance + " feet.");
    }
} 

Polymorphism comes into play when you use these in your program. You have a bunch of animals; maybe they have different types, or maybe you don't know what type they are. You want them all to move 10 feet forward, but you don't care how they get there. So you call the Move method that all animals have, even though they've all implemented it differently:

class Program
{
    void MoveAnimal(Animal animal)
    {
        animal.Move(10);
    }
//...
}

Now your flying animals will fly, your walking animals will walk, but they'll all move the same distance, which is what you really wanted. Some real-life applications of this type of polymorphism:

  • Rendering a group of WebControls in ASP.NET: You want all to show up in the page, but they won't all look the same.
  • Using the IEnumerable.Contains method of a bunch of LinkedList<T> and List<T> objects. They'll all look for what you're searching for, but they'll use different strategies to find it.

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜