type Interface interface Len() int Less(i,j int) bool Swap(i,j int)}...type reverse struct Interface}7 U, ?# N# N* {6 ^
Interfacestruct匿名界面的含义是什么?reverse? , W# k* [ j- P1 x# h 9 \; ` l' ]9 @4 z0 ?/ _) D解决方案: ) P+ s/ x+ E8 |" M; f
这样,反向实现sort.Interface,我们可以覆盖特定的方法而不必定义所有其他方法 / N- k3 k4 r7 j# H, T
type reverse struct This embedded Interface permits Reverse to use the methods of another Interface implementation. Interface} 0 m; {1 n5 T( P
注意如何在这里交换(j,i)而不是,即使实现了,(i,j)这也是声明结构的唯一方法reverse``reverse``sort.Interface 2 H+ `9 N% a8 ^' B- }6 o
// Less returns the opposite of the embedded implementation's Less method.func (r reverse) Less(i,j int) bool return r.Interface.Less(j,i)} " C& n0 G' Z2 `$ N, U* c