Interfaces are named collections of methods signatures
package main import ( "fmt" "math" ) type geometry interface { area() float64 perim() float64 } type square struct { width, height float64 } type circle struct { radius float64 } func (s square) area() float64 { return s.width * s.height } func (s square) perim() float64 { return 2*s.width + 2*s.height } func (c circle) area() float64 { return math.Pi * c.radius * c.radius } func (c circle) perim() float64 { return 2 * math.Pi * c.radius } func measure(g geometry) { fmt.Println(g) fmt.Println(g.area()) fmt.Println(g.perim()) } func main() { s := square{width: 3, height: 4} c := circle{radius: 5} measure(s) measure(c) }
{3 4} 12 14 {5} 78.53981633974483 31.41592653589793
总结 :
1 : interface ....没什么可说的....
2 : 实现接口对应的所有方法
时间: 2024-10-11 07:40:05