C # and Java Comparisons (ii)

Source: Internet
Author: User
Tags bool interface reference
Compare C # to Java comparison (turn two)


5. Reference (Delegate)
Refers to this thing is very special, it is a bit like a pointer, but not completely, but you can still see it as a type of safe, object-oriented pointers. (What is type safety and object oriented?) By the way, there are a lot of books on the translation of delegate into agents, I think this is not accurate, translated into "refer to" more appropriate, the truth is consistent, and also in line with its original meaning-Microsoft is to use delegate to "replace the pointer", so called "reference", hehe.

Speaking of the reference, perhaps even so far sun will be angrily to it, why? Because there is no such thing in Sun's standard Java, it is the "new feature" added by Microsoft's 99 msvj++6. To this end, two companies have a great deal of noise, and also dedicated to write a large number of articles on the Internet to attack each other, interested friends can go to see (only English version).
Http://www.Javasoft.com/docs/white/delegates.html
Http://msdn.microsoft.com/visualj/technical/articles/delegates/truth.asp

What are the characteristics of the reference? An obvious feature is that it has a pointer behavior, as if from Java back to C + +. In C #, the function of the reference is probably the same as the pointers in C + + and the interfaces in Java. However, the reference is more sophisticated than the "authentic pointers" of C + +, because it can have multiple methods at the same time, the equivalent of C + + pointer can point to multiple functions, and is type-safe, which embodies its "object" characteristics, compared to the Java interface, The point is that it can invoke a function without having to go through the inner class, or invoke multiple functions with a small amount of code, which embodies its "pointer" attribute. Oh, there is a "wave-particle duality" flavor it? The most important application of the reference is the handling of the event, which we will focus on in the next section.

6, Events (event)

C # is directly supported for events (this feature is also MSVJ). At present, many mainstream program language processing events are different, Delphi is the function of the pointer (this in the Delphi term is "closure"), Java with the adaptation class to achieve, VC with WINDOWSAPI message system, and C # The delegate and event keywords are used directly to solve the problem. Let's take a look at an example that gives you the whole process of declaring, invoking, and handling events.


The first is the declaration of a reference, which defines the event signal that wakes a function
public delegate void Scorechangeeventhandler (int newscore, ref bool cancel);

Defines a class > public class Game that generates events
{
Note that the event keyword is used here
public event Scorechangeeventhandler Scorechange;
int score;
Score Property
public int Score
{
get {
return score;
}
set {
If (score!= value)
{
bool Cancel = false;
Scorechange (value, ref cancel);
if (! Cancel)
Score = value;
}
}
}


Classes that handle events
public class referee
{
Public referee (Game Game)
{
The referee is responsible for adjusting the score changes in the game
Game. Scorechange + = new Scorechangeeventhandler (game_scorechange);
}

Notice how the function here is the number of the scorechangeeventhandler signal.
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 so high!");
}
}
}

Main function class for testing the above characteristics
public class Gametest
{
public static void Main ()
{
Game 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, and then we see how the referee responds to this by changing the score of the game.

Please note that in our system, the game object does not feel the presence of the referee, the game object is only responsible for generating events here, as for who will listen to the event and respond to it, game object is not to make any statement.

We notice that in the referee function of the referee class, what does it mean to use the + = and-= operators after Game.scorechange? Back to where we define scorechange, we can see that scorechange is decorated with the event keyword, so the meaning here is clear: Scorechange is an event, and events are triggered, and the corresponding event handling mechanism is required, +=/-= is to add/remove the corresponding event handler for this event, and not one event can only correspond to one handler, we can also add/remove several event handlers for the same event with these two operators. What do you think? It's very convenient!

In practical applications, the system that is similar to the one we are talking about (contest-referee) mechanism is the graphical user interface system. The game object can be seen as a small part of the graphical interface, and a score event is the equivalent of a user input event, and the referee is the equivalent of an application that handles user input.

The first appearance of the anaphora mechanism was in MSVJ, which was invented by Anders Hejlsberg and is now used in C #. The consequences of substitution in the Java language have led to a great deal of controversy and discussion about the relationship between Microsoft and sun on classes and pointers. Interestingly, the inventor of Java, James Gosling, is very humorous to address the inventor of the Anders Hejlsberg as "' Mr. Function pointer ', because Anders Hejlsberg always find ways to put the pointer in various languages But someone who has seen a lot of the use of various classes in Java, also jokingly called Java inventor James Gosling as "' All Is class ' Sir", is really the taste, do not say in the.

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.