Go-interfaces zijn geen overerving
κ°μ
Go μΈν°νμ΄μ€λ λμΌν μΈμμ λ°νκ°μ κ°λ ν¨μλ₯Ό μ¬λ¬ ꡬ쑰체μμ μ½κ² κ°μ§ μ μκ² νμ§λ§, javaμ extends ν€μλμ²λΌ κ·Έ λ΄λΆ ν¨μμ λμκΉμ§ μ μ ν μ°μ₯νκ³ μ€λ²λΌμ΄λνλ λ°©μκ³Όλ λ€λ¦ λλ€. Goμ ꡬμ±μ μ½λ μ¬μ¬μ©μ μ λλ‘ μ΄ν΄ν΄μΌλ§ μμκ³Ό ν·κ°λ¦¬μ§ μκ² μ§λ§, μ²μλΆν° μ΄λ‘ μ μΌλ‘ μλ²½ν μ΄ν΄λ₯Ό νλ κ²μ μ΄λ ΅μ΅λλ€. μ€μνκΈ° μ’μ μλ리μ€μ ν¨κ» μμλ΄ μλ€.
μμ£Ό νλ μ€μ
μ΄μ¬μ λΆλ€μ λ€μκ³Ό κ°μ μ€μλ₯Ό νμ€ μ μμ΅λλ€.
1package main
2import (
3 "fmt"
4 "strings"
5)
6
7type Fruits interface {
8 GetBrix() float64
9 GetName() string
10 SetLabel()
11 GetLabel(string) string
12 PrintAll()
13}
14
15type Apple struct {
16 Label string
17 Name string
18 Brix float64
19}
20
21type Watermelon struct {
22 Label string
23 Name string
24 Brix float64
25}
26
27func (a *Apple) PrintAll() {
28 fmt.Printf("Fruit: %s, Label: %s, Brix: %v\n", a.Name, a.Label, a.Brix)
29}
30
31const (
32 NO_LABEL = "EMPTY LABEL"
33)
34
35func (a *Apple) SetLabel(lbl string) {
36 a.Brix = 14.5;
37 a.Name = "apple";
38 lbl_lower := strings.ToLower(lbl)
39 if strings.Contains(lbl_lower, a.Name) {
40 fmt.Println("Succeed: Label was ", lbl)
41 a.Label = lbl;
42 } else {
43 fmt.Println("Failed: Label was ", lbl)
44 a.Label = NO_LABEL;
45 }
46}
47
48func (w *Watermelon) SetLabel(lbl string) {
49 w.Brix = 10;
50 w.Name = "watermelon";
51 lbl_lower := strings.ToLower(lbl)
52 if strings.Contains(lbl_lower, w.Name) {
53 w.Label = lbl;
54 } else {
55 w.Label = NO_LABEL;
56 }
57}
58
59func main() {
60 fmt.Println("Inheritance test #1")
61 apple := new(Apple)
62 watermelon := apple
63 apple.SetLabel("Apple_1")
64 fmt.Println("Apple, before copied to Watermelon")
65 apple.PrintAll()
66 watermelon.SetLabel("WaterMelon_2")
67 fmt.Println("Apple, after copied to Watermelon")
68 apple.PrintAll()
69 fmt.Println("Watermelon, which inherited Apple's Method")
70 watermelon.PrintAll()
71}
μ΄λ¬ν μ½λλ Goκ° μ ν΅μ μΈ μμμ λ°λ₯Έλ€κ³ μ°©κ°νλ©΄ λ¬Έμ κ° μμ΄ λ³΄μ λλ€. νμ§λ§ μ΄κ²μ μΆλ ₯ κ²°κ³Όλ λ€μκ³Ό κ°μ΅λλ€.
1Inheritance test #1
2Succeed: Label was Apple_1
3Apple, before copied to Watermelon
4Fruit: apple, Label: Apple_1, Brix: 14.5
5Failed: Label was WaterMelon_2
6Apple, after copied to Watermelon
7Fruit: apple, Label: EMPTY LABEL, Brix: 14.5
8Watermelon, which inherited Apple's Method
9Fruit: apple, Label: EMPTY LABEL, Brix: 14.5
μ¬κΈ°μ Goμ λμμ λ€λ§ λͺ νν΄μ§λλ€.
1watermelon := apple
μ΄ μ½λλ μ ν Appleμ κ·Έλλ‘ Watermelon ν΄λμ€λ‘ λ³ννμ§ μμ΅λλ€. λ€λ§ watermelonμ appleμ λν ν¬μΈν°μΌ λΏμ λλ€.
μ¬κΈ°μ λ€μ κ°μ‘°νμ§λ§, Goλ μ ν΅μ μΈ μμ κ°λ μ λ°λ₯΄μ§ μμ΅λλ€.
μ΄λ¬ν μ€ν΄λ₯Ό ν μνμμ μ½λλ₯Ό μ§ λ€λ©΄ 무μλ―Έν ν¬μΈν° μμ±, μκΈ°μΉ λͺ»ν ν ꡬ쑰체λ₯Ό μν ν¨μ λ³΅μ¬ λ±μ μΉλͺ μ μ€λ₯κ° μκΉλλ€.
κ·Έλ λ€λ©΄ λͺ¨λ²μ μΈ μ½λλ μ΄λ ν κΉμ?
GoμΈμ΄μμ μ μ ν μμ
1package main
2import (
3 "fmt"
4 "strings"
5)
6
7type Fruits interface {
8 GetBrix() float64
9 GetName() string
10 SetLabel()
11 GetLabel(string) string
12 PrintAll()
13}
14
15type BaseFruit struct {
16 Name string
17 Brix float64
18}
19
20type Apple struct {
21 Label string
22 Fruit BaseFruit
23}
24
25type Watermelon struct {
26 Label string
27 Fruit BaseFruit
28
29}
30
31func (b *BaseFruit) PrintAll() {
32 fmt.Printf("Fruit: %s, Brix: %v\n", b.Name, b.Brix)
33}
34
35
36const (
37 NO_LABEL = "EMPTY LABEL"
38)
39
40func (a *Apple) SetLabel(lbl string) {
41 a.Fruit.Brix = 14.5;
42 a.Fruit.Name = "apple";
43 lbl_lower := strings.ToLower(lbl)
44 if strings.Contains(lbl_lower, a.Fruit.Name) {
45 fmt.Println("Succeed: Label was ", lbl)
46 a.Label = lbl;
47 } else {
48 fmt.Println("Failed: Label was ", lbl)
49 a.Label = NO_LABEL;
50 }
51 fmt.Printf("Fruit %s label set to %s\n", a.Fruit.Name, a.Label);
52 a.Fruit.PrintAll()
53}
54
55func (w *Watermelon) SetLabel(lbl string) {
56 w.Fruit.Brix = 10;
57 w.Fruit.Name = "Watermelon";
58 lbl_lower := strings.ToLower(lbl)
59 if strings.Contains(lbl_lower, w.Fruit.Name) {
60 w.Label = lbl;
61 } else {
62 w.Label = NO_LABEL;
63 }
64 fmt.Printf("Fruit %s label set to %s\n", w.Fruit.Name, w.Label);
65 w.Fruit.PrintAll()
66}
67
68func main() {
69 apple := new(Apple)
70 watermelon := new(Watermelon)
71 apple.SetLabel("Apple_1")
72 watermelon.SetLabel("WaterMelon_2")
73}
κ·Έλ¬λ, Goμμλ μμμ²λΌ 보μ΄κ² νλ κ²μ κ°λ₯ν©λλ€. μ΅λͺ μλ² λ©μ΄λΌλ μμμ λλ€. μ΄κ²μ λ΄λΆ ꡬ쑰체λ₯Ό μ΄λ¦ μλ κ΅¬μ‘°μ²΄λ‘ μ μΈνλ©΄ κ°λ₯ν©λλ€. μ΄λ¬ν κ²½μ°μλ νμ ꡬ쑰체μ νλλ€μ λͺ μ μμ΄ μ¬μ©ν΄λ κ·Έλλ‘ μ κ·Όμ΄ κ°λ₯ν©λλ€. μ΄λ κ² νμ ꡬ쑰체μ νλλ₯Ό μμ κ΅¬μ‘°μ²΄λ‘ μΉκ²©νλ ν¨ν΄μ μ΄μ©νλ©΄ κ²½μ°μ λ°λΌ κ°λ μ±μ ν₯μμ΄ κ°λ₯ν©λλ€. κ·Έλ¬λ νμ ꡬ쑰체λ₯Ό λͺ μμ μΌλ‘ 보μ¬μ€μΌ νλ κ²½μ°μλ μ¬μ©νμ§ μκΈ°λ₯Ό κΆμ₯ν©λλ€.
1package main
2import (
3 "fmt"
4 "strings"
5)
6
7type Fruits interface {
8 GetBrix() float64
9 GetName() string
10 SetLabel()
11 GetLabel(string) string
12 PrintAll()
13}
14
15type BaseFruit struct {
16 Name string
17 Brix float64
18}
19
20type Apple struct {
21 Label string
22 BaseFruit
23}
24
25type Watermelon struct {
26 Label string
27 BaseFruit
28
29}
30
31func (b *BaseFruit) PrintAll() {
32 fmt.Printf("Fruit: %s, Brix: %v\n", b.Name, b.Brix)
33}
34
35
36const (
37 NO_LABEL = "EMPTY LABEL"
38)
39
40func (a *Apple) SetLabel(lbl string) {
41 a.Brix = 14.5;
42 a.Name = "apple";
43 lbl_lower := strings.ToLower(lbl)
44 if strings.Contains(lbl_lower, a.Name) {
45 fmt.Println("Succeed: Label was ", lbl)
46 a.Label = lbl;
47 } else {
48 fmt.Println("Failed: Label was ", lbl)
49 a.Label = NO_LABEL;
50 }
51 fmt.Printf("Fruit %s label set to %s\n", a.Name, a.Label);
52 a.PrintAll()
53}
54
55func (w *Watermelon) SetLabel(lbl string) {
56 w.Brix = 10;
57 w.Name = "Watermelon";
58 lbl_lower := strings.ToLower(lbl)
59 if strings.Contains(lbl_lower, w.Name) {
60 w.Label = lbl;
61 } else {
62 w.Label = NO_LABEL;
63 }
64 fmt.Printf("Fruit %s label set to %s\n", w.Name, w.Label);
65 w.PrintAll()
66}
67
68func main() {
69 apple := new(Apple)
70 watermelon := new(Watermelon)
71 apple.SetLabel("Apple_1")
72 watermelon.SetLabel("WaterMelon_2")
73}
μ΄ μμμμλ μ΄λ¬ν μ°¨μ΄μ μ΄ μμ΅λλ€.
1w.PrintAll() // w.Friut.PrintAll()μ΄ μλ, μ΄λ¦ μλ ꡬ쑰체λ₯Ό ν΅ν μλ μΉκ²© νΈμΆ
λ μμ λͺ¨λ μ€μν μ§μ μ μ΄λ¬ν©λλ€.
- mainμ κ°μνκ², ν¨μλ κΈ°λ₯ λ³λ‘
- λ€λ₯Έ ꡬ쑰체λΌλ©΄ λ€λ₯Έ κ°μ²΄λ₯Ό
- 곡μ κ° νμν κ²½μ° λ΄λΆ ꡬ쑰체λ₯Ό μ¬μ©
μ΄μ κ°μ νλ‘κ·Έλλ° μ² νμ μ΄λ ν μ΄μ μ΄ μμκΉμ?
μ΄μ
- 곡μ κ° νμν λ©μλμ μλ κ²μ κ΅¬λ³ λͺ ν
- κ°λ³ ꡬ쑰체, λ©μλμ μ± μ μμ¬ λΆλ¦¬
- νμν κΈ°λ₯ λͺ μΈμ λ°λΌ ꡬ쑰μ μΌλ‘ λΆλ¦¬λ μ½λ
μ²μμλ GoμΈμ΄λ μ ν΅μ μΈ OOPμ λ¬λΌ μ΅μνμ§ μμ μ μμΌλ, μ΅μν΄μ§λ©΄ λͺ μμ μΈ νλ‘κ·Έλλ°μ΄ κ°λ₯ν©λλ€.
μμ½
- μ± μ μμ¬λ₯Ό κ³ λ¦½μν€μ
- ꡬ쑰체 λ¨μλ‘ μΈλΆμ μΌλ‘ λλμ
- λ©μλλ μλ°μ abstract class zoals niet begrepen moet worden
- λͺ μμ μ΄κ³ ꡬ체μ μΈ νλ‘κ·Έλλ°μ νμ GoμΈμ΄λ μ ν΅μ μΈ OOP λͺ¨λΈλ³΄λ€ κ°λ¨λͺ λ£νκ³ κ°λ³μ μΌλ‘ λ€λ€μ ΈμΌ ν©λλ€. νμ₯μ μ΄κ² νλ‘κ·Έλλ°νκΈ°λ³΄λ€ λ¨κ³μ μ΄κ³ ꡬ쑰μ μΌλ‘ λΆλ¦¬νμ¬ μμ±νλλ‘ ν©μλ€.