unity delegateイベント依頼
2720 ワード
1.delegateの場合、c#はオブジェクト間のメッセージングを極めて発揮し、c++の関数ポインタに似ていますが、delegateはより多くの関数を搭載することができますが、c++の関数ポインタは1つの関数しか参照できません.
2.delegateはオブジェクト間で追加搭載できます.
3.delegate typeの宣言は本質的に新しいsubtype instanceを作成し、このsubtypeは派生した.NET library frameworkのabstract base classes DelegteまたはMulticastDelegteは、delegateオブジェクトまたはその搭載方法(methods)を問い合わせるためのpublic methodsのセットを提供し、関数ポインタとは異なり、依頼はオブジェクト向けであり、タイプが安全で安全である.
【1】public delegate void DelegateMethod().ここではDelegateMethodというDelegateMethodのタイプを宣言している.このDelegteタイプは、voidの戻り値であり、入力パラメータのない関数を搭載することができる.【2】public DelegateMethod DelegateMethod;ここでDelegateMethodのオブジェクトを宣言する(つまり、何らかのDelegateMethodタイプが宣言されたオブジェクト).区別:DelegateMethodはタイプ、delegateMethodはオブジェクト.【3】DelegateMethodは関数ポインタと見なすことができるのはなぜですか.test.delegateMethod=new TestDelegateMethod.DelegateMethod(test.NonStaticMethod);test.delegateMethod+=new TestDelegategate.DelegateMethod(TestDelegate.StaticMethod); test.delegateMethod += Program.OtherClassMethod;ここでdelegateMethodには3つの関数が搭載されており、delegateMethodを呼び出すことができます.Invoke();搭載された関数を実行します.これがDelegateが関数ポインタとして見られる理由です.上記のコードではdelegateMethodは、戻り値がvoidであり、入力パラメータのない関数(NonStaticMethod,StaticMethod,OtherClassMethodの定義を参照)しか搭載できません.これはDelegteタイプ宣言に関連しています(DelegateMethodの宣言:public delegate void DelegateMethod()を参照).【4】Delegate複数のメソッドを搭載する場合、+=搭載する関数を増やすこともできるし、-=でDelegateのいずれかの関数を削除することもできる.
2.delegateはオブジェクト間で追加搭載できます.
3.delegate typeの宣言は本質的に新しいsubtype instanceを作成し、このsubtypeは派生した.NET library frameworkのabstract base classes DelegteまたはMulticastDelegteは、delegateオブジェクトまたはその搭載方法(methods)を問い合わせるためのpublic methodsのセットを提供し、関数ポインタとは異なり、依頼はオブジェクト向けであり、タイプが安全で安全である.
class Program
{
static void OtherClassMethod(){
Console.WriteLine("Delegate an other class's method");
}
static void Main(string[] args)
{
var test = new TestDelegate();
test.delegateMethod = new TestDelegate.DelegateMethod(test.NonStaticMethod);
test.delegateMethod += new TestDelegate.DelegateMethod(TestDelegate.StaticMethod);
test.delegateMethod += Program.OtherClassMethod;
test.RunDelegateMethods();
}
}
class TestDelegate
{
public delegate void DelegateMethod(); // Delegate Type
public DelegateMethod delegateMethod; // Delegate
public static void StaticMethod()
{
Console.WriteLine("Delegate a static method");
}
public void NonStaticMethod()
{
Console.WriteLine("Delegate a non-static method");
}
public void RunDelegateMethods()
{
if(delegateMethod != null){
Console.WriteLine("---------");
delegateMethod.Invoke();
Console.WriteLine("---------");
}
}
}
【1】public delegate void DelegateMethod().ここではDelegateMethodというDelegateMethodのタイプを宣言している.このDelegteタイプは、voidの戻り値であり、入力パラメータのない関数を搭載することができる.【2】public DelegateMethod DelegateMethod;ここでDelegateMethodのオブジェクトを宣言する(つまり、何らかのDelegateMethodタイプが宣言されたオブジェクト).区別:DelegateMethodはタイプ、delegateMethodはオブジェクト.【3】DelegateMethodは関数ポインタと見なすことができるのはなぜですか.test.delegateMethod=new TestDelegateMethod.DelegateMethod(test.NonStaticMethod);test.delegateMethod+=new TestDelegategate.DelegateMethod(TestDelegate.StaticMethod); test.delegateMethod += Program.OtherClassMethod;ここでdelegateMethodには3つの関数が搭載されており、delegateMethodを呼び出すことができます.Invoke();搭載された関数を実行します.これがDelegateが関数ポインタとして見られる理由です.上記のコードではdelegateMethodは、戻り値がvoidであり、入力パラメータのない関数(NonStaticMethod,StaticMethod,OtherClassMethodの定義を参照)しか搭載できません.これはDelegteタイプ宣言に関連しています(DelegateMethodの宣言:public delegate void DelegateMethod()を参照).【4】Delegate複数のメソッドを搭載する場合、+=搭載する関数を増やすこともできるし、-=でDelegateのいずれかの関数を削除することもできる.