Skip to content

Commit edfa7be

Browse files
committed
\#28 - go
1 parent 2d3561a commit edfa7be

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
// La interfaz Vehicle define el comportamiento de un vehículo
4+
// "Clase" Vehicle
5+
type Vehicle interface {
6+
Accelerate()
7+
Brake()
8+
}
9+
10+
// Car implementa la interfaz Vehicle
11+
type Car struct{}
12+
13+
func (c Car) Accelerate() {
14+
println("Acelerando carro")
15+
}
16+
17+
func (c Car) Brake() {
18+
println("Frenando carro")
19+
}
20+
21+
// Boat implementa la interfaz Vehicle
22+
type Boat struct{}
23+
24+
func (b Boat) Accelerate() {
25+
println("Acelerando bote")
26+
}
27+
28+
func (b Boat) Brake() {
29+
println("Frenando bote")
30+
}
31+
32+
// Boat implementa una funcion para Unmoor, por lo tanto estamos agregando un objeto S sin afectar al comportamiento
33+
func (b Boat) Unmoor() {
34+
println("Desamarrando bote")
35+
}
36+
37+
// DriveVehicle acelera y frena un vehículo
38+
func DriveVehicle(v Vehicle) {
39+
v.Accelerate()
40+
v.Brake()
41+
}
42+
43+
func main() {
44+
car := Car{}
45+
boat := Boat{}
46+
47+
DriveVehicle(car)
48+
DriveVehicle(boat)
49+
50+
// Llamados particulares
51+
boat.Unmoor()
52+
}

0 commit comments

Comments
 (0)