2015-09-07 11:44:36 -06:00
|
|
|
|
// innerclasses/Parcel2.java
|
2015-11-14 16:18:05 -08:00
|
|
|
|
// <20>2016 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
|
|
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
2015-06-15 17:47:35 -07:00
|
|
|
|
// Returning a reference to an inner class.
|
|
|
|
|
|
|
|
|
|
public class Parcel2 {
|
|
|
|
|
class Contents {
|
|
|
|
|
private int i = 11;
|
|
|
|
|
public int value() { return i; }
|
|
|
|
|
}
|
|
|
|
|
class Destination {
|
|
|
|
|
private String label;
|
|
|
|
|
Destination(String whereTo) {
|
|
|
|
|
label = 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();
|
|
|
|
|
Destination d = to(dest);
|
|
|
|
|
System.out.println(d.readLabel());
|
|
|
|
|
}
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
Parcel2 p = new Parcel2();
|
|
|
|
|
p.ship("Tasmania");
|
|
|
|
|
Parcel2 q = new Parcel2();
|
|
|
|
|
// Defining references to inner classes:
|
|
|
|
|
Parcel2.Contents c = q.contents();
|
|
|
|
|
Parcel2.Destination d = q.to("Borneo");
|
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
|
}
|
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
|
Tasmania
|
2015-09-07 11:44:36 -06:00
|
|
|
|
*/
|