C#
 Computer >> コンピューター >  >> プログラミング >> C#

C#の動的ポリモーフィズムとは何ですか?


ポリモーフィズムは静的または動的にすることができます。静的ポリモーフィズムでは、関数への応答はコンパイル時に決定されます。動的ポリモーフィズムでは、実行時に決定されます。

動的ポリモーフィズムは、抽象クラスと仮想関数によって実装されます。以下は、動的ポリモーフィズムの例を示す例です-

using System;

namespace PolymorphismApplication {
   class Shape {
      protected int width, height;

      public Shape( int a = 0, int b = 0) {
         width = a;
         height = b;
      }

      public virtual int area() {
         Console.WriteLine("Parent class area :");
         return 0;
      }
   }

   class Rectangle: Shape {
      public Rectangle( int a = 0, int b = 0): base(a, b) {}

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

   class Triangle: Shape {
      public Triangle(int a = 0, int b = 0): base(a, b) {}
      public override int area() {
         Console.WriteLine("Triangle class area :");
         return (width * height / 2);
      }
   }

   class Caller {
      public void CallArea(Shape sh) {
         int a;
         a = sh.area();
         Console.WriteLine("Area: {0}", a);
      }
   }

   class Tester {
      static void Main(string[] args) {
         Caller c = new Caller();
         Rectangle r = new Rectangle(10, 7);
         Triangle t = new Triangle(10, 5);

         c.CallArea(r);
         c.CallArea(t);
         Console.ReadKey();
      }
   }
}

出力

Rectangle class area :
Area: 70
Triangle class area :
Area: 25

  1. 動的IPアドレスとは何ですか?

    動的IPアドレスは、各接続またはノードに自動的に割り当てられるIPアドレスです。 、スマートフォン、デスクトップPC、ワイヤレスタブレットなどのネットワークの。このIPアドレスの自動割り当ては、DHCPサーバーと呼ばれるものによって実行されます。 DHCPサーバーによって割り当てられたIPアドレスは、動的と呼ばれます。 ネットワークへの将来の接続では異なることが多いためです。 動的IPアドレスの「反対」は、静的IPアドレス(手動で構成されたもの)と呼ばれます。 動的IPアドレスはどこで使用されますか? ほとんどのホームユーザーとビジネスユーザーのルーターにISPによって割り当てられる

  2. Windows 10の動的更新とは何ですか?

    私たちのほとんどは、Windowsの更新を一時停止し続けています。時間が経つほど、メジャーな新しいアップデートに近づき、不安が残ります–インストールにはどのくらい時間がかかりますか? 動的更新を見てみましょう。 Windows10の場合。 Windows10の動的更新 Windows 10の動的更新は、Windowsのオプションの自動更新プロセスであり、すべての更新を一度に取得してインストールするため、多数の連続した更新を行う必要がありません。これは、Windows 10の機能の更新がシステムにインストールされている場合に重要な役割を果たし、新規インストールまたは更新の一部となる可能性があり