抽象类的特点 [英] characteristics of the abstract class

查看:154
本文介绍了抽象类的特点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是什么让一个被称为absract类类。我相信,abract关键词definetly使类类,但如果取出的关键字,那么我们就可以创建类的实例。

I like to know what makes a class to be called as absract class. I believe, abract key word definetly make a class class, but if one takes out the keyword, then we can create the instance of the class.

在otherwords,什么是抽象类的特点。

In otherwords, what are the characteristics of the abstract class.

在此先感谢。

-Harsha

推荐答案

的抽象类不形成不同于纯粹的实现类现实世界中的具体对象。摘要因为他们持有/定义需要被重用/在所有相关对象independantly定义相关对象的共有行为的名字suggestes。

An abstract class does not form a concrete object in the real world unlike pure implementation classes. Abstract as the name suggestes they hold/define common behaviours of related objects that need to be reused/defined independantly in all related objects.

以鸟的例子。如果你正在写一个PROGRM,将有一些做的鸟,那么你就先一个抽象基类鸟和每只鸟从抽象基类派生鸟。别注意,抽象类鸟不代表一个具体真实世界对象,但一类型相关的对象是鸟类!

Take an example of Birds. If you are writing a progrm that will have something to do with the birds, then you'll first have an abstract base class as Bird and each bird deriving from the abstract base class Bird. Do note that abstract class BIRD does not represent a concrete real world object but a type of related objects that is birds!

让我们开始与类图和随后的一些代码

Lets start with the class-diagram and then some code.

public abstract class Bird
{
    protected string Name = string.Empty;
    public Bird(string name)
    {
        this.Name = name;
    }

    public virtual void Fly()
    {
        Console.WriteLine(string.Format("{0} is flying.", this.Name));
    }

    public virtual void Run()
    {
        Console.WriteLine(string.Format("{0} cannot run.", this.Name));
    }
}

public class Parrot : Bird
{
    public Parrot() : base("parrot") { }
}

public class Sparrow : Bird
{
    public Sparrow() : base("sparrow") { }
}

public class Penguin : Bird
{
    public Penguin() : base("penguin") { }

    public override void Fly()
    {
        Console.WriteLine(string.Format("{0} cannot fly. Some birds do not fly.", this.Name));
    }

    public override void Run()
    {
        Console.WriteLine(string.Format("{0} is running. Some birds do run.", this.Name));
    }
}

class Program
{
    static void Main(string[] args)
    {

        Parrot p = new Parrot();
        Sparrow s = new Sparrow();
        Penguin pe = new Penguin();

        List<Bird> birds = new List<Bird>();

        birds.Add(p);
        birds.Add(s);
        birds.Add(pe);

        foreach (Bird bird in birds)
        {
            bird.Fly();
            bird.Run();
        }

        Console.ReadLine();
    }
}

这篇关于抽象类的特点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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