-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_inheritance.go
41 lines (31 loc) · 862 Bytes
/
fake_inheritance.go
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
package main
import "fmt"
type BaseMethods interface {
Impl() float32
UsesImpl() float32
}
type Base struct {
methods BaseMethods
value float32
}
type KeepsImpl struct { Base }
type OverridesImpl struct { Base }
func (t *Base) Impl() float32 { return t.value }
func (t *Base) UsesImpl() float32 { return t.methods.Impl() + 1 }
func (t *OverridesImpl) Impl() float32 { return t.value * t.value }
func main() {
t1 := &KeepsImpl{Base{nil, 2}}
t1.methods = t1
t2 := &OverridesImpl{Base{nil, 2}}
t2.methods = t2
bases := []BaseMethods{t1, t2}
for s, _ := range bases {
switch bases[s].(type) {
case *KeepsImpl:
fmt.Println("KeepsImpl")
case *OverridesImpl:
fmt.Println("OverridesImpl")
}
fmt.Printf("The value is: %f\n", bases[s].UsesImpl())
}
}