Brief analysis of some use and carding of Java inner class

Source: Internet
Author: User

========================================================
Qiujuer
Blog:Blog.csdn.net/qiujuer
Website:Www.qiujuer.net
Open Source Library:Genius-android
Reprint Please specify Source: http://blog.csdn.net/qiujuer/article/details/43282699

--open source of learning, for open source, beginner's mentality, with June mutual encouragement!
========================================================

Order

There is this article, purely coincidental, that day in use suddenly found in the Java inner class also divided static , to tell the truth is usually used, but it is not noticed, it feels necessary to summarize.

It is necessary to say that this article is purely analysis, if there are supplementary also please point out in the comments, welcome to summarize.

The location of the inner class

public class A {class B {}public void pint () {class C {}new C ();} public void Pint (Boolean b) {if (b) {class D {}new D ();}}}
As can be seen from the code, inner classes can be defined in many places, often in member variables (B), the method is also called the local inner class (C), in scope (D)

From the above, it seems that there is no use in the method and scope of the situation ah, this is wrong; take a look at this:

public interface Ainterface {void Show (); public class B {public void Show () {class Man implements Ainterface {@Overridepublic void Show () {}}man man = new Man (); MA N.show ();}}
Where we define two files, one is an interface class, and the other is Bthe document; BClass, the Show ()method, we created the class in the way we used the local inner class MansMans ClassInherit the interface and implement the method, and then use the class.

Why do internal classes have internal class permissions?

In my opinion, the main type is encapsulation, inheritance, polymorphism , of course, the callback thought is very important, and the appearance of the inner class is to simplify the problem of multiple inheritance, a class a , and can not inherit many other classes, However, in the use of other classes to use the method, this time the inner class will play a role, the typical event click Callback implementation.

So what are the permissions on the inner classes?

As for what the answer is, look at the code to see it.

public class C {int a = 1;private int b = 2;protected int C = 3;public int d = 4;void A () {System.out.println ("A:" + a);} private void B () {System.out.println ("B:" + b);} protected void C () {System.out.println ("C:" + C);} public void D () {System.out.println ("D:" + D);} Class D {void Show () {int max = a + B + C + d;a (); B (); C ();d(); System.out.println ("Max:" + max);}} public static void Main (string[] args) {d d = new C (). New D ();d. Show ();}
Operation Result:


As you can see, the inner class D has full access to class C, which is equal to the whole body stripped to you.

What if it's the opposite?

public class C {class D {private int a = 20;private void A () {System.out.println ("D.A:" + A);}} void Show () {d d = new D ();d. A (); System.out.println ("D.A:" + d.a);} public static void Main (string[] args) {new C (). Show ();}}
Operation Result:


Visible is also completely feasible, but also direct access to private property private methods, where it seems that the private restrictions have been invalidated general, this reminds me of the previous saw an interview: when the private decoration in Java will expire.

It's a total of two people stripped of each other.

Anonymous inner class

This is very common, especially in the button click event Bindings.

public class D {void Initbutton () {button B1 = new button (); B1.setonclicklistener (new Onclicklistener () {@Overridepublic V OID OnClick (Button v) {}}); Button B2 = New button (); B2.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (Button v) {}});}}
of which:

    New Onclicklistener () {@Overridepublic void OnClick (Button v) {}}
is the use of anonymous internal classes, Onclicklistener is an interface class, interface classes can not be directly new to an instance; it's not like that, it's a new class. The class is anonymous, without a name, except that the class implements the method in the Onclicklistener interface class.

The Add callback section above can be equivalent to:

public class D {void InitButton1 () {button B1 = new Button (); B1.setonclicklistener (new Listener1 ()); Button B2 = New button (); B2.setonclicklistener (new Listener2 ());} Class Listener1 implements Onclicklistener {@Overridepublic void OnClick (Button v) {}}class Listener2 implements Onclickl Istener {@Overridepublic void OnClick (Button v) {}}}
This is the first to create a class, inherit from the interface, and then assign the value to ButtonIn

To say the difference between the two and the benefits, this actually depends on the specific use of it, if you have a lot of buttons, but in order to avoid the creation of too many classes, you can create a callback class, and then all assigned to all buttons, but in the end it is necessary in the OnClick method to make a decision is the click of the button.

Anonymous internal classes use a lot of places, the specific use should depend on the use of the situation ~

static inner class/Static nested class

This is not really supposed to be called an inner class, because it does not have the full permission of the inner class, and it is basically the same as the general class in use; Why does this exist?

It seems to me that the existence of this class is for its inclusion as a service, meaning that it can be served alone, not by the outside class, as in this case:

public class E {private void Show () {new A ();} private static Class a{}}
Which class AUsed a Static, so is Static Nested Classes, which is used here PrivateThe class can only be Eclass, and cannot be instantiated in other files.

Is it possible to use the outside class for such a situation? Can't you? You might say that you can create a A.java in the E.java folder and use protected to modify it, but it's also accessible in the same package, or in the inherited class, which is just one of the more special cases.

Let's take a look at permissions

public class E {int a1 = 0;private int a2 = 0;protected int a3 = 0;public int a4 = 0;private void Show () {A A =new a (); System.out.print ("B1:" +A.B1); System.out.print ("B2:" +A.B2); System.out.print ("B3:" +a.b3); System.out.print ("B4:" +a.b4);} private static class A{int B1 = 0;private int b2 = 0;protected int b3 = 0;public int b4 = 0;private void print () {System.ou T.print ("A1:" +A1); System.out.print ("A2:" +A2); System.out.print ("A3:" +A3); System.out.print ("A4:" +a4);}}
What's the result in this?


As can be seen from the picture, its permission level is single-direction, static nested class A is completely transparent to its containing class E, but E is not transparent to A.

Take a look at the method:


Can see the same situation; Why is it that more than one static modification is so completely different? It is well understood that two separate classes are not intended to be used directly and must have references to invoke their properties and methods.

We may be able to adjust this just OK:

public class E {int a1 = 0;private int a2 = 0;protected int a3 = 0;public int a4 = 0;private void Show () {A A = new A (); System.out.print ("B1:" + a.b1); System.out.print ("B2:" + a.b2); System.out.print ("B3:" + a.b3); System.out.print ("B4:" + a.b4); a.b1 (); A.b2 (); a.b3 (); a.b4 ();}  void A1 () {}private void A2 () {}protected void A3 () {}public void A4 () {}private static class A {int b1 = 0;private int B2 = 0;protected int b3 = 0;public int b4 = 0;void B1 () {}private void B2 () {}protected void B3 () {}public void B4 () {}priva te void print (e e) {System.out.print ("A1:" + e.a1); System.out.print ("A2:" + e.a2); System.out.print ("A3:" + e.a3); System.out.print ("A4:" + e.a4); e.a1 (); e.a2 (); e.a3 (); e.a4 ();}}
In its static class, pass a ECan solve the problem by referring to it:


It can be seen that there is no error, and it can run normally.

The hidden difference between the two

But what about the inner class at the beginning? Is it haunted? The inner class above does not pass the reference, so why not add a static ?

Here we need to look at the bytecode, we first build a simple inner class:

public class F {class a{}}
Is this simple enough? Don't say it's hard, sweat ~

Then we find the class file and then we look at the byte code:

Here we see the byte code of class F and the bytecode of the f$a class respectively.

There is one such sentence: final F this$0; This sentence is very important, where this sentence appears in its inner class, meaning that when you new an inner class, you pass the current class in at the same time, so you can have full permissions for the current class in the inner class. , you can use everything directly, because you have passed the current class in the hidden case.

Then let's look at a simple static inner class:

public class G {static class A {}}
The only difference from the above is the addition of a Static。 At this point we look at the byte code:



It can be seen that either the G class, or the initialization of the g$a class, there are no other redundant parts, and there is no hidden passed into the current class, so this situation does not have access, we need to pass the reference into the It can also be passed through the interface completely, depending on the individual. Therefore, the inner class of the static class is the same as the normal class, except that it is more open to the class than the normal classes (and contains the class), so it should be called a static nested class.

Differences in initialization

In a class that contains both internal and static internal classes, what should be the initialization?

Are they direct new ? Or look at the code:

public class H {int a = 1;public Class A {public void Show () {System.out.print ("A:" + A);}} public static class B {public void Show (H h) {System.out.print ("A:" + H.A);}} public static void Main (string[] args) {h h = new H ();//a a = new A (); A a1 = H.new A (); b b = new B ();//b B1 = h.new B (); B B3 = new h.b ();}}
The two methods that are commented on are not allowed, that is, they do not work correctly.

a because there is a hidden reference, you must be an instance of H to initialize class a , whereas class B is a class that exists statically in the H class, so you need a new H.B (); a direct use of new B ()is related to the main method in the H class, since it is already in class H , so use h directly  The static property or method of a class can be added without: "H." In front.

Inheritance of inner classes

Cases of direct inheritance:


Can see the error, why? Because we need to pass an H class in, we need to show the indication when we inherit:

public Class I extends h.a{public I (H h) {h.super ();}}
That is, in the construction method, a reference to an H is passed in, and the H instance's super () method is called to instantiate.
You should use this:

public static void Main (string[] args) {        h h = new H ();        I i = new I (h);    }

However, if you inherit its static nested class, you do not need this:

public class J extends h.b{}
Just so it's OK.

Ah, almost ~ ~ the whole inner class of things is almost these, wrote me 3个小时42分钟 ~ Khan!!!!

If there is no place to write, please add ~ ~

The wrong place also please correct me ~ ~


========================================================
Qiujuer
Blog:Blog.csdn.net/qiujuer
Website:Www.qiujuer.net
Open Source Library:Genius-android
Reprint Please specify Source: http://blog.csdn.net/qiujuer/article/details/43282699

--open source of learning, for open source, beginner's mentality, with June mutual encouragement!
========================================================

Brief analysis of some use and carding of Java inner class

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.