Go_笔试题记录-指针与值类型实现接口的区别

1、如果Add函数的调用代码为:

func main() {var a Integer = 1var b Integer = 2var i interface{} = &asum := i.(*Integer).Add(b)fmt.Println(sum)
}

则Add函数定义正确的是()

A.type Integer int
func (a Integer) Add(b Integer) Integer {return a + b
}B.
type Integer int
func (a Integer) Add(b *Integer) Integer {return a + *b
}C.
type Integer int
func (a *Integer) Add(b Integer) Integer {return *a + b
}D.
type Integer int
func (a *Integer) Add(b *Integer) Integer {return *a + *b
}

参考答案:AC

2、如果Add函数的调用代码为:

func main() {var a Integer = 1var b Integer = 2var i interface{} = asum := i.(Integer).Add(b)fmt.Println(sum)
}

则Add函数定义正确的是()

A.type Integer int
func (a Integer) Add(b Integer) Integer {return a + b
}B.
type Integer int
func (a Integer) Add(b *Integer) Integer {return a + *b
}C.
type Integer int
func (a *Integer) Add(b Integer) Integer {return *a + b
}D.
type Integer int
func (a *Integer) Add(b *Integer) Integer {return *a + *b
}

参考答案:A

3、关于GetPodAction定义,下面赋值正确的是()

type Fragment interface {Exec(transInfo *TransInfo) error
}
type GetPodAction struct {
}
func (g GetPodAction) Exec(transInfo *TransInfo) error {...return nil
}A. var fragment Fragment = new(GetPodAction)
B. var fragment Fragment = GetPodAction
C. var fragment Fragment = &GetPodAction{}
D. var fragment Fragment = GetPodAction{}

参考答案:ACD

考察知识点

指针与值类型实现接口的区别

package mainimport ("fmt"
)type People interface {ReturnName() string
}type Student struct {Name string
}type Teacher struct {Name string
}func (s Student) ReturnName() string {return s.Name
}func (t *Teacher) ReturnName() string {return t.Name
}func main() {cbs := Student{Name: "咖啡色的羊驼"}sss := Teacher{Name: "咖啡色的羊驼的老师"}// 值类型var a Peoplea = cbsname := a.ReturnName()fmt.Println(name)// 指针类型//a = sss // <- 这样写不行!!!a = &sss // 由于是指针类型,所以赋值的时候需要加上&name = a.ReturnName()fmt.Println(name) // 输出"咖啡色的羊驼的老师"
}

“a = sss”这样写会发生报错:

cannot use sss (type Teacher) as type People in assignment:
Teacher does not implement People (ReturnName method has pointer receiver)

因为是Teacher的指针实现了ReturnName方法,Teacher本身没实现。

总结:

1.当是指针类型,它的方法可以是指针接收者,或者是值接收者
2.当是值类型,它的方法只能是值接收者

转载于:https://www.cnblogs.com/Paul-watermelon/p/11209603.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392465.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

leetcode 48. 旋转图像

解题思路 将数组从里到外分为若干层&#xff0c; 数组 [1,2,3], [4,5,6][7,8,9]的最外层即为 [1,2,3] [4 6][7,8,9] &#xff0c;将一层分为4条边&#xff0c;如741 123&#xff0c;将741放到123的位置&#xff0c;123放到369的位置&#xff0c;如此类推&#xff08;但是放置的…

如何恢复误删的OneNote页面

今天不小心把半个月的日记删掉了&#xff01;&#xff08;为了减少页面数量&#xff0c;每个月的日记会放在同一个页面上&#xff09;。 幸运的是OneNote有自动备份功能&#xff0c;喜极而泣。 操作方法来自微软支持 打开丢失了最近笔记的笔记本。 单击“文件”>“信息”&g…

javascript函数式_JavaScript中的函数式编程原理

javascript函数式After a long time learning and working with object-oriented programming, I took a step back to think about system complexity.经过长时间的学习和使用面向对象的编程&#xff0c;我退后了一步来思考系统的复杂性。 “Complexity is anything that mak…

java writeint_Java DataOutputStream.writeInt(int v)类型

DataOutputStream.writeInt(int v)方法示例DataOutputStream的DataOutputStream.writeInt(int v)方法具有以下语法。public final void writeInt(int v) throws IOException示例在下面的代码中展示了如何使用DataOutputStream.writeInt(int v)方法。import java.io.DataInputSt…

协方差意味着什么_“零”到底意味着什么?

协方差意味着什么When I was an undergraduate student studying Data Science, one of my professors always asked the same question for every data set we worked with — “What does zero mean?”当我是一名研究数据科学的本科生时&#xff0c;我的一位教授总是对我们处…

Go_笔试题记录-不熟悉的

1、golang中没有隐藏的this指针&#xff0c;这句话的含义是&#xff08;&#xff09; A. 方法施加的对象显式传递&#xff0c;没有被隐藏起来 B. golang沿袭了传统面向对象编程中的诸多概念&#xff0c;比如继承、虚函数和构造函数 C. golang的面向对象表达更直观&#xff0c;对…

leetcode 316. 去除重复字母(单调栈)

给你一个字符串 s &#xff0c;请你去除字符串中重复的字母&#xff0c;使得每个字母只出现一次。需保证 返回结果的字典序最小&#xff08;要求不能打乱其他字符的相对位置&#xff09;。 注意&#xff1a;该题与 1081 https://leetcode-cn.com/problems/smallest-subsequenc…

Go-json解码到结构体

废话不多说&#xff0c;直接干就得了&#xff0c;上代码 package mainimport ("encoding/json""fmt" )type IT struct {Company string json:"company" Subjects []string json:"subjects"IsOk bool json:"isok"…

leetcode 746. 使用最小花费爬楼梯(dp)

数组的每个索引作为一个阶梯&#xff0c;第 i个阶梯对应着一个非负数的体力花费值 costi。 每当你爬上一个阶梯你都要花费对应的体力花费值&#xff0c;然后你可以选择继续爬一个阶梯或者爬两个阶梯。 您需要找到达到楼层顶部的最低花费。在开始时&#xff0c;你可以选择从索…

安卓中经常使用控件遇到问题解决方法(持续更新和发现篇幅)(在textview上加一条线、待续)...

TextView设置最多显示30个字符。超过部分显示...(省略号)&#xff0c;有人说分别设置TextView的android:signature"true",而且设置android:ellipsize"end";可是我试了。居然成功了&#xff0c;供大家參考 [java] view plaincopy<TextView android:id…

网络工程师晋升_晋升为工程师的最快方法

网络工程师晋升by Sihui Huang黄思慧 晋升为工程师的最快方法 (The Fastest Way to Get Promoted as an Engineer) We all want to live up to our potential, grow in our career, and do the best work of our lives. Getting promoted at work not only proves that we hav…

java 银行存取款_用Java编写银行存钱取钱

const readline require(‘readline-sync‘)//引用readline-synclet s 2;//错误的次数for (let i 0; i < 3; i) {console.log(‘请输入名&#xff1a;(由英文组成)‘);let user readline.question();console.log(‘请输入密码&#xff1a;(由数字组成)‘);let password …

垃圾邮件分类 python_在python中创建SMS垃圾邮件分类器

垃圾邮件分类 python介绍 (Introduction) I have always been fascinated with Google’s gmail spam detection system, where it is able to seemingly effortlessly judge whether incoming emails are spam and therefore not worthy of our limited attention.我一直对Goo…

leetcode 103. 二叉树的锯齿形层序遍历(层序遍历)

给定一个二叉树&#xff0c;返回其节点值的锯齿形层序遍历。&#xff08;即先从左往右&#xff0c;再从右往左进行下一层遍历&#xff0c;以此类推&#xff0c;层与层之间交替进行&#xff09;。例如&#xff1a; 给定二叉树 [3,9,20,null,null,15,7],3/ \9 20/ \15 7 返回…

简单易用的MongoDB

从我第一次听到Nosql这个概念到如今已经走过4个年头了&#xff0c;但仍然没有具体的去做过相应的实践。最近获得一段学习休息时间&#xff0c;购买了Nosql技术实践一书&#xff0c;正在慢慢的学习。在主流观点中&#xff0c;Nosql大体分为4类&#xff0c;键值存储数据库&#x…

html画布图片不显示_如何在HTML5画布上显示图像

html画布图片不显示by Nash Vail由Nash Vail Ok, so here’s a question: “Why do we need an article for this, Nash?”好的&#xff0c;这是一个问题&#xff1a;“为什么我们需要为此写一篇文章&#xff0c;纳什&#xff1f;” Well, grab a seat.好吧&#xff0c;坐下…

java断点续传插件_视频断点续传+java视频

之前仿造uploadify写了一个HTML5版的文件上传插件&#xff0c;没看过的朋友可以点此先看一下~得到了不少朋友的好评&#xff0c;我自己也用在了项目中&#xff0c;不论是用户头像上传&#xff0c;还是各种媒体文件的上传&#xff0c;以及各种个性的业务需求&#xff0c;都能得到…

全栈入门_启动数据栈入门包(2020)

全栈入门I advise a lot of people on how to build out their data stack, from tiny startups to enterprise companies that are moving to the cloud or from legacy solutions. There are many choices out there, and navigating them all can be tricky. Here’s a brea…

Go-json解码到接口及根据键获取值

Go-json解码到接口及根据键获取值 package mainimport ("encoding/json""fmt""github.com/bitly/go-simplejson" )type JsonServer struct {ServerName stringServerIP string }type JsonServers struct {Servers []JsonServer }func main() {…

C#接口的显隐实现

显示接口实现与隐式接口实现 何为显式接口实现、隐式接口实现&#xff1f;简单概括&#xff0c;使用接口名作为方法名的前缀&#xff0c;这称为“显式接口实现”&#xff1b;传统的实现方式&#xff0c;称为“隐式接口实现”。下面给个例子。 IChineseGreeting接口&#xff0c;…