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

C#のクラスメソッドとクラスメンバーの違いは何ですか?


メンバー関数、つまりクラスのメソッドは、他の変数と同様に、クラス定義内にその定義またはプロトタイプを持つ関数です。メンバーであるクラスの任意のオブジェクトを操作し、そのオブジェクトのクラスのすべてのメンバーにアクセスできます。

以下は例です-

public void setLength( double len ) {
   length = len;
}

public void setBreadth( double bre ) {
   breadth = bre;
}

以下は、C#でクラスメンバー関数にアクセスする方法を示す例です-

using System;

namespace BoxApplication {
   class Box {
      private double length; // Length of a box
      private double breadth; // Breadth of a box
      private double height; // Height of a box

      public void setLength( double len ) {
         length = len;
      }

      public void setBreadth( double bre ) {
         breadth = bre;
      }

      public void setHeight( double hei ) {
         height = hei;
      }

      public double getVolume() {
         return length * breadth * height;
      }
   }

   class Boxtester {
      static void Main(string[] args) {
         Box Box1 = new Box(); // Declare Box1 of type Box
         Box Box2 = new Box();
         double volume;

         // Declare Box2 of type Box
         // box 1 specification
         Box1.setLength(8.0);
         Box1.setBreadth(9.0);
         Box1.setHeight(7.0);

         // box 2 specification
         Box2.setLength(18.0);
         Box2.setBreadth(20.0);
         Box2.setHeight(17.0);

         // volume of box 1
         volume = Box1.getVolume();
         Console.WriteLine("Volume of Box1 : {0}" ,volume);

         // volume of box 2
         volume = Box2.getVolume();
         Console.WriteLine("Volume of Box2 : {0}", volume);

         Console.ReadKey();
      }
   }
}

出力

Volume of Box1 : 504
Volume of Box2 : 6120

メンバー変数、つまりクラスメンバーは、(設計の観点から)オブジェクトの属性であり、カプセル化を実装するためにプライベートに保たれます。これらの変数には、パブリックメンバー関数を使用してのみアクセスできます。

この変数の新しいインスタンスはRectangleクラスの新しいインスタンスごとに作成されるため、長さと幅の下にはメンバー変数があります。

using System;

namespace RectangleApplication {
   class Rectangle {
      //member variables
      private double length;
      private double width;

      public void Acceptdetails() {
         length = 10;
         width = 14;
      }

      public double GetArea() {
         return length * width;
      }

      public void Display() {
         Console.WriteLine("Length: {0}", length);
         Console.WriteLine("Width: {0}", width);
         Console.WriteLine("Area: {0}", GetArea());
      }

   }//end class Rectangle

   class ExecuteRectangle {
      static void Main(string[] args) {
         Rectangle r = new Rectangle();
         r.Acceptdetails();
         r.Display();
         Console.ReadLine();
      }
   }
}

出力

Length: 10
Width: 14
Area: 140

  1. Wi-Fi 6と5Gネットワ​​ークの違いは何ですか?

    5Gネットワ​​ークは多くの話題を呼んでいるトピックであり、その実装はソーシャルネットワークに存在する会話のトピックです。別のテクノロジーであるWi-Fi6の周りに存在する期待のように。 Wi-Fi6と5Gネットワ​​ークの違いを特定する方法を学ぶ 。 WiFi6とは何ですか? Wi-Fi6は新しい認定に他なりません IEEE802.11ax規格に基づいています。以前のWi-Fiテクノロジーとは異なり、Wi-FI6はより長い距離でより高速な接続を可能にします。 その性能は、複数の接続されたデバイスで最適であり、電気的欠陥の影響を受けることなく適切に機能します。 2.4Ghzと5Ghzの周

  2. Windows 10 と Windows 11 の違いは?

    Microsoft は最新の Windows 11 をリリースし、多くの新機能と改善を行いました。また、多くのパッチとアップデートにより、Windows 10 は新しい Windows 11 よりも安定した OS になりました。同社によると、2025 年以降に Windows 10 でのサポートが終了するため、Windows 11 または Windows 10 へのアップグレードを決定するのに十分な時間があります。 Windows 10 を快適に使用してください。この記事では、Windows 10 と Windows 11 の比較を用意しました。 新しい OS に切り替えるか、Windows