Sample source Code
In this section we will cover a more typical scenario in an internal class application: An external class will have a method that returns a reference to an inner class, as in
To ()And
contents ()method as seen in the.
Package Com.mufeng.thetenthchapter;public class Parcell2 {class Contents {private int i = 11;public int value () {return i; }}class Destination {private string Label;public Destination (String whereto) {//TODO auto-generated constructor Stublabe L = whereto;} String Readlabel () {return label;}} Public Destination to (String s) {return new Destination (s);} Public Contents Contents () {return new Contents ();} public void ship (String dest) {Contents c = Contents ();D estination d = to (dest); System.out.println (C.value ()); System.out.println (D.readlabel ());} public static void Main (string[] args) {Parcell2 p = new Parcell2 ();p. Ship ("Tasmania"); System.out.println ("--------I am the dividing line----------"); PARCELL2 q = new Parcell2 (); Parcell2.contents C = q.contents ();//(or Q.new Contents ()) parcell2.destination d = q.to ("Borneo");//(or q.new//Destinat Ion ("Borneo")) System.out.println (C.value ()); System.out.println (D.readlabel ());}}
Output results
11Tasmania--------I'm a split line----------11Borneo
SOURCE parsing
If you want to create an object of an inner class from anywhere other than the non-static method of the outer class, you must like the
Main ()method, specifically indicating the type of the object:
Outerclassname.innerclassname。
Java within the class of the primary application of the detailed (with source)