-
Notifications
You must be signed in to change notification settings - Fork 0
/
herenciaEInterfaces.go
64 lines (53 loc) · 1.24 KB
/
herenciaEInterfaces.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import "fmt"
type Person struct {
name string
age int
}
type Employee struct {
id int
}
type FullTimeEmployee struct {
//Herencia
Person
Employee
//Es de forma anónima
vacation bool
}
type TemporalyTimeEmployee struct {
//Herencia
Person
Employee
//Es de forma anónima
taxRate int
}
//interface
type PrintInfo interface {
getMessage() string
}
func (ftEmployee FullTimeEmployee) getMessage() string {
return "Full time Employee"
}
func NewFullTimeEmployee(id int, name string, vacation bool) *FullTimeEmployee {
return &FullTimeEmployee{}
}
func getMessage(p PrintInfo) {
fmt.Println(p.getMessage())
}
//principio de composición sobre herencia
func main() {
e := FullTimeEmployee{}
e.age = 1
e.name = "Felipe"
e.id = 22
e.vacation = true
fmt.Printf("%v", e)
tEmployee := TemporalyTimeEmployee{}
getMessage(tEmployee)
getMessage(e)
// En go no existe la herencia directa, por decir que yo heredo directamente las funciones de x clase
// para implementar esto se necesita el uso de interfaces
// Estas son un tipo de contratos ( Comprometerse a implementar lo definido por la interfaz)
// que se tienen con las clases a la hora de ser implementadas
// Go no usa una forma explicita de usar estas implementaciones
}