C # comparison with Java 2)

Source: Internet
Author: User
C # comparison with Java (to 2)

5. Delegate)
This is a very special thing. It is a bit like a pointer, but not completely, but you can still think of it as a type of safe, object-oriented pointer. (You don't need to talk about what is type security and object-oriented ?) By the way, many books have translated delegate into a proxy. I think it is more appropriate to translate it into a "Reference", which is in line with the truth, it also conforms to its original meaning-Microsoft originally used delegate to "replace Pointer", so it is called ".

Sun may still be angry with it. Why? This is not in Sun's standard Java. It is a "new feature" added by msvj ++ 6 released by Microsoft in 99 ". To this end, the two companies were so happy that they wrote a lot on the Internet.ArticleAttack each other. If you are interested, you can check it out (only in English ).
Http://www.Javasoft.com/docs/white/delegates.html
Http://msdn.microsoft.com/visualj/technical/articles/delegates/truth.asp

What are the characteristics of the original saying? An obvious characteristic is that it has pointer behavior, as if from Java back to C ++. In C #, the complete function is roughly equivalent to the pointer in C ++ and the interface in Java. However, it is more advanced than C ++'s "authentic Pointer" because it can have multiple methods at the same time, which is equivalent to the pointer in C ++ pointing to multiple functions at the same time, it is also of type security, which reflects its "object" feature. Compared with Java interfaces, it is better to say that it can call functions without passing through internal classes, or use a fewCodeMultiple functions can be called, which reflects its "Pointer" feature. Haha, is it very "wave-particle two-Elephant? The most important application of is event processing. We will focus on this in the next section.

6. Events)

C # events are directly supported (this feature is also available in msvj ). Many mainstreamProgramThe language handles events in different ways. Delphi uses function pointers (the term "closure" in Delphi ") java uses adapted classes for implementation and VC uses windowsapi message systems, while C # directly uses delegate and event keywords to solve this problem. Let's take a look at the example below. We will give you the whole process of declaring, calling, and processing events.

// First, it refers to the declaration, which defines the event signal to wake up a function.
Public Delegate void scorechangeeventhandler (INT newscore, ref bool cancel );

// Define an event-generating class> public class game
{
// Note that the event keyword is used here
Public event scorechangeeventhandler scorechange;
Int score;
// Score attributes
Public int score
{
Get {
Return score;
}
Set {
If (score! = Value)
{
Bool cancel = false;
Scorechange (value, ref cancel );
If (! Cancel)
Score = value;
}
}
}

// Event handling class
Public class referee
{
Public referee (game)
{
// The referee is responsible for adjusting the score changes in the competition
Game. scorechange + = new scorechangeeventhandler (game_scorechange );
}

// Note how the function matches the signal of scorechangeeventhandler.
Private void game_scorechange (INT newscore, ref bool cancel)
{
If (newscore $ # @ 60; 100)
System. Console. writeline ("Good score ");
Else
{
Cancel = true;
System. Console. writeline ("No score can be that high! ");
}
}
}

// Main function class, used to test the above features
Public class gametest
{
Public static void main ()
{
Game = new game ();
Referee referee = new referee (game );
Game. Score = 70;
Game. Score = 110;
}
}

In the main function, we create a game object and a referee object. Then, we change the game score to observe what the referee will respond.

Please note that in our system, the game object does not feel the existence of the referee object. The game object is only responsible for generating events here. As for who will listen to this event, in response, the game object does not make any comments.

We noticed that in the referee function of the referee class, the + = and-= operators are used after game. scorechange. What does this mean? Return to the location where scorechange is defined and we can find that scorechange is modified with the event keyword, so the meaning here is clear: scorechange is an event, after an event is triggered, a corresponding event processing mechanism is required. + =/-= adds/removes the corresponding event processing program for the event, not an event can only correspond to one processing program. We can also use these two operators to add/remove several event processing programs for the same event. How is it? Very convenient!

In practical applications, a system that is very similar to the competition-Referee mechanism we mentioned above is a graphical user interface system. A game object can be viewed as a small part in the graphic interface, and a score event is equivalent to a user input event, while a referee is equivalent to an application for processing user input.

The first appearance of the reference mechanism was in msvj. It was invented by Anders hejlsberg and is now used in C. The consequences of the reference in the Java language directly lead to a lot of debate and discussion on the relationship between classes and pointers between Microsoft and sun. Interestingly, Java inventor James Gosling humorously calls the inventor Anders hejlsberg as "Mr. function pointer ", because Anders hejlsberg is always trying to put pointers in disguise in various languages; however, after someone has read a wide range of Java applications, james Gosling, the inventor of Java, is also known as "Mr. All classes". It's really a taste of it.

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.