Rewritten in C#

WBOY
WBOYforward
2023-09-04 14:53:02996browse

在 C# 中重写

Runtime polymorphism has method overriding, also known as dynamic binding or late binding. It is implemented through abstract classes and virtual functions. Abstract classes contain abstract methods, which are implemented by derived classes.

Let's see an example of an abstract class that implements runtime polymorphism and is used with overriding -

Example

using System;

namespace PolymorphismApplication {
   abstract class Shape {
      public abstract int area();
   }

   class Rectangle: Shape {
      private int length;
      private int width;

      public Rectangle( int a = 0, int b = 0) {
         length = a;
         width = b;
      }

      public override int area () {
         Console.WriteLine("Rectangle class area :");
         return (width * length);
      }
   }

   class RectangleTester {
      static void Main(string[] args) {
         Rectangle r = new Rectangle(10, 7);
         double a = r.area();
         Console.WriteLine("Area: {0}",a);
         Console.ReadKey();
      }
   }
}

The above is the detailed content of Rewritten in C#. 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