-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18_interface.go
More file actions
60 lines (43 loc) · 1.25 KB
/
18_interface.go
File metadata and controls
60 lines (43 loc) · 1.25 KB
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
package main
import "fmt"
// 인터페이스 - 메소드(기능)들의 집합
// type 인터페이스명 interface { 메소드 반환형 }
type Pizza struct{}
func (p Pizza) eat() {
fmt.Println("피자 한조각 먹기")
}
// func (리시버 인자) 메소드이름 (리턴타입) { }
func (p Pizza) taste() {
fmt.Println("피자는 역시 맛있어")
}
//--------------------------------------------
type Coke struct {}
func (c Coke) eat() {
fmt.Println("느끼할땐 콜라")
}
func (c Coke) taste() {
fmt.Println("콜라는 달콤해")
}
//--------------------------------------------
type fastFood interface { // 👉 interface는 메소드의 집합 / fastFood로 묶어줌
eat() // 메소드
taste() // 메소드
}
// 👇 interface 명 을 가져감
func westFood (f fastFood) {
f.eat()
f.taste()
}
//--------------------------------------------
func main() {
var NewYork Pizza // Pizza struct 를 변수에
var Coca Coke // Coke struct 를 변수에
westFood(NewYork) // interface를 담은 함수에 struct 대입
westFood(Coca)
// interface로 메소드들을 묶어줘서 한번에 출력이 가능하다
// 👉 출력값
// 피자 한조각 먹기
// 피자는 역시 맛있어
// 느끼할땐 콜라
// 콜라는 달콤해
}