Goノート2(Interfaces)
An Introduction To Programming In Go読書ノート
-- 9 Structs and Interfaces - Interfaces
-- 9 Structs and Interfaces - Interfaces
package main
import (
"fmt"
"math"
)
func distance(x1,y1,x2,y2 float64) float64 {
a := x2 - x1
b := y2 - y1
return math.Sqrt(a*a + b*b)
}
type Circle struct {
x, y, r float64
}
func (c *Circle) area() float64 {
return math.Pi * c.r * c.r
}
type Rectangle struct {
x1, y1, x2, y2 float64
}
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
}
type Shape interface {
area() float64
}
func totalArea(shapes ...Shape) float64 {
var area float64
for _, s := range shapes {
area += s.area()
}
return area
}
type MultiShape struct {
shapes []Shape
}
func (m *MultiShape) area() float64 {
var area float64
for _, s := range m.shapes {
area += s.area()
}
return area
}
func main() {
c := Circle{2,2,10}
fmt.Println(c.area())
r := Rectangle{1,1,2,2}
fmt.Println(r.area())
t := totalArea(&c, &r)
fmt.Println(t)
m := new(MultiShape)
m.shapes = append(m.shapes,&c)
m.shapes = append(m.shapes,&r)
m.shapes = append(m.shapes,&r)
m.shapes = append(m.shapes,&r)
fmt.Println(m.area())
}