Java Programming Idea (vii)--inner class

Source: Internet
Author: User
Tags ticket

Just see the Forum recommendation, Ali's school recruit did not expect to start at the end of August, such as spring recruit again to try, now still too tender point.


Place the definition of one class into the inner-inner class of another class definition. Java is used to write Android friends often used listener things, inside is the inner class. And don't just think Java is the only way to develop Android.


1) Simple Inner class

The functionality of the inner class seems to hide the code.

public class Ticket {    class destination{        private String content;        Destination (String s) {            content = s;        }        String showcontent () {            return content;        }    }        public void Show (String dest) {        Destination d = new Destination (dest);        System.out.println (D.showcontent ());    }    public static void Main (string[] args) {        Ticket t = new Ticket ();        T.show ("Beijing");}    }

a simple inner class is an ordinary class nested inside.


Public Destination getdestination () {return new Destination ("Shanghai");        } public static void Main (string[] args) {Ticket t = new Ticket ();        Ticket.destination dest = T.getdestination ();        Destination d = t.new Destination ("GuangZhou");    T.show ("Beijing"); }

If you create an inner class object from a static method, if it is a direct new, use the above notation and use the return value as the object's method to indicate the type of the object.


2) Link to external class

The inner class has access to all elements of its perimeter class (enclosing class,enclosing is closed meaning that the book is translated into a peripheral class object). Use the example in the book directly.

public class Sequence {private object[] item;    private int next = 0;    public Sequence (int size) {item = new object[size];        } public void Add (Object x) {if (next<item.length) {item[next++]=x;        }} Private class Simpleitertor implements myiterator{private int index = 0;            /*public Boolean End () {if (next = item.length) {return true;        } return false;        The code redundancy can actually be written more concise */public Boolean end () {return index = = Item.length;        } public Object current () {return item[index];            } public void Next () {if (index<item.length) {index + +;    }}} public Myiterator iterator () {return new simpleitertor ();        } public static void Main (string[] args) {Sequence s = new Sequence (10); for (int i = 0; i < i++) {S.add (i);        } Myiterator i = S.iterator ();            while (!i.end ()) {System.out.println (i.current ());        I.next (); }    }}

Why write This example, one is this kind of iterator that we often use the container, in fact, we also apply the design pattern-the iterator pattern . There are also internal classes that operate with private members of the perimeter class.


3) use. This and. New

public class Outer {    class inner{        Outer getouter () {            return outer.this;}}    }

If this is a simple, this point is Outer.Inner, if return outer object, still need outer.this.


The new words were mentioned at the first point of knowledge. Need

Destination d = t.new Destination ("GuangZhou");
An inner class object is secretly connected to an external class object, so it is not possible to create an inner class until the outer class object is created, when a reference to an outer class object is not required if it is a static inner class (nested Class).

public class Outer {     static class inner{    } public     static void Main (string[] args) {        Inner i = new Inner ();    }}

4) Internal class use--upward transformation

More advanced hidden code. In fact, the book to give examples. But look back and see that it has been used once.

Private class Simpleitertor implements Myiterator{}public Myiterator iterator () {return new simpleitertor ();

In fact, we have hidden the implementation details of this implementation class in the code, the property is private, the return time is to transform the simpleiterator upward to a common interface.

5) The inner class in the method and scope.

public class Outer {public    void get () {        class inner{        }}    }

Inside a method, called a local inner class.

In terms of scope, if the inner class is declared in scope under the IF condition statement, it is not available outside the domain.


6) Anonymous Inner class

public class Outer {public    Sequence get () {        return new Sequence (1) {};}       }

Although the sequence object is returned, it is something you can define yourself in curly braces. This is anonymous for the outside.

If an anonymous inner class uses its externally defined object, the parameter must be final.

Class inner{} public class Outer {public    Inner get (final String s) {        return new Inner () {           String type = s;        };    }}

cannot refer to a non-final variable s inside an inner class defined in a different method. You cannot put a non-final variable s inside a class in a different way Defined.


7) Nested Classes

The static inner class mentioned above. Similar to the characteristics of static methods. Creating nested class objects does not require external class object references, and non-static perimeter class objects cannot be accessed from nested class objects (static methods cannot use non-static methods in static methods).


8) Why internal classes are required

This thing design to do what, want to know actually language is person design, characteristic also oneself design, useless is not specially designed.

The inner class implements interfaces that are different from the outer class implementations, and each inner class inherits the interface, and the outer class inherits who does not affect the inner class. When using the GUI, you can feel the value of the inner class more.


9) Closures and callbacks

Closure, closure, refers to a callable object that records some information from the creation of his scope. Actually, the inner class is.


10) Inner class identifier

In the time before using the IDE, it is common to see what the compiled file looks like when it is compiled purely in a DOS window. Sometimes see the existence of the Outer$inner.class file, in fact, this is the result of compiler compilation, The outer class plus $ plus the name of the inner class.



Interfaces and internal classes these two chapters are really complicated and esoteric, and there's a control framework in the inner class that I didn't write out. C + + does not have these things, the combination of interfaces and internal classes can solve the problem of multiple inheritance of C + +. Write not much, contact with the two are relatively few, later contact to see again to know the value of the.






Java Programming Idea (vii)--inner class

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.