- public interface Contents {
- int value();
- }
- public interface Destination {
- String readLabel();
- }
- public class Goods {
- private class Content implements Contents {
- private int i = 11;
- public int value() {
- return i;
- }
- }
- protected class GDestination implements Destination {
- private String label;
- private GDestination(String whereTo) {
- label = whereTo;
- }
- public String readLabel() {
- return label;
- }
- }
- public Destination dest(String s) {
- return new GDestination(s);
- }
- public Contents cont() {
- return new Content();
- }
- }
- class TestGoods {
- public static void main(String[] args) {
- Goods p = new Goods();
- Contents c = p.cont();
- Destination d = p.dest("Beijing");
- }
- }
- public class Goods {
- private int valueRate = 2;
- private class Content implements Contents {
- private int i = 11 * valueRate;
- public int value() {
- return i;
- }
- }
- protected class GDestination implements Destination {
- private String label;
- private GDestination(String whereTo) {
- label = whereTo;
- }
- public String readLabel() {
- return label;
- }
- }
- public Destination dest(String s) {
- return new GDestination(s);
- }
- public Contents cont() {
- return new Content();
- }
- }
- public class Goods1 {
- public Destination dest(String s) {
- class GDestination implements Destination {
- private String label;
- private GDestination(String whereTo) {
- label = whereTo;
- }
- public String readLabel() {
- return label;
- }
- }
- return new GDestination(s);
- }
- public static void main(String[] args) {
- Goods1 g = new Goods1();
- Destination d = g.dest("Beijing");
- }
- }
- public class Goods2 {
- private void internalTracking(boolean b) {
- if (b) {
- class TrackingSlip {
- private String id;
- TrackingSlip(String s) {
- id = s;
- }
- String getSlip() {
- return id;
- }
- }
- TrackingSlip ts = new TrackingSlip("slip");
- String s = ts.getSlip();
- }
- }
- public void track() {
- internalTracking(true);
- }
- public static void main(String[] args) {
- Goods2 g = new Goods2();
- g.track();
- }
- }
- public class Goods3 {
- public Contents cont() {
- return new Contents() {
- private int i = 11;
- public int value() {
- return i;
- }
- };
- }
- }
- frame.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e){
- System.exit(0);
- }
- });
// Creating a constructor for an anonymous inner class.
import static net.mindview.util.Print.*;
abstract class Base {
public Base(int i) {
print("Base constructor, i = " + i);
}
public abstract void f();
}
public class AnonymousConstructor {
public static Base getBase(int i) {
return new Base(i) {
{ print("Inside instance initializer"); }
public void f() {
print("In anonymous f()");
}
};
}
public static void main(String[] args) {
Base base = getBase(47);
base.f();
}
} /* Output:
Base constructor, i = 47
Inside instance initializer
In anonymous f()
*///:~
//: innerclasses/Parcel9.java
// An anonymous inner class that performs
// initialization. A briefer version of Parcel5.java.
public class Parcel9 {
// Argument must be final to use inside
// anonymous inner class:
public Destination destination(final String dest) {
return new Destination() {
private String label = dest;
public String readLabel() { return label; }
};
}
public static void main(String[] args) {
Parcel9 p = new Parcel9();
Destination d = p.destination("Tasmania");
}
} ///:~