Root U3D-about all aspects of Delegate, u3d-delegate

Source: Internet
Author: User

Root U3D-about all aspects of Delegate, u3d-delegate

 

Do I need to read this article?

Code1:

1 private delegate void BloodNumDelegate ();2 3 public delegate void ExpNumChangeDelegate (int _currentExpNum);4 5 public event ExpNumChangeDelegate mOnExpNumChangeDelegate;

Code2:

PlayerVoProxy. instance. mOnExpNumChangeDelegate + = delegate (int _ expNum) {Console. WriteLine ("a received, ExpNum is: {0}", _ expNum );};

If you can clearly understand the meaning of the above two sections of code, it means that you already know Delegate (at least better than me :)),

So I think the following article may not be of great help to you. However, if you have some doubts in some places, I hope you can get some help from my article below.

 

What content does this article contain?

This article starts with the most basic Delegate introduction and will be extended to event, anonymous Delegate and my personal understanding of its use.

Articles to be read in advance

If you have no idea about Delegate as I did, read this article first.

NET beauty:. NET key technology in-depth analysis. NET beauty:. NET key technology in-depth analysis of the Mini Book in Chapter 3rd (C # in the Commission and events ).

(The author's understanding of Delegate and Its depth cannot match the following :()

What is Delegate?

Starting from the observer Mode

The observer mode is certainly not unfamiliar to everyone.

using System;using System.Collections;namespace L1{    public class FakeBloodChangeDelegate    {        public ArrayList mAllFakeDelegateListener;        public FakeBloodChangeDelegate ()        {            mAllFakeDelegateListener = new ArrayList ();        }        public void addListener (IFakeBloodNumChangeDelegateListener _listener)        {            mAllFakeDelegateListener.Add (_listener);        }        public void upldateListener ()        {            foreach (IFakeBloodNumChangeDelegateListener listener in mAllFakeDelegateListener)            {                listener.onBloodNumChange ();            }        }    }}
 
using System;namespace L1{    public interface IFakeBloodNumChangeDelegateListener    {        void onBloodNumChange ();    }}
 
using System;namespace L1{    public class Idiot : IFakeBloodNumChangeDelegateListener    {        public Idiot ()        {        }        public void onBloodNumChange ()        {            Console.WriteLine ("Idiot --> onBloodNumChange");        }    }}

using System;namespace L1{    public class TestArea1    {        private FakeBloodChangeDelegate mFakeBloodChangeDelegate;        private Idiot mIdiot;        public TestArea1 ()        {        }        public void run ()        {            mFakeBloodChangeDelegate = new FakeBloodChangeDelegate ();            mIdiot = new Idiot ();            mFakeBloodChangeDelegate.addListener (mIdiot);            mFakeBloodChangeDelegate.upldateListener ();        }    }}

This is a standard observer. idiot is the listener. When the subject (FakeBloodChangeDelegate) changes, the updateListener method is called to notify all listener objects.

OK. Let's perform a simple encapsulation on this FakeBloodChangeDelegate class and create a Player Object for it. Blood is part of the Player.

using System;namespace L1{    public class FakePlayerVoProxy    {        public FakeBloodChangeDelegate bloodNumChangeListner;        private int mCurrentBloodNum = 0;        public FakePlayerVoProxy ()        {            bloodNumChangeListner = new FakeBloodChangeDelegate ();        }        public void addPlayerBlood (int _addNum)        {            mCurrentBloodNum += _addNum;            bloodNumChangeListner.upldateListener ();        }    }}

Therefore, the call in the main class and change:

using System;namespace L1{    public class TestArea1    {        private FakePlayerVoProxy mPlayerVo;        private Idiot mIdiot;        public TestArea1 ()        {        }        public void run ()        {            mPlayerVo = new FakePlayerVoProxy ();            mIdiot = new Idiot ();            mPlayerVo.bloodNumChangeListner.addListener (mIdiot);            mPlayerVo.addPlayerBlood (10);        }    }}

Run the code to get the same output as above. After all, it is actually a simple encapsulation of FakeBloodChangeDelegate without changing any code.

If the above example can be fully understood, let's continue.

Delegate is a class.

This is actually a very important piece of information, which has been clearly stated in the mini book mentioned above. the reason why I wrote this code in a poor way is to illustrate it.

In the above example, FakeBloodChangeDelegate, including the corresponding Listener, can be simplified to a line of code.

public delegate void BloodNumChangeDelegate ();

Leave public aside, and use delegate void BloodNumChangeDelegate () later. This is equivalent to telling the editor to create

FakeBloodChangeDelegate and IFakeBloodNumChangeDelegateListener, after reading the subsequent content, you can go back and read the mini book I mentioned above, which is detailed)

using System;namespace L1{    public class FakePlayerVoProxy    {        public delegate void BloodNumChangeDelegate ();        public BloodNumChangeDelegate bloodNumChangeListner;        private int mCurrentBloodNum = 0;        public FakePlayerVoProxy ()        {        }        public void addPlayerBlood (int _addNum)        {            mCurrentBloodNum += _addNum;            bloodNumChangeListner ();        }    }}

Note:

Public delegate void BloodNumChangeDelegate ();This lineIndicates that the FakeBloodChangeDelegate class has been created.And FackPlayerVoProxyThis class does not existAny link,

You can create a file, name it "Apple", and put this line in the Apple class. However, when you create an object based on this class (Delegate), you cannot write

Public BloodNumChangeDelegate bloodNumChangeListner;

To write

Public Apple. BloodNumChangeDelegate bloodNumChangeListner;

 

My personal understanding about Delegate

 

Because it is written AS (ActionSprite), it is no stranger to the callback function, and Delegate can be understood AS the callback function in C #. The only thing that will be confusing is

public delegate void BloodNumChangeDelegate ();public BloodNumChangeDelegate bloodNumChangeListner;

The core of the two lines of code is to understand the Delegate, which tells the compiler to generate a class that implements the observer mode for you.

 

About the Public modifier before Delegate

Previously, when I explained how Delegate was, I intentionally ignored the class modifier public. this article is actually quite informative. for Delegate or regression, the three most important functions of the observer mode should be

AddListener, removeListener, updateListener

The idea of OO is to encapsulate things that external listeners should only be able to access addListener, removeListener, and upldateListner should only be able to do things of the main class.

Let's first return to the example before implementing Delegate and continue to use FakeBloodChangeDelegate,

To meet the preceding requirements, you only need to change FakeBloodChangeDelegate to private and provide the corresponding public function.

Using System; namespace L1 {public class FakePlayerVoProxy {private parameter mBloodNumChangeListner; private int mCurrentBloodNum = 0; public FakePlayerVoProxy () {mBloodNumChangeListner = new parameter ();} public void addListener (IFakeBloodNumChangeDelegateListener _ listener) {mBloodNumChangeListner. add (_ listener);} public void removeListener (listener _ listener) {// Remove code} public void addPlayerBlood (int _ addNum) {mCurrentBloodNum + = _ addNum; bloodNumChangeListner. upldateListener ();}}}

This should be easy to understand, right? Therefore, if you use Delegate to perform the same operation, you can write it as follows:

using System;namespace L1{    public class FakePlayerVoProxy    {        public delegate void BloodNumChangeDelegate ();        private BloodNumChangeDelegate bloodNumChangeListner;        private int mCurrentBloodNum = 0;        public FakePlayerVoProxy ()        {        }        public void addListener (?? _listener)        {            bloodNumChangeListner += _listener;        }                    public void addPlayerBlood (int _addNum)        {            mCurrentBloodNum += _addNum;            bloodNumChangeListner ();        }    }}

You will find that you don't know how to write it here in addListener (it can be written, but if you understand how to write it, you don't have to continue reading it ), what type should this _ listener Be?

 

Use Event keywords

In the recommended Mini Book (what ?! Not yet...) I have already understood this. To encapsulate Delegate, only add, remove, but not provide external services.

Update method, but if it is not written as public, it cannot be accessed externally, and the keyword "Event" is generated.

using System;namespace L1{    public class FakePlayerVoProxy    {        public delegate void BloodNumChangeDelegate ();        public event BloodNumChangeDelegate bloodNumChangeListner;        private int mCurrentBloodNum = 0;        public FakePlayerVoProxy ()        {        }        public void addPlayerBlood (int _addNum)        {            mCurrentBloodNum += _addNum;            bloodNumChangeListner ();        }    }}

 

using System;namespace L1{    public class TestArea1    {        private FakePlayerVoProxy mPlayerVo;        private Idiot mIdiot;        public TestArea1 ()        {        }        public void run ()        {            mPlayerVo = new FakePlayerVoProxy ();            mIdiot = new Idiot ();            mPlayerVo.bloodNumChangeListner += mIdiot.onBloodNumChange;            mPlayerVo.addPlayerBlood (10);        }    }}

If you call mPlayerVo. bloodNumChangeListner () in TestArea1 ();

Will Receive

Error (14,14): Error CS0070: The event 'l1. fakePlayerVoProxy. bloodNumChangeListner 'Can only appear on the left hand side of + = or-= when used outside of the type' L1. fakePlayerVoProxy '(CS0070) (L1)

That is to say, the method "updateListener" cannot be called outside the class, which conforms to the OO encapsulation principle.

 

Conclusion

 

At this point, I think you should have a preliminary understanding of the use of Delegate and Event. As long as you try multiple times in the project, you should have a good grasp of it.

(In fact, I have been in contact with C # for less than a week, so I can understand it and hope to help you .)

 

Wait! What is the code in Code2?

PlayerVoProxy. instance. mOnExpNumChangeDelegate + = delegate (int _ expNum) {Console. WriteLine ("a received, ExpNum is: {0}", _ expNum );};

In fact, this code can be understood as an anonymous function (actually an anonymous delegate)

For example

private void fun1(){   dummyBtn.onClick+=fun2}private void fun2(){   //Fun2Code}

However, if the code of fun2 is very simple or for other reasons, you can use anonymous delegation, that is, the above method.

private void fun1(){   dummyBtn.onClick+=delegate()   {       //Fun2Code   }}

Most buttons and Tween operations are willing to do this. However, I personally do not like to use anonymous delegation (in As3, it is an anonymous function.

 

 Cannot I use private BloodNumChangeDelegate bloodNumChangeListner?

using System;namespace L1{    public class FakePlayerVoProxy    {        public delegate void BloodNumChangeDelegate ();        private BloodNumChangeDelegate bloodNumChangeListner;        private int mCurrentBloodNum = 0;        public FakePlayerVoProxy ()        {        }        public void addListener(BloodNumChangeDelegate _listener)        {            bloodNumChangeListner += _listener;        }        public void addPlayerBlood (int _addNum)        {            mCurrentBloodNum += _addNum;            bloodNumChangeListner ();        }    }}

 

using System;namespace L1{    public class TestArea1    {        private FakePlayerVoProxy mPlayerVo;        private Idiot mIdiot;        public TestArea1 ()        {        }        public void run ()        {            mPlayerVo = new FakePlayerVoProxy ();            mIdiot = new Idiot ();            mPlayerVo.addListener (mIdiot.onBloodNumChange);            mPlayerVo.addPlayerBlood (10);        }    }}

The above two pieces of code can be compiled and run. This is what I suddenly think of as the Delegate modifier for anonymous delegation, but I don't understand this very well. If someone knows the reason, please explain it to me.

The related content may be related to the following: Delegate, Action, Func, anonymous method, anonymous Delegate, and the content mentioned in this article.


That's All

Eran.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.