C# basics of abstract learning

C# basics of abstract learning

1. Definition

Abstract classes are often used to characterize the abstract concepts derived from the analysis and design of the problem domain. It is an abstraction of a series of concrete concepts that look different but are essentially the same.

2. Features

  • Can not be instantiated, mainly used for inheritance

  • Can contain two kinds of abstract members, namely abstract attributes and abstract methods , and can contain other non-abstract members

  • If you inherit the abstract class, you need to implement the abstract properties and methods of the abstract class

  • The Sealed keyword cannot be used to modify the abstract class, because Sealed means that the class cannot be inherited, and Static cannot modify the abstract class.

In short, abstract classes are used for inheritance, and the subclasses after inheritance must follow the will of the abstract class to realize the will, that is, inherit all the abstract methods and abstract properties in the abstract class.

Simple application example:

(1) Define the abstract class Fruit. The abstract class has public attribute vendor, abstract attribute Price and abstract method GrowArea. The code is as follows:

namespace AbstractClassLearn
{
    public abstract class Fruit
    {
        public string vendor { get; set; }

        public abstract float Price { get; }

        public abstract void GrowArea();
    }
}

(2) Define an Apple class that inherits the abstract class, the code is as follows:

namespace AbstractClassLearn
{
    public class Apple : Fruit
    {
        public override float Price
        {
            get
            {
                if (vendor == "红富士")
                {
                    return 1000;
                }
                else return 0;
            }
        }

        public override void GrowArea()
        {
            Console.WriteLine("水果品种:" + vendor + "  " + "当前价格是:" + Price);
        }
    }
}

(3) Define an Orange class that inherits the abstract class. The code is as follows:

namespace AbstractClassLearn
{
    public class Orange : Fruit
    {
        public override float Price
        {
            get
            {
                if (vendor == "橘子") return 200;
                else return 0;
            }
        }

        public override void GrowArea()
        {
            Console.WriteLine("水果品种:" + vendor + "  " + "当前价格是:" + Price);
        }
    }
}

(4) Enter in the main function:

namespace AbstractClassLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            Fruit f = new Apple();
            f.vendor = "苹果";
            f.GrowArea();

            f = new Orange();
            f.vendor = "橘子";
            f.GrowArea();

            Console.ReadKey();
        }
    }
}

The result is shown in the figure below:

3. Under what circumstances should abstract classes be used

For example: we need to create objects (classes) such as "dog", "cat", "horse", etc., and they have some common properties (properties) like mouth, tail, weight, color, size, etc., but they are mutually exclusive There are differences among the attributes (such as the size of the mouth, the length of the tail, etc.). In this case, it would be more cumbersome if we define the respective attributes one by one. If you use abstract classes, the implementation process will be very convenient.

There is also a better aspect of abstract classes, that is, "similarities and different images", which is explained as: inherit the same method in different implementation forms , such as dogs, cats, horses, etc. with different breathing methods or different running speeds. At this time Define abstract methods in abstract classes, let them inherit from abstract classes, and implement their own forms in abstract methods. The meaning of abstraction lies in: extracting the common things and encapsulating them, but not implementing them but inheriting them .

4. Summary

  1. The abstract attributes and abstract methods in the abstract class must be public, using the public modifier

  2. The subclass must override all abstract attributes and abstract methods in the abstract class. If not all of the abstract properties and methods in the abstract class must be overridden, then the subclass must be an abstract class

  3. An abstract class can have non-abstract properties and non-abstract methods, or it can be private or public, but if it is private, then the subclass will be inaccessible and meaningless, so it is generally written as public

  4. Classes with abstract methods or abstract properties must be abstract classes, and the properties or methods in an abstract class are not necessarily all abstract

  5. Abstract methods cannot have function bodies, they can only be defined, and cannot contain specific implementations. Abstract classes cannot be instantiated, but can only be inherited.

Guess you like

Origin blog.csdn.net/Kevin_Sun777/article/details/113884255