interface _ golang
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 : 实现接口对应的所有方法
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。