Csharp-classes
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* キーワードを使用して、クラスメンバーを静的として定義できます。 クラスのメンバーを静的として宣言する場合、クラスのオブジェクトがいくつ作成されても、静的メンバーのコピーは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