Getting started from C # to Java 3-internal class

Source: Internet
Author: User

C # differs greatly from Java in understanding the static keyword, mainly in the internal class. In addition, because JAVA does not have a data structure similar to delegate, internal classes must take important responsibilities such as encapsulation methods and response events.

Getting started with C # to Java-Basic Types and strings
Getting started from C # To JAVA -- class
Getting started from C # to Java 3-internal class

Completely different internal classes

Unlike C #, Java does not allow external classes to be modified using the static keyword. Let's take a look at an example of modifying the internal class with the static keyword (from Java).

 Public   Class  Circle {  Private   Static   Int  Radius;  Public   Static   Class Center {  //  Static internal classes can also declare static members.          Private   Static  Center defaultcenter;  Static  {Defacenter Center = New Center (10, 10 );  //  Access a private member of an external class  System. Out. println (RADIUS );}  Public  Int  X, Y;  Public Center ( Int X, Int  Y ){  This . X = X;  This . Y = Y ;}}}  Public   Class  Main {  Public   Static  Void  Main (string [] ARGs ){  //  Static internal classes can exist independently of external instances. Circle. Center center = New Circle. Center (15, 20 );}} 

Those who are used to C # Will be afraid and confused. Obviously, this internal class is static. Why is there a constructor ?!
This is caused by different interpretations of static in two languages:
In C #, static modifies the internal class, indicating that this is a static class that cannot be instantiated. in Java, this indicates that this internal class can be instantiated without relying on external class instances.

Therefore, the static internal class in Java is equivalent to a common internal class in C.
So what does an internal class without static modification mean? Naturally, internal classes that depend on external class instances can be instantiated!

 Public   Class  Circle { Private   Int  Radius;  Public   Class  Center {  //  Common internal classes can also declare static members.  //  Private Static center defaultcenter;  //  But it can contain static constants.          Private   Static   Final   Int Final_static = 10 ;  Public   Int  X, Y;  Private   Int  Radius;  Public Center ( Int X, Int  Y ){  This . X = X;  This . Y =Y; radius = 3; //  Access members of internal classes              This . Radius = 3; //  This points to the internal class Circle. This . Radius = 3; //  Circle. This points to an external class  }}}  Public   Class  Main {  Public   Static  Void  Main (string [] ARGs ){  //  The common internal class must depend on the instance of the external class.  //  Circle. Center center = new circle. Center (15, 20 );  //  You must first create an external class instance Circle = New  Circle (); circle. Center = Circle. New Center (15, 20 );}} 

The row where the internal class instance is createdCodeIs it awkward?
That's right. If inheritance is involved, for example, inheriting the internal class of a class, it is even more complicated. So my suggestion is that non-static internal classes are not needed if they are not needed.

Local classes declared in the Method

Common internal classes can be declared in statement blocks (such as methods), that is, local classes.
Local classes can only be accessed in the current statement block.
Local classes can access local variables in the statement block (but must be modified using final)And Members in the external class.

More common anonymous classes

If a local class is used only once, it can be declared as an anonymous class. The example also comes from Java:

 Interface  Center {}  Public   Class  Circle {  Public Center getcenter ( Final  Int Pointx, Final   Int  Pointy ){  Return   New  Center (){  Public   Int  X, Y; {x = Pointx; y = Pointy ;}};}}  Public   Class Main {  Public   Static   Void  Main (string [] ARGs) {circle = New  Circle (); Center = Circle. getcenter (10, 15 );}} 

Anonymous classes cannot declare constructor methods, so they can only initialize members by initializing blocks.
The anonymous class is not the type after new: if new is followed by a class, the type of the anonymous class is the subclass of this class; if new is followed by an interface, the type of the anonymous class is an implementation of this interface.

For example, we often add a handler to the activity to process various messages. The activity of each activity is different, so there is no need to reuse it. Use an anonymous class:

PrivateHandler mhandler =NewHandler () {@ overridePublic VoidHandlemessage (Message MSG ){//...}};

The anonymous class is used for encapsulation.

In many cases, we need to treat the method as a variable and pass it to another method. C/C ++ can use function pointers, and C # can use delegation. Java does not have a more intuitive method, but generally it can be done as follows:
1. Create the ifunc interface and put a method prototype, such as void func ();
2. Create a funcclass that implements ifunc and implement the func method;
3. Create the funcobject object of the funcclass class and pass it to other methods.

It is troublesome. Fortunately, there is an anonymous class, which can combine the creation class and the Creation object. For example, if you want to put a piece of code in a new thread to run, you have to put the code into trouble. Fortunately, there is an anonymous class that combines the creation class and the Creation object.
For example, to put a piece of code into a new thread to run, you need to create the runnable interface implementation, plug the code into the run method, and pass it to the thread constructor:

Thread thread =NewThread (NewRunnable () {@ overridePublic VoidRun (){//...}); Thread. Start ();

Note: in actual use, you only need to directly rewrite the thread run method, which is actually a detour.

To implement this method, people will put the method to be passed in a class, then create an object using this class, and implement the transfer method by passing this object. At this time, the anonymous class can be used.

Anonymous class and Event Response Mechanism

In many cases, we need to treat the method as a variable and pass it to another method. C/C ++ can use function pointers, while C # can use delegation, but Java does not have a more intuitive way. Generally, in Java code, people put the methods to be passed in a class, and then use this class to create an object, and pass this object to implement the transfer method. At this time, the anonymous class can be used.

The simplest example is the response button click event:

Mbutton. setonclicklistener (NewView. onclicklistener () {@ overridePublic VoidOnclick (view ){//...}}

Static Internal interface by default

The interface declared inside the class, that is, the internal interface.
The internal interface is static by default-this is easy to understand. Since it is an interface, is it still dependent on instances of external classes.

 

 

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.