-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathJdk8Interface.java
82 lines (72 loc) · 1.35 KB
/
Jdk8Interface.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package net.codingme.feature.jdk8;
/**
* <p>
* 接口的静态方法和默认方法
*
*
* @Author niujinpeng
* @Date 2019/2/18 22:52
*/
public class Jdk8Interface {
public static void main(String[] args) {
// 接口静态方法
Person.say();
// 接口默认方法
Person southerner = new Southerner();
southerner.eat();
// 接口重写方法
Northerners northerners = new Northerners();
northerners.eat();
/**
* result<br/>
* 你好啊<br/>
* 吃米饭<br/>
* 吃馒头<br/>
*/
}
}
/**
* 南方人
*/
class Southerner implements Person {
}
/**
* 北方人
*/
class Northerners implements Person {
@Override
public void eat() {
System.out.println("吃馒头");
}
}
/**
* 多个接口有相同的默认方法必须重写方法
*/
class PersonImpl implements Person, Person2 {
@Override
public void eat() {
System.out.println("吃米饭吃粥");
}
}
interface Person {
/**
* 接口静态方法
*/
static void say() {
System.out.println("你好啊");
}
/**
* 接口默认方法
*/
default void eat() {
System.out.println("吃米饭");
}
}
/**
*
*/
interface Person2 {
default void eat() {
System.out.println("吃粥");
}
}