Closures and callbacks between Java internal classes _java

Source: Internet
Author: User
Tags anonymous closure static class

Objective

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

One of the most contentious aspects of Java is that it is thought that Java should contain some sort of pointer-like mechanism to allow callbacks (callback). With callbacks, an object can carry some information that allows it to invoke the original object at a later time. If the callback is implemented by a pointer, you can only hope that the programmer will not misuse the pointer.

One, the member internal class

You can think of an inner class as a member. Member-Inner classes can unconditionally access all member properties and member methods of an external class.

Class Outterclass {//external class
 private int in = 0;
 static int inn=4;
 public outterclass (int in) {
   this.in = in;
 }
 Class Innerclass {   //internal classes public
   void output () {
     System.out.println ();
     System.out.println (INN);}}

When a member inner class has a member variable or method with the same name as an external class, the member's inner class is 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:

Outterclass (external Class). this. Member

An external class accesses an inner class, and you must first create an object of the member's inner class, and then access it by referring to the object's reference.

Class Outterclass {
 private int in = 0;
 static int inn=4;
 public outterclass (int in) {
   innerclass inner=new innerclass ();
   This.in=inner.innernum;
 }
 Class Innerclass {  //Internal classes public
   int innernum=1;
   public void output () {
     System.out.println (in);
     System.out.println (INN);
     int a=outterclass.this.inn;
   }}}

A member's inner class exists as a dependency on an external class, that is, if you want to create an object of the member's inner class, if you must have an object of an external class. The general way to create members ' inner class objects is as follows:

public class ClassA {public
 static void main () {
   outterclass oc=new outterclass (3);
   Outterclass.innerclass in=oc.new innerclass ();
 }

Second, the local internal class

A local inner class is like a local variable within a method, and cannot have public , and protected private static modifiers.

Class Outterclass {public
 outterclass (int in) {class
   Innerclass {  //local inner class
      int innernum=1;
   }
 }
}

Three, nested internal class

Nested internal classes, which are decorated static with an inner class. The static inner class declared as, does not require the connection between the inner class object and the Outer class object, which means we can refer directly outer.inner to the need not to create an external class or to create an inner class.

Class Outterclass {public
 outterclass (int in) {    
 }
 static class Innerclass {  //local inner class
   int Innernum=1
 }
}
public class ClassA {public
 static void main () {
   outterclass.innerclass in=new outterclass.innerclass ();
 }
}

Four, anonymous internal class

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

Btnsan.setonclicklistener (Newonclicklistener () {
  @Override
  Publicvoidonclick (View v) {
  }
});

V. Closures and callbacks

A closure (Closure) is an object that can be invoked to save information about the scope in which it was created. Java does not explicitly support closures, but in Java, closures can be implemented through "interface + inner classes."

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

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 ();}}}

The subclass defines an inner class that follows the programmer's interface rules, and then uses the inner class to implement the programmer's work() method callback code() method, which directly implements the parent writer's method in the subclass work() .

Vi. the role of the inner class

The inner class can be well hidden.

General non-internal classes that are not allowed private with protected permissions, but the inner class can be

Internal classes have access to all elements of the perimeter class

But implementing multiple inheritance

You can avoid modifying interfaces to implement calls to 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 or use Java can have some help, if there is doubt you can message exchange.

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.