Learning C # - Abstract class

This class can not be instantiated, but the class member functions defined subclasses must override.

When using, pay attention to the difference between abstract and virtual member functions:

1. virtual function may or may not be covered overwrite, the function has actually defined function.

abstract class FourLeggedAnimal
    {
        public virtual string Describe()
        {
            return "Not much is known about this four legged animal!";
        }
    }

class Dog : FourLeggedAnimal
    {

    }

or

abstract class FourLeggedAnimal
{
    public virtual string Describe()
    {
        return "This animal has four legs.";
    }
}


class Dog : FourLeggedAnimal
{
    public override string Describe()
    {
        string result = base.Describe();
        result += " In fact, it's a dog!";
        return result;
    }
}

2. abstract function simply defines the function interface, no real function, must be re-implemented in the subclass is instantiated.

abstract class FourLeggedAnimal
    {
        public abstract string Describe();
    }


class Dog : FourLeggedAnimal
    {

        public override string Describe()
        {
            return "I'm a dog!";
        }
    }

class Cat : FourLeggedAnimal
    {
        public override string Describe()
        {
            return "I'm a cat!";
        }
    }
 

Reproduced in: https: //my.oschina.net/u/63375/blog/3057269

Guess you like

Origin blog.csdn.net/weixin_34311757/article/details/91918989