Java新特性

2017-04-24  本文已影响19人  嘉美伯爵

Java5新特性

可变参数

package practice;
public class change {
    public static void main(String []args){
        System.out.println(add(new int[] {1,2,3}));
        System.out.println(add(new int[] {10,30}));
    }
    public static int add(int data[]){   //接收
        int num = 0;
        for(int x=0;x<data.length;x++){
            num += data[x];
        }
        return num;
    }
}

package practice;

public class change {
    public static void main(String []args){
        System.out.println(add(new int[] {1,2,3}));
        System.out.println(add(new int[] {10,30}));
    }
    public static int add(int ... data){   //接收
        int num = 0;
        for(int x=0;x<data.length;x++){
            num += data[x];
        }
        return num;
    }
}
//运行结果一致,可以接收数组。

foreach循环

package practice;
public class change {
    public static void main(String []args){
        int data[] = new int [] {1,2,3,4,5};
        for(int x : data){
            System.out.println(x);
        }
    }
}

静态导入

引入:

package cn.fovegage.util;

public class MyMath {
    public static int add(int x,int y){
        return x+y;
    }
    public static int div(int x,int y){
        return x/y;
    }
}
-----------------------------------------------
package cn.fovegage.practice;

import cn.fovegage.util.MyMath;

public class Demo {
    public static void main(String []args){
        System.out.println("加法操作:"+MyMath.add(10,20));
        System.out.println("加法操作:"+MyMath.div(10,2));
    }
}

静态导入:

package cn.fovegage.util;

public class MyMath {
    public static int add(int x,int y){
        return x+y;
    }
    public static int div(int x,int y){
        return x/y;
    }
}
-----------------------------------------------
package cn.fovegage.practice;

import static cn.fovegage.util.MyMath.*;

public class Demo {
    public static void main(String []args){
        System.out.println("加法操作:"+add(10,20));
        System.out.println("加法操作:"+div(10,2));
    }
}

Java8新特性

接口定义的增强(可以在父类用static或default定义普通类)

default定义普通类

package cn.fovegage.Demo;

interface IMessage{
    public void print();
    default void fun(){
        System.out.print("default操作!");
    }
}
class MessageImpl implements IMessage{
    public void print(){
        System.out.println("Hello World!!!");
    }
}
public class JouKou {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        IMessage i = new MessageImpl();
        i.fun();
    }

}

static定义的操作(static定义的方法可以直接用类调用)

package cn.fovegage.Demo;

interface IMessage{
    public void print();
    default void fun(){
        System.out.println("default操作!");
    }
    static void funa(){
        System.out.println("static操作!");
    }
}
class MessageImpl implements IMessage{
    public void print(){
        System.out.println("Hello World!!!");
    }
}
public class JouKou {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        IMessage i = new MessageImpl();
        i.fun();
        IMessage.funa();
    }

}

Lamda表达式

匿名内部类(简化代码)

package cn.fovegage.Demo;

interface IMessagea{
    public void print();
}
public class Lamda {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        fun(new IMessagea(){
            public void print(){
                System.out.println("Heelo World!!!");
            }
        });
    }
    public static void fun(IMessagea im){
        im.print();
    }
}

Lamda表达式

三种表达形式
package cn.fovegage.Demo;

interface IMessagea{
    public void print();
}
public class Lamda {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        fun(() -> System.out.println("Heelo World!!!"));
        
    }
    public static void fun(IMessagea im){
        im.print();
    }

}

有参单行输出

package cn.fovegage.Demo;

interface IMessagea{
    public void print(String str);
}
public class Lamda {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        fun((s) -> System.out.println(s));
        
    }
    public static void fun(IMessagea im){
        im.print("Hello World!!!");
    }

}

多行语句

package cn.fovegage.Demo;

interface IMessagea{
    public void print(String str);
}
public class Lamda {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        fun((s) -> {
            s = s.toUpperCase();
            System.out.println(s);
            });
        
    }
    public static void fun(IMessagea im){
        im.print("Hello World!!!");
    }

}

表达式

package cn.fovegage.Demo;

interface IMessagea{
    public int add(int x,int y);
}
public class Lamda {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        fun((s1,s2) -> s1+s2);
        
    }
    public static void fun(IMessagea im){
        System.out.print(im.add(10, 20));
    }

}

方法引用(函数式接口()在接口中使用,且接口中只可定义一个方法)

引用静态方法

package cn.fovegage.Demo;

interface IMessage<P,R>{   //P是引用方法的参数类型,R是引用方法的返回类型
    public R zhuanhuan(P p);
}
public class yinyong {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //将String.VauleOf()的方法转换成IMessageq的接口的zhuanhuan()方法
        IMessage<Integer,String> msg = String :: valueOf;
        String str = msg.zhuanhuan(1000);
        System.out.println(str.replaceAll("0", "9"));
    }

}

普通方法引用

package gg;

@FunctionalInterface
interface IMessage<R>{
    public R upper();
}
public class f {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        IMessage<String> msg = "hello" :: toUpperCase;
        String str = msg.upper();
        System.out.print(str);
    }
}

特殊方法的引用

package gg;

@FunctionalInterface
interface IMessage<P>{
    public int upper(P p1,P p2);
}
public class f {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        IMessage<String> msg = String :: compareTo;
        System.out.print(msg.upper("A", "B"));
    }
}

引用构造方法(一般构造方法只有在实例化的时候才能引用,静态方法则可以直接引用)

package gg;

@FunctionalInterface
interface IMessage<P>{
    public P upper(String name,double price);
}
class Book{
    private String name;
    private double price;
    public Book(String name,double price){
        this.name = name;
        this.price = price;
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "书名:"+this.name+" "+"价格:"+this.price;
    }
}
public class f {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        IMessage<Book> msg = Book :: new;
        Book book = msg.upper("Java", 36.6);
        System.out.print(book);
    }
}

内建函数式接口

上一篇下一篇

猜你喜欢

热点阅读