Why Internal classes are needed in Java programming ideas?

Source: Internet
Author: User

Taking part in the Java programming ideology-profound and complex

13,Why Internal classes?

1 ). the most attractive reason for internal classes is that each internal class can independently inherit from one (Interface) implementation, so no matter whether the peripheral class has inherited a certain implementation (Interface, internal classes are not affected.

2) A class implements two interfaces in some way. Because of the flexibility of interfaces, you have two options: Use a single class or use an internal class. From the perspective of implementation, there is no difference between the two methods.

interface A{}interface B{} class X implements A, B{} class Y implements A{   B makeB(){      return new B(){              };   }} public class MultiInterfaces{   static void takesA(A a){}   static void takesB(B b){}   public static void main(String[] args){      X x = new X();      Y y = new Y();      takesA(x);      takesA(y);      takesB(x);      takesB(y.makeB());   }}

3). If you haveAbstract class or specific class, not InterfaceThenOnly internal classes can be usedTo achieve multi-inheritance.

4). The internal class can also obtain other features (Not very understandable !!):

·Internal classes can have multiple instances, each of which has its own status information, and is independent of the Information of peripheral class objects.

·In a single peripheral class, multiple internal classes can implement the same interface in unnecessary ways, or inherit the same class.

· The creation of internal class objects does not depend on the creation of peripheral class objects.

· The internal class has no confusing "is-a" relationship; it is an independent entity.

See the following example:

interface Selector{   boolean end();   Object current();   void next();}public class Sequence{   private Object[] objects;   private int next = 0;   public Sequence(int size){      objects = new Object[size];   }   public void add(Object x){      if(next < objects.length)        objects[next++] = x;   }   private class SSelector implements Selector{      private int  i = 0;      public boolean end(){        return i == objects.length;      }      public Object current(){        return objects[i];      }      public void next(){        if(i < objects.length)           i++;      }   }   public Selector getSelector(){      return new SSelector();   }   public static void main(String[] args){      Sequence sequence = new Sequence(10);      for(int i = 0; i < 10; i++)        sequence.add(Integer.toString(i));      Selector selector = sequence.getSelector();      while(!selector.end()){        System.out.println(selector.current());        selector.next();      }   }}

If sequence does not apply to internal classes, you must declare"Sequence is a selector", Only one selector can be used for a specific sequence. At the same time, it is easy to use an internal class to have another method getrselector (), and use it to generate a selector for reverse traversal. This is only available for internal classes.Flexibility.

14,ClosureIt is a callable object that records some information from the scope of its creation.The internal class is an object-oriented closure.. Because it not only contains information about peripheral class objects ("create internal class scope"), it also automatically has a reference pointing to this peripheral object, within this scope, all members of the internal class operation, includes "private" members.

15. One of the most controversial issues of Java is that Java should include a pointer-like mechanism to allow callback. PassCallbackThe object can carry some information, which allows it to call the initial object at a certain time later.

Providing closures through internal classes is a perfect solution, which is more flexible and safer than pointers. See the following example:

Interface incrementable {void increment () ;}// external class implementation Interface Class callee1 implements incrementable {private int I = 0; Public void increment () {I ++; system. out. println (I) ;}} class myincrement {void increment () {system. out. println ("myincrement. increment () ");} static void func (myincrement) {myincrement. increment () ;}} class callee2 extends myincrement {private int I = 0; private void incr () {I ++; system. out. println (I) ;}// internal class implementation interface public class closureimplements incrementable {public void increment () {incr () ;}} incrementable getcallbackreference () {return new closure (); // upward transformation} class caller {private incrementable callbackreference; caller (incrementable callbackreference) {This. callbackreference = callbackreference;} void go () {callbackreference. increment () ;}} public class callbacks {public static void main (string [] ARGs) {callee1 C1 = new callee1 (); callee2 C2 = new callee2 (); myincrement. func (C2); caller caller1 = new caller (C1); caller caller2 = new caller (c2.getcallbackreference (); caller1.go (); caller1.go (); caller2.go (); caller2.go ();}}

Callee2 is inherited from myincrement. There is an increment () method with the same name as the incrementable interface, but the increment () method behavior of the two is different. Therefore, if callee2 inherits myincrement, it cannot overwrite the increment () method for the purpose of incrementable. Therefore, it can only implement incrementable using internal classes independently. Note that when an internal class is createdYou have not added anything to the interface of the peripheral class or modified the interface of the peripheral class.

The getcallbackreference () method in closure of the internal class in callee2 returns an incrementable reference. No matter who obtains this reference, only increment () can be called (), there are no other functions (unlike pointers, you can do a lot of things ).

The value of callback lies in itsFlexibility-- You canDynamic Decision during runtimeThe method to call.

16,

1 ).Application Framework (applicationframeword)Is a class or group of classes designed to solve a specific problem.

2) to run an application framework, it generally inherits one or more classes and overwrites a method.

3 ).Control FrameworkIs a special application framework used to solveRESPONSE event.

4) The system mainly used to respond to events is calledEvent-driven system.

Click here to see an example to better understand the value of the internal class.

The above content is compiled from Java programming ideas. If any omission exists, please leave it blank!

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.