Home  >  Article  >  Backend Development  >  C# example of hierarchical inheritance

C# example of hierarchical inheritance

王林
王林forward
2023-08-29 18:33:02644browse

层次继承的 C# 示例

In hierarchical inheritance, multiple classes are inherited from the base class.

In the example, our base class is Father -

class Father {
   public void display() {
      Console.WriteLine("Display...");
   }
}

which has Son and Daughter as derived classes . Let us how to add derived class in inheritance -

class Son : Father {
   public void displayOne() {
      Console.WriteLine("Display One");
   }
}

Example

Here is the complete example to implement hierarchical inheritance in C# -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inheritance {
   class Test {
      static void Main(string[] args) {
         Father f = new Father();
         f.display();
         Son s = new Son();
         s.display();
         s.displayOne();
         Daughter d = new Daughter();
         d.displayTwo();
         Console.ReadKey();
      }
      class Father {
         public void display() {
            Console.WriteLine("Display...");
         }
      }
      class Son : Father {
         public void displayOne() {
            Console.WriteLine("Display One");
         }
      }
      class Daughter : Father {
         public void displayTwo() {
            Console.WriteLine("Display Two");
         }
      }
   }
}

The above is the detailed content of C# example of hierarchical inheritance. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete