Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ クラスコンストラクタ | オブジェクト指向プログラミング(OOP)イントロダクション
C#オブジェクト指向構造

bookクラスコンストラクタ

メニューを表示するにはスワイプしてください

クラスコンストラクターは、構造体コンストラクターと同じです。オブジェクトが作成されるときに実行されるメソッド。

コンストラクターを作成するための構文は次のとおりです:

index.cs

index.cs

copy
1234567
class className { // ... other class code public className() { // code } }

例:

index.cs

index.cs

copy
1234567891011121314151617
using System; class Player { public Player() { Console.WriteLine("A new player is created"); } } public class HelloWorld { public static void Main(string[] args) { Player p1 = new Player(); Player p2 = new Player(); Player p3 = new Player(); } }

出力から、Console.WriteLine コンストラクター内の Player 文が新しいオブジェクトが作成されるたびに実行されていることがわかります。

Note
注意

C# 9 およびそれ以前のバージョンの C# では、構造体とクラスのコンストラクターにいくつかの違いがあります。たとえば、古いバージョンでは構造体にパラメーターなしのコンストラクターを作成できませんが、C# 10 以降のバージョンではサポートされています。最新バージョンの C# コンパイラーを使用することが推奨されます。そうしないと、一部のコードをコンパイルする際にエラーが発生する可能性があります。

以下は、Struct Constructors(構造体コンストラクタ)章からのサンプルコードです。static という用語は、この章でより関連性が高いためコメント内で説明されていますが、この概念について完全に理解する必要はありません。詳細は後のセクションで説明されます。

Note
注意

通常のフィールドとは異なり、static フィールドはクラスのオブジェクトが存在しなくてもデータを保持できます。static フィールドはすべてのオブジェクトで同じ値を持つため、1つのオブジェクトから static フィールドの値を変更すると、すべてのオブジェクトに対して変更されます。Program クラスはプログラム自体を表しているため、static フィールドが 'Program' クラスのメンバーである場合、グローバル変数のように機能します。

index.cs

index.cs

copy
1234567891011121314151617181920212223
using System; struct Program { public static int totalPlayers = 0; struct Player { public int id; public Player() { id = totalPlayers++; Console.WriteLine($"New Player Object Created With ID {id}"); } } static void Main(string[] args) { Player player1 = new Player(); Player player2 = new Player(); Player player3 = new Player(); } }

Player構造体をクラスに変換するには、structclassに変更するだけです。

index.cs

index.cs

copy
12345678910111213141516171819202122232425262728
using System; class Program { // Unlike normal fields, a 'static' field can hold data even if there's no object. // Static fields have the same value for all objects. // Changing the value of a static field from one object changes it for all objects. // In this case the static field is part of the 'Program' class which represents the program itself // Therefore this field is essentially a global variable public static int totalPlayers = 0; struct Player { public int id; public Player() { id = totalPlayers++; Console.WriteLine($"New Player Object Created With ID {id}"); } } static void Main(string[] args) { Player player1 = new Player(); Player player2 = new Player(); Player player3 = new Player(); } }

同様に、引数付きのコンストラクターも定義できます。通常、この方法はクラスフィールドの初期化を簡単にするために使用されます。

index.cs

index.cs

copy
12345678910111213141516171819202122232425262728
using System; class Point { public double x; public double y; public Point(double x, double y) { // 'this' keyword is also used in classes just like how its used in structs this.x = x; this.y = y; } public void showCoordinates() { Console.WriteLine($"({x}, {y})"); } } class Program { static void Main(string[] args) { Point p1 = new Point(50, 70); Point p2 = new Point(70, 90); p1.showCoordinates(); p2.showCoordinates(); } }

この時点で、Classes基本的な概念のほとんどを学習しました。構文や動作から判断すると、ClassesStructsとまったく同じように見えますが、実際にはそうではありません。このセクションの最初の章で述べたように、構造体はクラスの限定バージョンであり、基本的な機能のみを提供します。一方で、クラスの機能ははるかに複雑かつ広範です。このセクションでは、ClassesStructsの間で共通するすべての機能について説明しました。次の2つのセクションでは、クラスおよびオブジェクト指向プログラミングのすべての詳細について学びます。

1. コンストラクタメソッドはいつ呼び出されますか?

2. 「Animal」というクラスのコンストラクタメソッドの名前は何になりますか?

3. コンストラクタ名の前に使用されるキーワードはどれですか?

question mark

コンストラクタメソッドはいつ呼び出されますか?

正しい答えを選んでください

question mark

「Animal」というクラスのコンストラクタメソッドの名前は何になりますか?

正しい答えを選んでください

question mark

コンストラクタ名の前に使用されるキーワードはどれですか?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 3.  9

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 3.  9
some-alt