-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterfaces.go
53 lines (45 loc) · 916 Bytes
/
interfaces.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
package main
import (
"fmt"
)
// bike interface
type Bike interface {
RevEngine() string
GetSpecs() string
TopSpeed() int
}
// motorcycle implements bike
type Motorcycle struct {
Brand string
Model string
CC int
Category string
MaxSpeed int
HasABS bool
}
func (m Motorcycle) RevEngine() string {
if m.Model == "MT-15" {
return "Vroom Vrroom! * MT-15 VVA engine roars *"
}
return "Generic motorcycle sounds"
}
func (m Motorcycle) GetSpecs() string {
return fmt.Sprintf("Bike: %s %s\nCategory: %s\nEngine: %dcc\nABS: %v",
m.Brand, m.Model, m.Category, m.CC, m.HasABS)
}
func (m Motorcycle) TopSpeed() int {
return m.MaxSpeed
}
func main() {
bike := Motorcycle{
Brand:"Yamaha",
Model:"MT-15",
CC:155,
Category:"Naked",
MaxSpeed:140,
HasABS:true,
}
fmt.Println(bike.RevEngine())
fmt.Println(bike.GetSpecs())
fmt.Printf("Top Speed: %d km/h\n", bike.TopSpeed())
}