Inheritance of C# learning notes

Posted by manlio on Tue, 01 Mar 2022 11:34:09 +0100

Inheritance is one of the most important concepts in object-oriented programming. Inheritance allows us to define one class according to another, which makes it easier to create and maintain applications. At the same time, it is also conducive to reusing code and saving development time.

When creating a class, programmers do not need to completely rewrite new data members and member functions. They only need to design a new class and inherit the members of the existing class. The existing class is called the base class, and the new class is called the derived class.

Base and derived classes

<Access modifier > class <Base class>
{
 ...
}
class <Derived class> : <Base class>
{
 ...
}

Initialization of base class

The derived class inherits the member variables and member methods of the base class. Therefore, the parent object should be created before the child object is created. You can initialize the parent class in the member initialization list.

C# multiple inheritance

Multiple inheritance refers to the function that a category can inherit behaviors and features from more than one parent class at the same time. In contrast to single inheritance, single inheritance means that a category can inherit from only one parent class.

C # does not support multiple inheritance. However, you can use interfaces to implement multiple inheritance.

using System;
namespace InheritanceApplication
{
   class Shape
   {
      public void setWidth(int w)
      {
         width = w;
      }
      public void setHeight(int h)
      {
         height = h;
      }
      protected int width;
      protected int height;
   }

   // Base class PaintCost
   public interface PaintCost
   {
      int getCost(int area);

   }
   // Derived class
   class Rectangle : Shape, PaintCost
   {
      public int getArea()
      {
         return (width * height);
      }
      public int getCost(int area)
      {
         return area * 70;
      }
   }
   class RectangleTester
   {
      static void Main(string[] args)
      {
         Rectangle Rect = new Rectangle();
         int area;
         Rect.setWidth(5);
         Rect.setHeight(7);
         area = Rect.getArea();
         // Plot the area of the object
         Console.WriteLine("Total area: {0}",  Rect.getArea());
         Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
         Console.ReadKey();
      }
   }
}
  • 1. Inherited syntax: class subclass class name: class parent class name {/ / subclass body}
  • 2. Characteristics of inheritance: subclasses have all fields, properties and methods in all parent classes
  • 3. A class can have multiple subclasses, but the parent class can only have one
  • 4. While a class inherits another class, it can also be inherited by other classes
  • All classes directly or indirectly inherit from C#

 

class Hardware  //Class Hardware is the parent class
{
    public string name;
    public double price;

}
class CPU : Hardware  //Class CPU is a subclass
{
    public string kind;
}
class MainBoard : CPU  //Point 4: CPU is the parent class of MainBoard
{

}
class Display
{


}
class Program
{
    static void Main(string[] args)
    {
        CPU c = new CPU();
        c.name = "";
        c.price = 1;
        c.ToString();  //It can be found that there is no ToString method in both parent and child classes  
    }
}
Enter anywhere in the program Object After, right-click the mouse to find the definition and click or press directly F12,You can see the following code:

public class Object
{
    //
    // Summary:
    //     Initialize system A new instance of the object class.
    [NonVersionableAttribute]
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
    public Object();

    //
    // Summary:
    //     Allow an object to attempt to free resources and perform other cleanup operations before garbage collection recycles it.
    [NonVersionableAttribute]
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    ~Object();

    //
    // Summary:
    //     Determines whether the specified object instances are considered equal.
    //
    // Parameters:
    //   objA:
    //     The first object to compare.
    //
    //   objB:
    //     The second object to compare.
    //
    // Return result:
    //     True if objects are considered equal, otherwise false. If both objA and objB are null, this method will return true.
    public static bool Equals(Object objA, Object objB);
    //
    // Summary:
    //     Determine the specified system Whether the object instance is the same.
    //
    // Parameters:
    //   objA:
    //     The first object to compare.
    //
    //   objB:
    //     The second object to compare.
    //
    // Return result:
    //     true if objA is the same instance as objB or if both are null; Otherwise, it is false.
    [NonVersionableAttribute]
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    public static bool ReferenceEquals(Object objA, Object objB);
    //
    // Summary:
    //     Determines whether the specified object is equal to the current object.
    //
    // Parameters:
    //   obj:
    //     The object to compare with the current object.
    //
    // Return result:
    //     true if the specified object is equal to the current object, otherwise false.
    public virtual bool Equals(Object obj);
    //
    // Summary:
    //     As the default hash function.
    //
    // Return result:
    //     The hash code of the current object.
    public virtual int GetHashCode();
    //
    // Summary:
    //     Gets the system. Name of the current instance Type. 
    //
    // Return result:
    //     The exact runtime type of the current instance.
    [SecuritySafeCritical]
    public Type GetType();
    //
    // Summary:
    //     Returns a string representing the current object.
    //
    // Return result:
    //     A string representing the current object.
    public virtual string ToString();
    //
    // Summary:
    //     Create the current system Shallow copy of object.
    //
    // Return result:
    //     Current system Shallow copy of object.
    [SecuritySafeCritical]
    protected Object MemberwiseClone();
}

Topics: C#