-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLesson5_7.java
More file actions
52 lines (45 loc) · 987 Bytes
/
Lesson5_7.java
File metadata and controls
52 lines (45 loc) · 987 Bytes
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
/**
接口
*/
public class Lesson5_7{
public static void main(String[] args){
Girl g = new Girl("jie");
g.sleep();
g.eat();
g.run();
g.print();
}
}
interface IEat{
//public abstract void eat();//接口中只能定義抽象方法
void eat();//接口中定義的方法沒有聲明修飾符,默認為public abstract
public static final int Num = 10;//接口中定義一個常量
int NUM = 10;//常量
public default void print(){//JDK1.8後新特性,可以被所有實現類繼承
System.out.println("eat");//實現類為實現接口的類
}
}
interface IRun extends IEat{
void run();
}
//接口之間可以多繼承(注意:類只能單繼承)
interface ISleep extends IEat,IRun{
void sleep();
}
//具體類實現接口必須實現接口的所有方法
class Girl implements ISleep,IEat{
private String name;
public Girl(){}
public Girl(String name){
this.name = name;
}
public void sleep(){
System.out.println("我愛睡覺");
}
public void eat(){
System.out.println("我是"+name+"我愛吃");
}
public void run(){
System.out.println("跑跑");
}
}