Examples of Java internal classes: linking to external classes

Source: Internet
Author: User
Tags int size

So far, the inner class we've seen seems to be just a name-hiding and code-organizing scenario. Although these features are useful, they do not seem particularly compelling. However, we have also overlooked another important fact. When you create your own inner class, the object of that class also has a link to the encapsulated object that encapsulates or generates an inner class. So they can access the members of that packaged object-no qualification required. In addition, the inner class has access to all elements of the encapsulated class (annotation ②). The following example illustrates this problem:
 

//: Sequence.java//holds a Sequence of Objects interface Selector {Boolean end ();
  Object current ();
void Next ();
  public class Sequence {private object[] o;
  private int next = 0;
  public Sequence (int size) {o = new object[size];
      public void Add (Object x) {if (Next < O.length) {O[next] = x;
    next++;
    } Private class Sselector implements Selector {int i = 0;
    public Boolean end () {return i = = O.length;
    Public Object current () {return o[i];
    public void Next () {if (I < o.length) i++;
  } public Selector Getselector () {return new sselector ();
    public static void Main (string[] args) {Sequence s = new Sequence (10);
    for (int i = 0; i < i++) S.add (integer.tostring (i));    
    Selector SL = S.getselector ();
      while (!sl.end ()) {System.out.println ((String) sl.current ());
    Sl.next (); }
  }
} ///:~


②: This is quite different from the design of C + + "nested Classes", which is just a simple name-hiding mechanism. In C + +, there is no link to a encapsulated object, and no default access rights exist.

Where sequence is just a fixed-size array of objects, and one class encapsulates it internally. We call Add () to add a new object to the end of the sequence (if there is a place). To get each object in the sequence, use an interface called selector, which enables us to know if we are at the end (the) and can view the current object And the next object (next ()) that can be moved to the sequence. Since selector is an interface, many other classes can implement interfaces in their own way, and many methods can use the interface as an argument to create generic code.
Here, Sselector is a private class that provides selector functionality. In main (), you can see the sequence creation process, followed by the addition of a series of string objects. Subsequently, a selector is generated by a call to Getselector (). and use it to move through the sequence, selecting each item at the same time.
From the surface, Sselector seems to be just another inner class. But don't be fooled by superficial phenomena. Notice the end (), current (), and Next (), each of which references an O. O is a handle that is not part of the sselector, but rather a private field in the encapsulated class. However, internal classes can access methods and fields from the encapsulated class, just as they already have them. This feature is very convenient for us, as seen in the example above.
Therefore, we now know that an inner class can access members of the encapsulated class. How did this happen? An inner class must have a reference to a specific object that encapsulates the class, and the purpose of the encapsulated class is to create the inner class. Then, when we reference a member of the encapsulated class, we use that (hidden) reference to select that member. Luckily, the compiler will help us take care of all these details. But we can now understand that an object of an inner class can only be created jointly with an object that encapsulates the class. During this creation, a handle to the encapsulated class object is required to be initialized. If you cannot access that handle, the compiler will make an error. For all of these operations, no programmer intervention is required for most of the time.

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.