著者:桂素偉 翻訳者:欧Lily もし誤りがございましたらご指摘いただければ幸いです
本記事は初心者向けの基礎的な知識です!!!
普通クラス | 静的クラス | 抽象クラス | シールクラス | 部分クラス | |
---|---|---|---|---|---|
修飾子 | 無 | static | abstract | sealed | partial |
コントラストが呼び出されるタイミング | newキーワードでインスタンスを生成する際 | 任意の内部メンバーが呼ばれる際 | 派生クラス newキーワードでインスタンスを生成する際 | newキーワードでインスタンスを生成する際 | newキーワードでインスタンスを生成する際 |
メンバー | フィールド、プロパティ、メソッド、イベント、インデクサ、演算子のオーバーロード、ユーザー定義型 | 静的フィード、 静的プロパティ、静的メソッド、静的イベント、ユーザー定義型 | 普通クラスの すべてのメンバー、抽象プロパティ、抽象メソッド 抽象イベント 抽象インデクサ | 普通クラス すべての メンバー | 普通クラス すべての メンバー |
特徴 | インスタンスを生成できる。継承できる | クラス名.メンバー名でアクセスできる | 派生クラスでインスタンス化 | 継承の禁止 | クラスの分割定義 |
/// <summary>
/// 普通クラス
/// </summary>
public class CommonClass
{
/// <summary>
/// ユーザー定義enum型
/// </summary>
enum MyEnum
{
enum1,
enum2
}
/// <summary>
/// ユーザー定義delegate型
/// </summary>
public delegate void MyDeleaget();
/// <summary>
/// コンストラクタ
/// </summary>
public CommonClass()
{
_arr = new double[10];
}
/// <summary>
/// フィールド
/// </summary>
private string _myField;
/// <summary>
/// プロパティ
/// </summary>
public string MyProperty { get; set; }
/// <summary>
/// メソッド
/// </summary>
public void MyMethod()
{ }
/// <summary>
/// イベント
/// </summary>
public event MyDeleaget MyEvent;
/// <summary>
/// 演算子オーバーロード
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static CommonClass operator +(CommonClass a, CommonClass b)
{
return new CommonClass() { MyProperty = a.MyProperty + b.MyProperty };
}
/// <summary>
/// インデクサコレクション
/// </summary>
double[] _arr;
/// <summary>
/// インデクサ
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public double this[int index]
{
get
{
return _arr[index];
}
set
{
_arr[index] = value;
}
}
}
/// <summary>
/// 静的クラス
/// </summary>
public static class StaticClass
{
/// <summary>
/// 静的コンストラクタ
/// </summary>
static StaticClass()
{
Console.WriteLine("静的コンストラクタ");
}
/// <summary>
/// 静的メソッド
/// </summary>
public static void StaticMethod()
{
Console.WriteLine("静的クラスの静的メソッド");
}
/// <summary>
/// 静的プロパティ
/// </summary>
public static string StaticProperty { get; set; }
/// <summary>
/// ユーザー定義delegate型
/// </summary>
public delegate void MyDeleaget();
/// <summary>
/// フィールド
/// </summary>
private static string _myField;
/// <summary>
/// イベント
/// </summary>
public static event MyDeleaget MyEvent;
}
/// <summary>
/// 抽象クラス
/// </summary>
public abstract class AbstractClass
{
public AbstractClass()
{
Console.WriteLine("抽象クラスのコンストラクタ");
}
/// <summary>
/// ユーザー定義delegate型
/// </summary>
public delegate void MyDeleaget();
/// <summary>
/// プロパティ
/// </summary>
public string MyProperty { get; set; }
/// <summary>
/// メソッド
/// </summary>
public abstract void MyMethod();
/// <summary>
/// イベント
/// </summary>
public abstract event MyDeleaget MyEvent;
/// <summary>
/// インデクサ
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public abstract double this[int index] { get; set; }
}
/// <summary>
/// シールクラス
/// </summary>
public sealed class SealedClass
{
/// <summary>
/// コンストラクタ
/// </summary>
public SealedClass()
{
Console.WriteLine("シールクラスコンストラクタ");
}
/// <summary>
/// ユーザー定義delegate型
/// </summary>
public delegate void MyDeleaget();
/// <summary>
/// フィールド
/// </summary>
private string _myField;
/// <summary>
/// プロパティ
/// </summary>
public string MyProperty { get; set; }
/// <summary>
/// メソッド
/// </summary>
public void MyMethod()
{ }
/// <summary>
/// イベント
/// </summary>
public event MyDeleaget MyEvent;
/// <summary>
/// オペレーターのオーバーロード
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static SealedClass operator +(SealedClass a, SealedClass b)
{
return new SealedClass() { MyProperty = a.MyProperty + b.MyProperty };
}
/// <summary>
/// インデクサコレクション
/// </summary>
double[] _arr;
/// <summary>
/// インデクサ
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public double this[int index]
{
get
{
return _arr[index];
}
set
{
_arr[index] = value;
}
}
}
/// <summary>
/// 部分クラス1
/// </summary>
public partial class PartialClass
{
public PartialClass()
{
Console.WriteLine("部分クラスコンストラクタ");
}
/// <summary>
/// ユーザー定義delegate型
/// </summary>
public delegate void MyDeleaget();
/// <summary>
/// フィールド
/// </summary>
private string _myField;
/// <summary>
/// プロパティ
/// </summary>
public string MyProperty { get; set; }
/// <summary>
/// メソッド
/// </summary>
public void MyMethod()
{ }
/// <summary>
/// オペレーターオーバーロード
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static PartialClass operator +(PartialClass a, PartialClass b)
{
return new PartialClass() { MyProperty = a.MyProperty + b.MyProperty };
}
/// <summary>
/// インデクサコレクション
/// </summary>
double[] _arr;
}
/// <summary>
/// 部分クラス1
/// </summary>
public partial class PartialClass
{
/// <summary>
/// イベント
/// </summary>
public event MyDeleaget MyEvent;
/// <summary>
/// インデクサ
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public double this[int index]
{
get
{
return _arr[index];
}
set
{
_arr[index] = value;
}
}
}