关于java:java8新特性

一.外部类

java外部类总结:

https://segmentfault.com/a/1190000013386064

外部类演示:

public class Demo{
    
    private String str = "外部类中的字符串";
    
    static class Inner{
        private String inStr = "外部类中的字符串";
        public void print() {
            // 动态外部类无奈调用 ,非动态的内部参数。
            //System.out.println(str);
            System.out.println("外部类打印");
        }
    }
    
    // 
    public void fun() {
        // 在外部类 创立外部类对象
        Inner i = new Inner();
        i.print();
    }
    
}

匿名外部类:

public static void test() {
        // 外部类,1---n的累加
        class XXx{
            public void sum(int n) {
                int count = 0;
                for (int i = 1; i <= n; i++) {
                    count = count + i;
                    System.out.println(count);
                }
            }
        }
        XXx x = new XXx();
        x.sum(100);
    }

lambda表达式:

把代码变的更加简略。可读性比拟差。 scala( spark ).

1、简化匿名外部类的编写。

2、间接实现接口中的函数。

3、函数名 (参数列表)

4、函数实现用”->” 示意实现。{}示意实现的具体逻辑。

5、用接口去申明应用。

6、用申明的变量调用实现 的办法。
例如:
没有参数,间接返回。这样的函数 () -> 6; 参数列表 -> 语句块;

//实现内部接口
interface B{
    void b(String str);
}

// 5、接管字符串对象,并在控制台打印。
        new B() {
            public void b(String s) {
                System.out.println(s);
            }
        }.b("hello");

能够简写为:
两种写法:

        // () -> {}; 等于   () -> o;
        B b5 = (String str) -> {System.out.println(str);};
        B b5_1 = (s) -> System.out.println(s);
        b5.b("moring");
        b5_1.b("hello");

办法的援用:

::,使代码更加紧凑简洁。

List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        //list.forEach((String b) -> System.out.print(b));
        //list.forEach(e -> System.out.println(e));
        //简写为上面一种办法
        // 办法援用
        list.forEach(System.out::println);

java8流式操作stream:

https://segmentfault.com/a/1190000020266327

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理