Simple application code analysis of c# inheritance

Time:2022-1-17

For example, there are some graphs that need to calculate their area. The method of calculating the area is different. You can do this

Declare an abstract class

//Base class
    abstract class Shape
    {
        //Abstract method to calculate area
        public abstract double ComputerArea();

    }

Declare subclasses

//Subclasses inherit shape to implement abstract methods
    class Circle : Shape
    {
        private double _radius;

        //Constructor
        public Circle(double radius) => _radius = radius;

        //Implement abstract methods
        public override double ComputerArea()
        {
            return _radius * _radius * Math.PI;
        }
    }

    //Subclasses inherit shape to implement abstract methods
    class Rectangle : Shape
    {

        private double _width;

        private double _height;

        //Constructor
        public Rectangle(double width, double height)
        {
            _width = width;
            _height = height;
        }
        //Implement abstract methods
        public override double ComputerArea()
        {
            return _width * _height;
        }
    }

    //Subclasses inherit shape to implement abstract methods
    class Triangle : Shape
    {

        private double _bottom;

        private double _height;

        //Constructor
        public Triangle(double bottom, double height)
        {
            _bottom = bottom;
            _height = height;
        }
        //Implement abstract methods
        public override double ComputerArea()
        {
            return _bottom * _height / 2;
        }
    }

Declare calculation class

//Calculation class
    class Calculate
    {
        //Pass in a parent class as a parameter and call the method
        public void Calc(Shape shape)
        {

            Console. Writeline ($"{shape. Gettype(). Name}" area: {shape. Computerarea()} ");
        }
    }

test


class Program
    {
        static void Main(string[] args)
        {
            var circle = new Circle(5);
            var rect = new Rectangle(5, 10);
            var triangle = new Triangle(6, 8);

            var calc = new Calculate();

            calc.Calc(circle);
            calc.Calc(rect);
            calc.Calc(triangle);
        }
    }

Operation results

In fact, if only this method is to be implemented, the inheritance interface is also possible!

Instance extension:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


/**
 *Suppose our company has two kinds of programmers:
 *Java programmers refer to programmers who write programs in Java syntax, represented by the class jpro;
 *C # programmers refer to programmers who write programs in C # syntax, represented by the class CPRO.
 *Each class has a writecode () method.
 *All belong to programmers, represented by the class programmer.
 *Now the company has come to a project and asked to send a programmer to write a program.
 */
namespace inheritApplication
{
  //Programmer class (base class)
  class Programmer
  {
    Public string name = "I am a programmer";
  }
  //Java programmer class
  class Jpro 
  {
    Public string name = "java programmer";
    Public string language = "I write code in Java syntax";
  }
  //C programmer class
  class Cpro 
  {
    Public string name = "c# programmer";
    Public string language = "I write code in c# syntax";
  }
  //Write project class
  Class Pro: Programmer // inherits the base class
  {
    Public void writeprogram (jpro program) // inherit java programmer class
    {
      Console.WriteLine("" + program.name + ":" + name + "," + program.language + "");
    }
    public void WriteProgram(Cpro program)
    {
      Console.WriteLine("" + program.name + ":" + name + "," + program.language + "");
    }
  }
  //Main program
  class Program
  {
    static void Main(string[] args)
    {
      Pro p = new Pro();
      Jpro p1 = new Jpro();
      p.WriteProgram(p1);
      Cpro p2 = new Cpro();
      p.WriteProgram(p2);
      Console.ReadKey();
    }
  }
}

This is the end of this article about simple application code analysis of c# inheritance. For more simple application content of c# inheritance, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!