golang struct组合,转型问题请教
type Action interface {
OnHurt2(other Action)
GetDamage() int
}
type Base struct {
atk, hp int
}
func (this *Base) OnHurt(other *Base) {
this.hp -= other.atk
}
func (this *Base) OnHurt2(other Action) {
this.hp -= other.GetDamage()
}
func (this *Base) GetDamage() int {
return 100
}
type Child struct {
Base
}
c1 := &Child{Base{atk: 20, hp: 100}}
c2 := &Child{Base{atk: 40, hp: 60}}
//这里报错cannot use type *Child to type *Base
c1.OnHurt(c1)
//要显示指定Base,有没有办法不显示指定
c1.OnHurt(&c2.Base)
//用一个接口来转型,但是这样的话一个接口会很臃肿。
//有没有好办法,帮忙改造下
c1.OnHurt2(c2)
入群交流(和以上内容无关):Go中文网 QQ 交流群:729884609 或加微信入微信群:274768166 备注:入群;关注公众号:Go语言中文网
是你自己在OnHurt里制定需要传入 Base啊……
能想到的做法有两个,一个是把base里的方法提取为一个interface
另一个版本就是做 base的Getter,比如 GetBase()*Base
然后加到action这个interface里面去。
golang struct组合,转型问题请教的更多相关文章
- Golang Struct 声明和使用
Golang Struct 声明和使用 Go可以声明自定义的数据类型,组合一个或多个类型,可以包含内置类型和用户自定义的类型,可以像内置类型一样使用struct类型 Struct 声明 具体的语法 t ...
- golang struct扩展函数参数命名警告
今天在使用VSCode编写golang代码时,定义一个struct,扩展几个方法,如下: package storage import ( "fmt" "github.c ...
- golang struct
ex1 /* https://golangbot.com/structs/ struct 结构 结构就是一组字段. */ package main import "fmt" // ...
- golang struct tag
golang可以在struct中的每个字段,写上一个tag.这个tag可以通过反射的机制获取到,最常用的场景就是json序列化和反序列化 package main import ( "enc ...
- [Golang] struct Tag说明
在处理json格式字符串的时候,经常会看到声明struct结构的时候,属性的右侧还有小米点括起来的内容.形如 type User struct { UserId int `json:"use ...
- golang struct 转map 及 map[string]*Struct 初始化和遍历
package main import ( "encoding/json" "errors" "fmt" "reflect&quo ...
- 【GoLang】GoLang struct 使用
代码示例: package main import "fmt" type Human struct { name string age int weight int } type ...
- Golang struct结构
结构struct Go中的struct与C中的struct非常相似,并且Go没有class,代替了class的位置,但并没有代替class的功能 使用type struct{} 定义结构,名称遵循可见 ...
- golang struct 和 byte互转
相比于encoding, 使用unsafe性能更高 type MyStruct struct { A int B int } var sizeOfMyStruct = int(unsafe.Sizeo ...
随机推荐
- Mybatis 动态SQL注解 in操作符的用法
在SQL语法中如果我们想使用in的话直接可以像如下一样使用: ,,) ; ,,) ; 但是如果在MyBatis中的使用 in 操作符,像下面这样写的话,肯定会报错: @Update("upd ...
- java-利用BitSet查找素数
高效存储为序列可以使用位积,由于位集将位包装在字节里,所以位集要比使用Boolean对象的ArrayList更高效. 自己的代码,素数是false public class Sieve { @Test ...
- ASP.Net用户控件的使用
一.概述: 与WEB窗体页相同,程序员可以使用任何文本编辑器创作用户控件,或者使用代码隐藏类开发用户控件.此外,与WEB窗体页一样,用户控件可以在第一次请求时被编译并存储在服务器内存中,从而缩短以后请 ...
- contenteditable属性让div也可以当做输入框
你知道div也可以当做输入框么? H5的全局属性contenteditable,带有contenteditable属性的div而不是input或者textarea来作为输入框(div可以根据内容自动调 ...
- mongodb中find $ne null 与$exists的区别
$ne null 会把空列表也算入,即使不存在. $exists 的识别效果就比较好 1.插入样例数据 db.nullexistsdemo.insertMany( [{ "name" ...
- BZOJ 3218 A + B Problem (可持久化线段树+最小割)
做法见dalao博客 geng4512的博客, 思路就是用线段树上的结点来进行区间连边.因为有一个只能往前面连的限制,所以还要可持久化.(duliu) 一直以来我都是写dinicdinicdinic做 ...
- Girls and Boys POJ - 1466 【(二分图最大独立集)】
Problem DescriptionIn the second year of the university somebody started a study on the romantic rel ...
- MySQL新特性文档型数据库
mongodb在文档型数据库这方面一直做的很好,也发展了很多年,MySQL作为一个比较大众的数据库也慢慢支持了该特性,下面介绍一下MySQL支持文档型数据库的简单操作. 环境: 主机名 IP 系统 软 ...
- CF1214B
CF1214B 解法: 暴力枚举,时间复杂度 $ O(n) $ CODE: #include<iostream> #include<cstdio> #include<cs ...
- Hdu 5344
Hdu5344 题意: 给你一个数组,求所有的 $ a_i + a_j $ 的异或值. 解法: 因为 $ (a_i+a_j) \bigoplus (a_j + a_i) = 0$ . 所以答案就是 $ ...