Rewritten in C#

WBOY
Release: 2023-09-04 14:53:02
forward
996 people have browsed it

在 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();
      }
   }
}
Copy after login

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

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template