Abstract Categories of C#

Abstract classes have three characteristics:

First, cannot be instantiated.

Second, abstract methods in classes must be overridden in inherited subclasses.

Third, once a class has an abstract method, it must be defined as an abstract class.

 

In real-world development, common code is found that can be placed in an abstract class.For example, calculate the perimeter of a quadrilateral.


Rectangle, Rectangle, Square:

 

 class Rectangle
    {
        private double _A;
        public double A
        {
            get { return _A; }
            set { _A = value; }
        }

        private double _B;
        public double B
        {
            get { return _B; }
            set { _B = value; }
        }

        public Rectangle()
        {

        }

        public Rectangle(double a, double b)
        {
            this._A = a;
            this._B = b;
        }

        public double Perimeter()
        {
            return 2 * (_A + _B);
        }
    }
Source Code

 

Oblique, oblong diamond:

 

 class Rhomboid 
    {
        private double _A;
        public double A
        {
            get { return _A; }
            set { _A = value; }
        }


        private double _B;
        public double B
        {
            get { return _B; }
            set { _B = value; }
        }

        public Rhomboid()
        {

        }

        public Rhomboid(double a,double b)
        {
            this._A = a;
            this._B = b;
        }
        public double Perimeter()
        {
            return 2 * (_A + _B);
        }
    }
Source Code

 

Trapezoid, unequal quadrilateral:

 

 class Trapezoid
    {
        private double _A;
        public double A
        {
            get { return _A; }
            set { _A = value; }
        }


        private double _B;
        public double B
        {
            get { return _B; }
            set { _B = value; }
        }


        private double _C;
        public double C
        {
            get { return _C; }
            set { _C = value; }
        }


        private double _D;
        public double D
        {
            get { return _D; }
            set { _D = value; }
        }

        public Trapezoid()
        {

        }

        public Trapezoid(double a,double b,double c,double d)
        {
            this._A = a;
            this._B = b;
            this._C = c;
            this._D = d;
        }

        public double Perimeter()
        {
            return _A + _B + _C + _D;
        }
    }
Source Code

 

You can see from the three categories above that Perimeter() is the method for calculating the perimeter of a quadrilateral.
As you said at the beginning of this post, you can pull this method out and put it in an abstract class, unified interface:

public abstract class Quadrilateral
    {
        //Perimeter
        public abstract double Perimeter();
    }
Source Code

 

After writing methods as abstract methods, each class needs to inherit this abstract class and override the abstract method.

 

 

 

To verify the code below, if you need to calculate the perimeter of an irregular quadrilateral, add up the four edges:

 

Run in the console:

Keywords: C#

Added by wertz33 on Fri, 08 May 2020 19:29:57 +0300