Preface
A closure is a callable object that records some information from the scope in which it was created. Through this definition, it can be seen that the inner class is an object-oriented closure, because it not only contains information about the peripheral class object (the scope of creating the inner class), but also automatically has a reference to the peripheral class object. In this role, the inner class has the right to operate all members, including private members.
One of the most controversial issues in Java is that it is believed that Java should include some pointer-like mechanism to allow callbacks. Through the callback, the object can carry some information that allows it to call the initial object at some later point. If the callback is implemented through a pointer, then you can only hope that the programmer will not misuse the pointer.
1. Internal class of members
A inner class can be regarded as a member. Member inner class can unconditionally access all member properties and member methods of external classes.
class OutterClass {//External class private int in = 0; static int inn=4; public OutterClass(int in) { this.in = in; } class InnerClass { //Inner class public void output() { System.out.println(in); System.out.println(inn); } }}When a member internal class owns a member variable or method with the same name as an external class, members of the member internal class are accessed by default. If you want to access a member of the same name of an external class, you need to access it in the following form:
OutterClass(external class).this.member
When an external class accesses an internal class, you must first create an object of the member internal class, and then access it through a reference to this object.
class OutterClass { private int in = 0; static int inn=4; public OutterClass(int in) { InnerClass inner=new InnerClass(); this.in=inner.innerNum; } class InnerClass { //Inner class public int innerNum=1; public void output() { System.out.println(in); System.out.println(inn); int a=OutterClass.this.inn; } }}Member internal classes exist based on external classes, that is, if you want to create an object of the member internal class, the premise is that an object of the external class must exist. The general way to create a member internal class object is as follows:
public class class class { public static void main(){ OutterClass oc=new OutterClass(3); OutterClass.InnerClass in=oc.new InnerClass(); }} 2. Local internal categories
A local inner class is like a local variable in a method, and cannot have public , protected , private , and static modifiers.
class OutterClass { public OutterClass(int in) { class InnerClass { // Local inner class int innerNum=1; } }} 3. Nested internal classes
Nested inner classes are internal classes modified as static . An inner class declared as static does not require a connection between the inner class object and the outer class object, that is, we can directly refer to outer.inner , that is, we do not need to create an outer class or an inner class.
class OutterClass { public OutterClass(int in) { } static class InnerClass { // Local inner class int innerNum=1; }} public class class { public static void main(){ OutterClass.InnerClass in=new OutterClass.InnerClass(); }} 4. Anonymous internal category
Anonymous internal classes are the ones we use most because we don’t want to give them names, so we have anonymity. Anonymous internal classes need to be defined in advance.
btnSan.setOnClickListener(newOnClickListener() { @Override publicvoidonClick(View v) { }}); 5. Closures and callbacks
A closure is an object that can be called, which saves information about the scope that creates it. JAVA cannot explicitly support closures, but in JAVA, closures can be implemented through "interface + inner class".
For example: an interface programmer and a base class writer have the same method work and the same method name, but its meaning is completely different. At this time, a closure is required.
class Writer {//Writer base class void work(){};}interface programmer{//Programmer interface void work();} The closure implementation code is as follows:
public class WriterProgrammer extends Writer { @Override public void work(){ //Writing} public void code(){ //Write code} class ProgrammerInner implements programmer{ @Override public void work(){ code(); } }} In the subclass, an internal class that follows the programmer's interface rules is defined, and then the internal class implements the programmer's work() method callback code() method, and directly implements work() method of the parent class writer in the subclass.
6. The role of internal categories
Internal classes can be well implemented to hide.
Generally, non-internal classes do not allow private and protected permissions, but internal classes can
The inner class has access to all elements of the peripheral class
But multiple inheritance is achieved
It can avoid modifying the interface and implementing calls of two methods of the same name in the same class.
7. Summary
The above is the entire content of this article. I hope it will be of some help to everyone learn or use Java. If you have any questions, you can leave a message to communicate.