黑马程序员技术交流社区

标题: 【广州校区】+【原创】浅谈jdk8 [打印本页]

作者: yqw_gz_java    时间: 2018-1-13 14:16
标题: 【广州校区】+【原创】浅谈jdk8
本帖最后由 yqw_gz_java 于 2018-1-13 20:39 编辑

jdk8接口的改变jdk8之后接口中可以有实体方法,但是要由关键字default 修饰既次方法是默认实现如下:
[AppleScript] 纯文本查看 复制代码
package com.qw.jdk8test;
public interface defautMetahd {
        int MAX_SIZE=100;
        void println();
        /**
         * 接口中可以有默认实现
         * @param obj
         */
        default void println(Object obj) {
                System.out.println(obj);
        }

}
jdk8之前 匿名内部类访问局部变量必须使用final修饰,jdk可以不用final修饰
[AppleScript] 纯文本查看 复制代码
package com.qw.jdk8test;

public interface A {
void test();
}
//测试类  
package com.qw.jdk8test;

public class Atest {
        static int age=8;
        public static void main(String[] args) {
                new A() {
                        
                        public void test() {
                                // TODO Auto-generated method stub
                                System.out.println(age);
                        }
                }.test();
        }

}

Lamabda表达式   
lamabada 表达式支持代码块做为参数传递,要用lambda表达式要明白一个叫(函数式接口的概念:既只有一个抽象方法的接口
简单使用如下:
[AppleScript] 纯文本查看 复制代码
package com.qw.jdk8test;
/**
*
* @author 40248
* 函数式接口 (只有一个抽象方法)
*/
public interface Print {

        void printArray(int[] buf) ;
}
package com.qw.jdk8test;

public class PrintTest {
        public static void main(String[] args) {
                PrintTest printTest = new PrintTest();
                printTest.show(new int[] {2,3,4},(int[] targe)->{
                        System.out.print("[ "+targe[0]+" ,");
                                for (int i = 1; i < targe.length-1; i++) {
                                        System.out.print(targe+" ,");
                                }
                                if(targe.length-1>0) {
                                        System.out.print(targe[targe.length-1]+" ]");
                                }
                });
        }
        public void show(int[] buf,Print p) {
                p.printArray(buf);
        }

}
lambda表达式如果只有一行代码,可以省略{}
列如:
[AppleScript] 纯文本查看 复制代码
package com.qw.jdk8test;

public interface Sysout {
void show();
}
package com.qw.jdk8test;

public class TestRuanbel {

        public void run(Sysout r) {
                        r.show();
        }
        public static void main(String[] args) {
                TestRuanbel testRuanbel = new TestRuanbel();
                testRuanbel.run(()->System.out.println("zhangs"));
        }
}

如果lambda只有一个返回语句return 可以省略:
[AppleScript] 纯文本查看 复制代码
package com.qw.jdk8test;

public interface Addable {
        int add(int a,int b);
}
package com.qw.jdk8test;

public class TestAddable {

        public static void main(String[] args) {
                TestAddable testAddable = new TestAddable();
                testAddable.show(3, 4, (a,b)->a+b);
        }
        public void show (int a,int b,Addable c) {
                System.out.println(c.add(a, b));
        }
}
lambda表达式【targe type】 创建对象
[AppleScript] 纯文本查看 复制代码
                //因为 Runnable 是函数式接口 所以可以下面这样写
                Runnable rn=()->System.out.println();

lambda强制转换:
[AppleScript] 纯文本查看 复制代码
                //这样写会报错, 无法类型推导
                //Object object =()->System.out.println("张三");
                Object object =(Runnable)()->System.out.println("张三");
  
lambda 方法的引用
[AppleScript] 纯文本查看 复制代码
package com.qw.jdk8test;
@FunctionalInterface
public interface Converter {
        Integer converter(String str);

}
package com.qw.jdk8test;

public class TestConverter {

        
        public static void main(String[] args) {
                /**
                 * 方法的引用
                 */
                Converter converter = s->Integer.parseInt(s);
                Converter converter1 =Integer::parseInt;
        }
}

lambda 引用类的构造方法:
[AppleScript] 纯文本查看 复制代码
package com.qw.jdk8test;

public interface ConstructorsTest {
        Student constructos(String s);
}
package com.qw.jdk8test;
/**
* lambda 引用构造方法
* @author 40248
*
*/

public class TestGZ {

        public static void main(String[] args) {
                //引用构造方法  两种方式
                ConstructorsTest constructorsTest=(String s)->new Student(s);
                ConstructorsTest constructorsTest2= Student::new ;
        }
       
}





作者: Yin灬Yan    时间: 2018-1-14 16:28
我来占层楼啊   
作者: ggislazy    时间: 2018-1-16 19:57
本帖最后由 ggislazy 于 2018-1-16 20:10 编辑

伟哥我爱你,那是不存在的




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2