栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

具有嵌入式匿名接口的结构的含义?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

具有嵌入式匿名接口的结构的含义?

通过这种方式,反向实现了

sort.Interface
,我们可以覆盖特定的方法而不必定义所有其他方法

type reverse struct {        // This embedded Interface permits Reverse to use the methods of        // another Interface implementation.        Interface}

请注意,这里是如何交换

(j,i)
而不是交换的
(i,j)
,这也是为struct声明的唯一方法,
reverse
即使
reverse
实现
sort.Interface

// 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)}

无论此方法内部传递了什么结构,我们都会将其转换为新的

reverse
结构。

// Reverse returns the reverse order for data.func Reverse(data Interface) Interface {        return &reverse{data}}

真正的价值在于,如果您认为如果无法采用这种方法,该怎么办。

  1. Reverse
    sort.Interface
    吗?添加另一种方法?
  2. 创建另一个ReverseInterface?
  3. …?

任何此类更改都需要跨数千个要使用标准反向功能的软件包的许多行代码。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/471084.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号