-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
inteface.go
44 lines (35 loc) · 837 Bytes
/
inteface.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
package main
import (
"fmt"
"math"
)
/* 定义一个 interface */
type shape interface {
area() float64
}
/* 定义一个 circle */
type circle struct {
x,y,radius float64
}
/* 定义一个 rectangle */
type rectangle struct {
width, height float64
}
/* 定义一个circle方法 (实现 shape.area())*/
func(circle circle) area() float64 {
return math.Pi * circle.radius * circle.radius
}
/* 定义一个rectangle方法 (实现 shape.area())*/
func(rect rectangle) area() float64 {
return rect.width * rect.height
}
/* 定义一个shape的方法*/
func getArea(shape shape) float64 {
return shape.area()
}
func main() {
circle := circle{x:0,y:0,radius:5}
rectangle := rectangle {width:10, height:5}
fmt.Printf("circle area: %f\n",getArea(circle))
fmt.Printf("rectangle area: %f\n",getArea(rectangle))
}