-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path14-interface.go
86 lines (71 loc) · 1.7 KB
/
14-interface.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* @Author: Barnabas Makonda
* @Date: 2019-01-16 11:11:52
* @Last Modified by: Barnabas Makonda
* @Last Modified time: 2019-01-16 11:26:14
*/
package main
import (
"fmt"
"math"
)
/*
* Like struct an interface is created using the type keyword followed by the name and the keyword interface.
* But instead of defining fields we define a "method set". A method set is a list of methods that a type must have in order to implement the interface.
* They can be used as field type too.
*/
// Circle represent a circle
type Circle struct {
x, y, r float64
}
// Rectangle struct
type Rectangle struct {
x1, y1, x2, y2 float64
}
func (c *Circle) area() float64 {
return math.Pi * c.r * c.r
}
func (c *Circle) perimeter() float64 {
return 2 * math.Pi * c.r
}
// Shape Interface with a method
type Shape interface {
area() float64
perimeter() float64
}
func distance(x1, y1, x2, y2 float64) float64 {
a := x2 - x1
b := y2 - y1
return math.Sqrt(a*a + b*b)
}
func (r *Rectangle) area() float64 {
l := distance(r.x1, r.y1, r.x1, r.y2)
w := distance(r.x1, r.y1, r.x2, r.y1)
return l * w
}
func (r *Rectangle) perimeter() float64 {
l := distance(r.x1, r.y1, r.x1, r.y2)
w := distance(r.x1, r.y1, r.x2, r.y1)
return 2 * (l + w)
}
// All argument must implement method area as interface Shape suggest.
func totalArea(shapes ...Shape) float64 {
var area float64
for _, s := range shapes {
area += s.area()
}
return area
}
func totalPerimeter(shapes ...Shape) float64 {
var perimeter float64
for _, s := range shapes {
perimeter += s.perimeter()
}
return perimeter
}
func main() {
c := Circle{0, 0, 5}
r := Rectangle{0, 0, 10, 10}
fmt.Println(totalArea(&c, &r))
fmt.Println(totalPerimeter(&c, &r))
}