A description of closures and callbacks between classes within Java

Source: Internet
Author: User
Tags closure

Objective

A closure (closure) is a callable object that records information from the scope in which it was created. With this definition, you can see that the inner class is an object-oriented closure because it contains not only information about the perimeter class object (the scope of the inner class), but also a reference to the object of the perimeter class, within which the inner class has permission to manipulate all members, including private members.

One of the most controversial questions in Java is that Java should contain some kind of pointer-like mechanism to allow callbacks (callback). With callbacks, an object can carry some information that allows it to invoke the initial object at a later point in time. If the callback is implemented by pointers, then you can only hope that the programmer will not misuse the pointer.

First, members of the internal class

An inner class can be considered a member. Member inner classes have unconditional access to all member properties and member methods of an external class.

?
12345678910111213 class OutterClass {//外部类 private int in = 0; static int inn=4; public OutterClass(int in) {   this.in = in; } class InnerClass {   //内部类   public void output() {     System.out.println(in);     System.out.println(inn);   } }}

When a member inner class has a member variable or method with the same name as an external class, the members of the member's inner class are accessed by default. If you want to access a member of the same name as an external class, you need to access it in the following form:

?
1 OutterClass(外部类).this.成员

An external class accesses an inner class and must first create an object of a member's inner class and then access it through a reference to the object.

?
12345678910111213141516 class OutterClass { private int in = 0; static int inn=4; public OutterClass(int in) {   InnerClass inner=new InnerClass();   this.in=inner.innerNum; } class InnerClass {  //内部类   public int innerNum=1;   public void output() {     System.out.println(in);     System.out.println(inn);     int a=OutterClass.this.inn;   } }}

A member inner class exists that is dependent on an external class, that is, if you want to create an object of the member's inner class, as long as an object of an outer class must exist. The general way to create a member's inner class object is as follows:

?
123456 public class classa {   public static void main () {     outterclass oc= new outterclass ( 3     outterclass.innerclass In=oc. new innerclass ();   } }

Second, local internal class

A local inner class is like a local variable inside a method, and it cannot have a, public protected ,, or a private static modifier.

?
1234567 class outterclass {   public Code class= "Java Plain" >outterclass ( int in) {     class innerclass {  //local inner class         int innernum= 1       } }

Third, nested inner class

Nested inner static classes are the inner class that is decorated. The inner class that is declared static , does not require a connection between the inner class object and the Outer class object, that is, we can refer directly, that is outer.inner , we do not need to create an external class, nor do we need to create an inner class.

?
123456789101112 class OutterClass { public OutterClass(int in) {     } static class InnerClass {  //局部内部类   int innerNum=1; }}public class classa { public static void main(){   OutterClass.InnerClass in=new OutterClass.InnerClass(); }}

Iv. Anonymous Internal classes

Anonymous inner class is the most we use, because we don't want to give it a name, so we have anonymity. Anonymous inner classes need to be defined in advance.

?
12345 btnSan.setOnClickListener(newOnClickListener() {  @Override  publicvoidonClick(View v) {  }});

V. Closures and callbacks

A closure (Closure) is an object that can be called, which holds information about the scope in which it was created. Java does not explicitly support closures, but in Java, closures can be implemented through "Interface + internal classes".

For example: an interface programmer and a base class writer all have the same method work , the same method name, but the meaning is completely different, this time need closure.

?
123456 classWriter {//作家基类 void work(){};}interface programmer{//程序员接口 voidwork();}

The closure implementation code is as follows:

?
123456789101112131415 public class WriterProgrammer extends Writer { @Override public void work(){   //写作 } public void code(){   //写代码 } class ProgrammerInner implements programmer{   @Override   public void work(){     code();   } }}

An inner class that follows the programmer interface rules is defined in the subclass, then implements the programmer's method callback method using the inner class, and implements the method of the work() code() parent writer directly in the subclass work() .

Vi. Role of internal classes

Inner classes can be well-implemented to hide.

Generic non-intrinsic classes are not allowed private with protected permissions, but inner classes can be

Inner class has access to all elements of the perimeter class

But implementing multiple inheritance

You can avoid modifying an interface and implementing the invocation of two methods of the same name in the same class.

Vii. Summary

The above is the entire content of this article, I hope to learn from you or use Java can have a certain help, if there are questions you can message exchange.

A description of closures and callbacks between classes within Java

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.