【Java】-NO.16.EBook.4.Java.1.004-【疯狂Java讲义第3版 李刚】- 内部类

1.0.0 Summary

Tittle:【Java】-NO.16.EBook.4.Java.1.004-【疯狂Java讲义第3版 李刚】- 内部类

Style:EBook

Series:Java

Since:2017-09-04

End:....

Total Hours:...

Degree Of Diffculty:2

Degree Of Mastery:2

Practical Level:2

Desired Goal:2

Archieve Goal:....

Gerneral Evaluation:...

Writer:kingdelee

Related Links:

http://www.cnblogs.com/kingdelee/

package com.lee.test.java.ebook.crazy_java.u_6_oop_2.c_6_7_inner_test;

// 1. 内部类提供了更好的封装
// 2. 在内部类,访问内部类非静态变量用 this.var,访问外部类非静态变量用 Out.this.var
// 3. 在外部类,访问内部类非静态变量用 new Inner().var
// 4. 在外部类,静态的方法不能调用非静态类
// 5. 在非静态内部类,不能存在静态声明
// 6. 外部类访问静态内部类的静态成员,StaticInner.staticVar
// 6.1 外部类访问静态内部类的非静态成员,new StaticInner().var
// 7. 静态内部类无法访问外部类的非静态成员
// 8. 在外部类的外部构造外部类的内部类实例 Out.In in = new Out().new In();
// 8.1 等价于 Out.In in1; Out out = new Out();  in1 = out.new In();
// 9. 子类继承外部类的非静态内部类时,子类构造器必须显式的调用父类的构造器
// 9.1 子类可以直接继承外部类的静态内部类,故优先使用静态内部类会更加方便
// 10. 在外部类的外部构造静态内部类实例 Out.In in = new Out.In();
// 10.1 等价于 Out.staticIn staticIn1; staticIn1 = new Out.staticIn();
// 11. 局部内部类,即定义在方法中的类,Out$NInner.class规则生成class文件:Cow.class Cow$1Inner.class Cow$1InnerSub.class
// 12.1 匿名内部类lambada无参版,接口必须是有唯一抽象方法(类方法、默认方法随意),即函数式接口 @FunctionInterface 下同
// 12.2 匿名内部类lambada有参版,@FunctionInterface
// 12.3  匿名内部类lambada有参版 有返回值 缩略return,@FunctionInterface
// 12.4 匿名内部类lambada有参版,有返回值 显式return,@FunctionInterface
// 12.5 匿名
// 12.6 将实现函数式的接口强转
// 12.7 常见的函数式接口 java.util.function
//   xxxFunction, apply() 处理 数值 并返回新值
// xxxConsumer, accept() 处理 新值 不返回值
// xxxPredicate, test() 判断 返回boolean
// xxxSupplier, getAsXxx() 按某种逻辑算法 返回值
// 12.8.1 Lambada 未缩减 调用类方法
// 12.8.1.1 Lambada ::方法调用 省略参数,引用类方法 Converter converter1_1 = Integer::getInteger;
// 12.8.2 Lambada 未缩减 调用实例 Converter converter2 = str -> "".indexOf("");
// 12.8.2.1 Lambada ::方法调用 省略参数,引用实例方法 Converter converter2_1 = str -> "".indexOf("");
// 12.8.3 Lambada 未缩减 调用实例 MyTest converter3 = (a1, b, c) -> a1.substring(b, c);
// 12.8.3.1 Lambada ::方法调用 省略参数,第一个参数为调用者,之后的参数为形参传进来 MyTest converter3_1 = String::substring;
// 12.8.4 Lambada ::new 构造参数 Converter converter4 = Integer::new;

public class Cow {

    private int weight;
    private String color = "white";

    public Cow(int weight) {
        this.weight = weight;
    }

    // 内部类提供了更好的封装
    private class CowLeg {

        private int num;
        private String color = "black";

        // 5.1 在非静态内部类,不能存在静态声明
//        static{
//
//        }

        // 5.2  在非静态内部类,不能存在静态声明
//        private static void t1(){
//
//        }

        // 5.3  在非静态内部类,不能存在静态声明
//        private static int a;

        public CowLeg(int num) {
            this.num = num;
        }

        public void printLeg() {
            System.out.println("this cow weight:" + weight + ", has leg:" + num);
            // 2. 在内部类,访问内部类非静态变量用 this.var,访问外部类非静态变量用 Out.this.var
            System.out.println("leg color:" + this.color + ", cow color:" + Cow.this.color);
        }

    }

    // 静态内部类
    private static class Life {
        private static int num = 5;
        private int age;

        static {
            // 7 编译失败,静态内部类无法访问外部类的非静态成员
//            System.out.println(Cow.this.color);
        }

    }

    public void getLife() {
        // 6. 外部类访问静态内部类的静态成员,StaticInner.staticVar
        System.out.println(Life.num);
        // 6.1 外部类访问静态内部类的非静态成员,new StaticInner().var
        System.out.println(new Cow(1).color);
    }

    public void getLegInfo(int num) {
        new CowLeg(num).printLeg();
        // 3. 在外部类,访问内部类非静态变量用 new Inner().var
        System.out.println("inner color:" + new CowLeg(1).color);
    }

    public static void main(String[] args) {
        new Cow(10).getLegInfo(2);

        // 4. 编译失败,外部类的静态方法,无法调用非静态的内部类
        //new CowLeg(1);

        // 8. 在外部类的外部构造非静态内部类实例 Out.In in = new Out().new In();
        Out.In in = new Out().new In();
        // 8.1 等价于 Out.In in1; Out out = new Out();  in1 = out.new In();
        Out.In in1;
        Out out = new Out();
        in1 = out.new In();

        // 10 在外部类的外部构造静态内部类实例 Out.In in = new Out.In();
        Out.staticIn staticIn = new Out.staticIn();
        // 10.1 等价于
        Out.staticIn staticIn1;
        staticIn1 = new Out.staticIn();

        // 11. 局部内部类,即定义在方法中的类,Out$NInner.class规则生成class文件:Cow.class Cow$1Inner.class Cow$1InnerSub.class
        class Inner {

        }

        class InnerSub extends Inner {

        }

        // 12. 匿名内部类
        new Cow(1).showFace(new Face() {
            @Override
            public void show() {
                System.out.println("show Cow face");
            }
        });

        // 12.1 匿名内部类lambada无参版,接口必须是有唯一抽象方法(类方法、默认方法随意),即函数式接口 @FunctionInterface 下同
        new Cow(1).showFace(() -> System.out.println("show Cow face"));

        // 12.2 匿名内部类lambada有参版,@FunctionInterface
        new Cow(1).drop(num -> System.out.println("drop egg:" + num));

        // 12.3  匿名内部类lambada有参版 有返回值 缩略return,@FunctionInterface
        Wallet2 w = (int banknote, int coin) -> banknote + coin;

        // 12.4 匿名内部类lambada有参版,有返回值 显式return,@FunctionInterface
        Wallet wallet = (int[] bb, int[] cc) -> {
            int count = 0;
            for (int b : bb) {
                count += b;
            }
            for (int c : cc) {
                count += c;
            }
            return count;
        };

        int[] banknotes = {1, 2, 3};
        int[] coins = {1, 2, 3};
        int count1 = new Cow(1).count(wallet, banknotes, coins);

        // 12.5 匿名  Runnable r = () -> {};
        Runnable r = () -> {
        };

        // 12.6 将实现函数式的接口强转
        Object a = (Runnable) () -> {
        };

        // 12.7 常见的函数式接口 java.util.function
        //   xxxFunction, apply() 处理 数值 并返回新值
        // xxxConsumer, accept() 处理 新值 不返回值
        // xxxPredicate, test() 判断 返回boolean
        // xxxSupplier, getAsXxx() 按某种逻辑算法 返回值

        // 12.8.1 Lambada 未缩减 调用类方法
        Converter converter1 = str -> Integer.getInteger(str);
        // 12.8.1.1 Lambada ::方法调用 省略参数,引用类方法
        Converter converter1_1 = Integer::getInteger;
        // 12.8.2 Lambada 未缩减 调用实例
        Converter converter2 = str -> "".indexOf("");
        // 12.8.2.1 Lambada ::方法调用 省略参数,引用实例方法
        Converter converter2_1 = str -> "".indexOf("");
        // 12.8.3 Lambada 未缩减 调用实例
        MyTest converter3 = (a1, b, c) -> a1.substring(b, c);
        // 12.8.3.1 Lambada ::方法调用 省略参数,第一个参数为调用者,之后的参数为形参传进来
        MyTest converter3_1 = String::substring;
        // 12.8.4 Lambada ::new 构造参数
        Converter converter4 = Integer::new;

    }

    public void showFace(Face face) {
        face.show();
    }

    public void drop(Egg egg) {
        egg.show(2);
    }

    public int count(Wallet wallet, int[] banknotes, int[] coins) {
        return wallet.countMoney(banknotes, coins);
    }

}

class Out {

    class In {
        public In() {
            System.out.println("in");
        }
    }

    static class staticIn {
        public staticIn() {
            System.out.println("staticIn");
        }
    }

}

// 9. 子类继承外部类的非静态内部类时,子类构造器必须显式的调用父类的构造器
class Sub extends Out.In {
    public Sub(Out out) {
        out.super();
    }
}

// 9.1 子类可以直接继承外部类的静态内部类,故优先使用静态内部类会更加方便
class Sub2 extends Out.staticIn {

}

@FunctionalInterface
interface Face {
    void show();
}

@FunctionalInterface
interface Egg {
    void show(int num);
}

@FunctionalInterface
interface Wallet {
    int countMoney(int[] banknotes, int[] coins);
}

@FunctionalInterface
interface Wallet2 {
    int countMoney(int banknote, int coin);
}

@FunctionalInterface
interface Converter {
    Integer convert(String str);
}

@FunctionalInterface
interface MyTest {
    String test(String a, int b, int c);
}

  

猜你喜欢

转载自www.cnblogs.com/kingdelee/p/7529176.html
今日推荐