接口在Jdk8的新特性以及lambda的使用

其中也不算是新特性吧……毕竟现在都Jdk13啦…..

在以前的学习中,总是认为接口,就是定义一种规范,它里面只能定义常量和抽象类,而这些都是public修饰的。但现在Jdk也给了接口新的特性,没用过以为没啥用….

而以前说的这句话就太错了。自己没了解太多,而说不建议用,太错了。

现在让我们好好学习Jdk8给接口的新特性。当然,现在都13了,也肯定有其他的新特性,这个以后再了解吧……(我现在Jdk才11.0.4……)

接口的新特性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class ExInterfaceTest01 {
public static void main(String[] args) {
Son s = new Son();
//s.printA(); //错误,不能使用接口的静态方法
Fater.printA(); //只能接口自己调用自己的静态方法
s.printB(); //可以调用接口的默认方法
}
}
interface Fater{
static void printA() { //我们可以在接口定义一个静态的方法
System.out.println("爸爸只能自己调用");
}
default void printB() { //也可以在接口定义一个默认的方法
System.out.println("为你写好了代码");
}
}
class Son implements Fater{ //这里我们只测试static和default修饰的方法,所以没有任何实现
// @Override //接口静态的方法不能被重写
// public static void printA() {
//
// }
@Override
public void printB() {
Fater.super.printB(); //调用接口的方法printB()
}
}

可以看到,现在的接口不止可以定义一个常量和抽象类,也可以在接口实现静态方法和默认方法。

但是要注意,静态方法只能是接口去使用也不能重写。而接口的默认方法可以直接调用,还可以重写。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
interface Fater{
static void printA() {
System.out.println("爸爸只能自己调用");
}
default void printB() { /Fater里的printB方法
System.out.println("为你写好了代码");
}
}
interface mother{
default void printB() { //mother里的printB方法
System.out.println("为你写好了代码");
}
}
class Son implements Fater,mother{ //son继承两个接口
@Override
public void printB() { //接口方法冲突,必须要重写方法
//Fater.super.printB();
System.out.println("我要自己写代码");
}
}

一般来说,两个接口里面有两个相同的抽象方法,子类继承了它们两个,只需要实现的其中一个就行。而现在两个接口都有两个相同的默认方法,子类继承了它们,这时候我们就要重写这个默认方法。这方法冲突可能就存在接口里吧,毕竟java多继承只有接口。

lambda

我们先简单的使用匿名内部类。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Test01 {

public static void main(String[] args) {

Axx a = new Axx() {

@Override
public void printA(String str) {
System.out.println(str);

}
};
a.printA("哈哈");
}

}
interface Axx{
void printA(String str);
}

现在把它转换成lambda表达式。

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Test01 {

public static void main(String[] args) {

Axx a = (str)->System.out.println(str);

a.printA("哈哈");
}
}

interface Axx{
void printA(String str);
}

下面是lambda的推导。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Test01 {
public static void main(String[] args) {

// Axx a = (String str)->System.out.println(str);
// Axx a = (str)->System.out.println(str);
Axx a = str->System.out.println(str);

a.printA("哈哈");

}
interface Axx{
void printA(String str);
}
}

这是只要一个参数,没有返回值的情况。并且只有一行代码的情况下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Test01 {
public static void main(String[] args) {

// Axx ax = (int a, int b)->a+b;
Axx ax = (a,b)->a+b;
//Axx ax = a,b->a+b; //这种就不行了,多个参数必须加( )

System.out.println(ax.addB(1,1));

}
interface Axx{
int addB(int a, int b);
}
}

有多个参数,并且可以直接返回值,并且只有一行代码,那么处理的的情况有点复杂呢?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class Test01 {
public static void main(String[] args) {

Axx ax = (a,n) ->{ //一定要加{
//简单的排序:冒泡排序
for (int i=0; i<n; i++) {
for (int j=i+1; j<n; j++) {
if (a[i]>a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

return a;
}; //别忘了}

int[] a = new int [10];
System.out.print("排序前:");
for (int i=0; i<a.length; i++) {
a[i] = (int)(Math.random()*20+1);
System.out.print(a[i]+" ");
}

System.out.print("\n排序后:");
int[] b=ax.sortArray(a, a.length); //调用实现接口的方法
for(int i=0; i<b.length; i++) {
System.out.print(b[i] + " ");
}
}
interface Axx{
int[] sortArray(int[] array, int length);
}
}

这是一个简单的排序算法,有多行代码,返回值也是排序后的结果。

我们的关注点就是(a,n)->{};这里多了{};,也多了return。

如果没有参数,不能直接“->“这样写,一定要“()->”才行。

谢谢您对我的支持
0%