C#-クラス
クラスを定義するときは、データ型の設計図を定義します。これは実際にはデータを定義しませんが、クラス名の意味を定義します。つまり、クラスのオブジェクトの構成要素と、そのオブジェクトに対して実行できる操作です。オブジェクトはクラスのインスタンスです。クラスを構成するメソッドと変数は、クラスのメンバーと呼ばれます。
クラスの定義
クラス定義は、キーワード class で始まり、その後にクラス名が続きます。クラス本体は一対の中括弧で囲まれています。以下は、クラス定義の一般的な形式です-
<access specifier> class class_name { // member variables <access specifier> <data type> variable1; <access specifier> <data type> variable2; ... <access specifier> <data type> variableN; // member methods <access specifier> <return type> method1(parameter_list) { // method body } <access specifier> <return type> method2(parameter_list) { // method body } ... <access specifier> <return type> methodN(parameter_list) { // method body } }
注-
-
アクセス指定子は、メンバーとクラス自体のアクセス ルールを指定します。言及されていない場合、クラス タイプのデフォルトのアクセス指定子は internal です。 .メンバーのデフォルト アクセスは private です .
-
データ型は変数の型を指定し、戻り型はメソッドが返すデータのデータ型を指定します (存在する場合)。
-
クラス メンバーにアクセスするには、ドット (.) 演算子を使用します。
-
ドット演算子は、オブジェクトの名前をメンバーの名前にリンクします。
次の例は、これまでに説明した概念を示しています −
ライブデモusing System; namespace BoxApplication { class Box { public double length; // Length of a box public double breadth; // Breadth of a box public double height; // Height of a box } class Boxtester { static void Main(string[] args) { Box Box1 = new Box(); // Declare Box1 of type Box Box Box2 = new Box(); // Declare Box2 of type Box double volume = 0.0; // Store the volume of a box here // box 1 specification Box1.height = 5.0; Box1.length = 6.0; Box1.breadth = 7.0; // box 2 specification Box2.height = 10.0; Box2.length = 12.0; Box2.breadth = 13.0; // volume of box 1 volume = Box1.height * Box1.length * Box1.breadth; Console.WriteLine("Volume of Box1 : {0}", volume); // volume of box 2 volume = Box2.height * Box2.length * Box2.breadth; Console.WriteLine("Volume of Box2 : {0}", volume); Console.ReadKey(); } } }
上記のコードをコンパイルして実行すると、次の結果が生成されます −
Volume of Box1 : 210 Volume of Box2 : 1560
メンバー関数とカプセル化
クラスのメンバー関数は、他の変数と同様に、クラス定義内に定義またはプロトタイプを持つ関数です。メンバーであるクラスの任意のオブジェクトを操作し、そのオブジェクトのクラスのすべてのメンバーにアクセスできます。
メンバー変数は (設計の観点から) オブジェクトの属性であり、カプセル化を実装するために非公開に保たれます。これらの変数には、パブリック メンバー関数を使用してのみアクセスできます。
クラス内のさまざまなクラスメンバーの値を設定および取得するために上記の概念を入れましょう-
ライブデモ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(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0); // box 2 specification Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.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 : 210 Volume of Box2 : 1560
C# コンストラクター
クラス コンストラクタ クラスの新しいオブジェクトを作成するたびに実行されるクラスの特別なメンバー関数です。
コンストラクターは、クラスの名前とまったく同じ名前を持ち、戻り値の型はありません。次の例は、コンストラクターの概念を説明しています-
ライブデモusing System; namespace LineApplication { class Line { private double length; // Length of a line public Line() { Console.WriteLine("Object is being created"); } public void setLength( double len ) { length = len; } public double getLength() { return length; } static void Main(string[] args) { Line line = new Line(); // set line length line.setLength(6.0); Console.WriteLine("Length of line : {0}", line.getLength()); Console.ReadKey(); } } }
上記のコードをコンパイルして実行すると、次の結果が生成されます −
Object is being created Length of line : 6
デフォルトのコンストラクタ パラメーターはありませんが、必要に応じて、コンストラクターにパラメーターを含めることができます。このようなコンストラクタは、パラメータ化されたコンストラクタと呼ばれます .この手法は、次の例に示すように、作成時にオブジェクトに初期値を割り当てるのに役立ちます −
ライブデモusing System; namespace LineApplication { class Line { private double length; // Length of a line public Line(double len) { //Parameterized constructor Console.WriteLine("Object is being created, length = {0}", len); length = len; } public void setLength( double len ) { length = len; } public double getLength() { return length; } static void Main(string[] args) { Line line = new Line(10.0); Console.WriteLine("Length of line : {0}", line.getLength()); // set line length line.setLength(6.0); Console.WriteLine("Length of line : {0}", line.getLength()); Console.ReadKey(); } } }
上記のコードをコンパイルして実行すると、次の結果が生成されます −
Object is being created, length = 10 Length of line : 10 Length of line : 6
C# デストラクタ
デストラクタ クラスのオブジェクトがスコープ外になるたびに実行されるクラスの特別なメンバー関数です。 デストラクタ 接頭辞 (~) が付いたクラスの名前とまったく同じ名前を持ち、値を返すこともパラメータを取ることもできません。
デストラクタは、プログラムを終了する前にメモリ リソースを解放するのに非常に役立ちます。デストラクタは継承またはオーバーロードできません。
次の例は、デストラクタの概念を説明しています-
ライブデモusing System; namespace LineApplication { class Line { private double length; // Length of a line public Line() { // constructor Console.WriteLine("Object is being created"); } ~Line() { //destructor Console.WriteLine("Object is being deleted"); } public void setLength( double len ) { length = len; } public double getLength() { return length; } static void Main(string[] args) { Line line = new Line(); // set line length line.setLength(6.0); Console.WriteLine("Length of line : {0}", line.getLength()); } } }
上記のコードをコンパイルして実行すると、次の結果が生成されます −
Object is being created Length of line : 6 Object is being deleted
C# クラスの静的メンバー
static を使用して、クラス メンバーを static として定義できます。 キーワード。クラスのメンバーを静的として宣言すると、クラスのオブジェクトがいくつ作成されても、静的メンバーのコピーは 1 つしかないことを意味します。
キーワード static クラスに対してメンバーのインスタンスが 1 つだけ存在することを意味します。静的変数は、インスタンスを作成せずにクラスを呼び出すことで値を取得できるため、定数の定義に使用されます。静的変数は、メンバー関数またはクラス定義の外で初期化できます。クラス定義内で静的変数を初期化することもできます。
次の例は、静的変数の使用を示しています −
ライブデモusing System; namespace StaticVarApplication { class StaticVar { public static int num; public void count() { num++; } public int getNum() { return num; } } class StaticTester { static void Main(string[] args) { StaticVar s1 = new StaticVar(); StaticVar s2 = new StaticVar(); s1.count(); s1.count(); s1.count(); s2.count(); s2.count(); s2.count(); Console.WriteLine("Variable num for s1: {0}", s1.getNum()); Console.WriteLine("Variable num for s2: {0}", s2.getNum()); Console.ReadKey(); } } }
上記のコードをコンパイルして実行すると、次の結果が生成されます −
Variable num for s1: 6 Variable num for s2: 6
メンバー関数を宣言することもできます 静的として .このような関数は、静的変数のみにアクセスできます。静的関数は、オブジェクトが作成される前から存在します。次の例は、静的関数の使用を示しています −
ライブデモusing System; namespace StaticVarApplication { class StaticVar { public static int num; public void count() { num++; } public static int getNum() { return num; } } class StaticTester { static void Main(string[] args) { StaticVar s = new StaticVar(); s.count(); s.count(); s.count(); Console.WriteLine("Variable num: {0}", StaticVar.getNum()); Console.ReadKey(); } } }
上記のコードをコンパイルして実行すると、次の結果が生成されます −
Variable num: 3
C言語