Java编程思想:内部类中级部分

public class Test {
    public static void main(String[] args) {
//        Parcel4Test.test();
//        Parcel5_1.test();
//        Parcel6.test();
//        Parcel8.test();
//        Parcel9.test();
//        AnonymousConstructor.test();
        Parcel10.test();
    }
}

/*
    当内部类向上转型为其基类,尤其是转型为一个接口时,内部类就有了用武之地。
    (从实现了某个接口的对象,得到对此接口的引用,与向上转型为这个对象的基类
    实质上效果是一样的。)这是因为此内部类——某个接口的实现——能够完全不可
    见,并且不可用。所得到的只是指向基类或者接口的引用,能方便的隐藏实现细节
 */

interface Destination{
    String readLable();
}

interface Content {
    int value();
}

class Parcel4 {
    private class PContents implements Content {
        private int i = 11;
        @Override
        public int value() {
            return i;
        }
    }

    protected class PDestination implements Destination {
        private String label;
        @Override
        public String readLable() {
            return label;
        }

        public PDestination(String label) {
            this.label = label;
        }
    }

    //这个地方就发生了向上转型
    public Destination to(String toWhere) {
        return new PDestination(toWhere);
    }

    //这个地方就发生了向上转型
    public Content contents() {
        return new PContents();
    }
}

/*
    private 内部类给类的设计者提供了一种途径,通过这种方式完全可以阻止任何依赖于类型
    的编码,并且完全隐藏了实现细节。
 */

class Parcel4Test {
    public static void test() {
        Parcel4 p = new Parcel4();
        Content c = p.contents();
        Destination d = p.to("E");

        //无法访问
        //Parcel4.PContents pc = p.new PContents();
    }
}

/*
    定义在方法中的类:
        在destination()中定义了内部类PDestination,并不代表一点destination()方法
        执行完毕了,PDestination就不可用了。

            ——如果,这个内部内访问了方法里的参数的话,会怎样呢?
 */
class Parcel5 {
    public Destination destination(String s) {

        /*
            实验:看是否方法内部的类,能不能访问方法里的数据
            结论:
                可以访问?
                    ——那这个方法消失后,这些数据会怎样呢?
         */

        int h = 100;
        class PDestination implements Destination {
            private String Label;
            private int a;

            public PDestination(String label) {
                a = h;
                Label = label;
            }

            @Override
            public String readLable() {
                return Label;
            }
        }
        return new PDestination("F");
    }

    public static void test() {
        Parcel5 p = new Parcel5();

        Destination d = p.destination("H");
    }
}

/*
    实验:
        1.定义在方法内部的内,能否访问方法里的参数
        2.方法执行完毕后,这个内部内还能访问这些参数么
        3.如果方法里的参数是基本数据类型(储存在栈中),还可以访问么

    结论:
        可以访问哦,这就有点恐怖了!这有点香Lua中的闭包了,我能理解它,但是
        这和我在C++里学的有很大的出入。
 */

class Parcel5_1 {
    public Destination destination(String s) {

        int[] arr = new int[100];
        int a = 10;

        for (int i = 0; i < 100; i++) {
            arr[i] = i;
        }

        class PDestination implements Destination {
            private String Label;
            private int i = 0;

            public PDestination(String label) {
                Label = label;
            }

            @Override
            public String readLable() {
//                return arr[i++]+"";
                return a+"";
            }
        }
        return new PDestination("F");
    }

    public static void test() {
        Parcel5_1 p = new Parcel5_1();

        Destination d = p.destination("H");

//        System.out.println("f:"+d.readLable());
//        System.out.println("s:"+d.readLable());
//        System.out.println("t:"+d.readLable());
        System.out.println("a:"+d.readLable());
        System.out.println("a:"+d.readLable());
        System.out.println("a:"+d.readLable());
    }
}

/*
    定义在作用域的类,此作用域在方法的内部
 */
class Parcel6 {
    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 test() {
        Parcel6 p = new Parcel6();
        p.track();
    }
}

/*
    实现了接口的匿名类
 */
class Parcel7 {
    public Content contents() {
        return new Content() {
            private int i = 11;
            @Override
            public int value() {
                return i;
            }
        };
    }

    public static void test() {
        Parcel7 p = new Parcel7();
        Content c = p.contents();
    }
}

/*
    一个匿名类,它扩展了有非默认构造器的类

    你的基类需要一个有参数的构造器,应该怎么办?
 */
class Wrapping {
    private int i;

    public Wrapping(int x) {
        this.i = x;
    }

    public int value() {
        return i;
    }

}
class Parcel8 {
    public Wrapping wrapping(int x) {
        return new Wrapping(x){
            public int values() {
                return super.value() * 47;
            }
        };
    }

    public static void test() {
        Parcel8 p = new Parcel8();
        Wrapping w = p.wrapping(10);
        System.out.println("w.value():"+w.value());
    }
}

/*
    一个匿名类,它执行字段的初始化

    如果定义一个匿名内部类,并希望它使用一个在外部定义的对象,那么编译器会要求其参数的引用
    是final的,就像你在destination()中看到的那样。
 */
class Parcel9 {
    public Destination destination(final String dest) {
        return new Destination() {
            private String label = dest;
            @Override
            public String readLable() {
                return label;
            }
        };
    }

    //实际上,我没有传入一个final值,编译器没有报任何错误,时代不一样了
    public Destination destination2(String dest) {
        return new Destination() {
            private String label = dest;
            @Override
            public String readLable() {
                return label;
            }
        };
    }

    public static void test() {
        Parcel9 p = new Parcel9();
        Destination d1 = p.destination("CCC");
        Destination d2 = p.destination2("HHH");
        System.out.println("d1:"+d1.readLable());
        System.out.println("d2:"+d2.readLable());
    }
}

/*
    一个匿名类,它通过实例初始化实现构造(匿名类不可能有构造器)

    需求:
        如果想做一些类似的构造器的行为,该怎么办?在匿名类中不可能有命名的构造器
        (因为它根本没有名字!),但通过实例初始化,就能够达到为匿名内部类创造一
        个构造器的效果
 */
abstract class Base {
    public Base(int i) {
        System.out.println("Base constructor i = " + i);
    }

    public abstract void f();
}

class AnonymousConstructor {
    public static Base getBase(int i) {
        return new Base(i) {
            {
                System.out.println("Inside instance initializer.");
            }
            @Override
            public void f() {
                System.out.println("In anonymous f()");
            }
        };
    }

    public static void test() {
        Base base = getBase(47);
        base.f();
    }
}

class Parcel10 {
    public Destination destination(final String dest, final float price) {
        return new Destination() {
            private int cost;
            {
                cost = Math.round(price);
                if (cost > 100) {
                    System.out.println("Over budget!");
                }
            }
            private String label = dest;

            @Override
            public String readLable() {
                return label;
            }
        };
    }

    //测试不用final关键字会怎样
    public Destination destination2(String dest,float price) {
        return new Destination() {
            private int cost;
            {
                cost = Math.round(price);
                if (cost > 100) {
                    System.out.println("Over budget!");
                }
            }
            private String label = dest;

            @Override
            public String readLable() {
                return label;
            }
        };
    }

    public static void test() {
        Parcel10 p = new Parcel10();
        Destination d1 = p.destination("AAA", 10.f);
        Destination d2 = p.destination2("BBB", 11.f);
    }
}

猜你喜欢

转载自www.cnblogs.com/junjie2019/p/10544580.html
今日推荐