image.png

    1. public class Parcel5 {
    2. public Destination destination(String s) {
    3. class PDestination implements Destination {
    4. private String label;
    5. private PDestination(String whereTo) {
    6. label = whereTo;
    7. }
    8. @Override
    9. public String realLabel() {
    10. return label;
    11. }
    12. }
    13. return new PDestination(s);
    14. }
    15. public static void main(String[] args) {
    16. Parcel5 p = new Parcel5();
    17. Destination d = p.destination("Tasmania");
    18. }
    19. }
    1. public class Parcel6 {
    2. private void internalTracking(boolean b) {
    3. if(b) {
    4. class TrackingSlip {
    5. private String id;
    6. TrackingSlip(String s) {
    7. id = s;
    8. }
    9. String getSlip() { return id; }
    10. }
    11. TrackingSlip ts = new TrackingSlip("slip");
    12. String s = ts.getSlip();
    13. }
    14. // 这里不能这样用,超出作用域
    15. //! TrackingSlip ts = new TrackingSlip("x");
    16. }
    17. public void track() { internalTracking(true); }
    18. public static void main(String[] args) {
    19. Parcel6 p = new Parcel6();
    20. p.track();
    21. }
    22. }