与Go一起使用多态性
我正在尝试使用一些通用方法的基类,并创建另一个类,
package entity
import "fmt"
type Entity struct {
Name string
Health int
DamagePts int
}
func NewEntity(name string, health int, damagePts int) Entity {
entity := new(Entity)
entity.Name = name
entity.Health = health
entity.DamagePts = damagePts
return *entity
}
func (entity Entity) Attack(target *Entity) {
target.TakeDamage(entity.DamagePts)
}
func (entity Entity) Defend(target *Entity) {
target.TakeDamage(entity.DamagePts)
}
func (entity *Entity) TakeDamage(damage int) {
entity.Health -= damage
fmt.Println("Entity: ", entity.Name, " took ", damage, " damage")
if entity.Health < 0 {
entity.Health = 0
}
}
func (entity *Entity) ShowHealth() int {
return entity.Health
}
func (entity *Entity) Heal(health int) {
entity.Health += health
if entity.Health > 100 {
entity.Health = 100
}
}
func (entity Entity) IsDead() bool {
return false
}
func (entity Entity) Speak() {
fmt.Println("I am an entity")
}
该类别继承了基本方法的其他类 软件包怪物
import (
e "magickombat/internal/pkg/Entity"
)
type EntityInterface interface {
TakeDamage(damage int)
}
type Monster struct {
EntityInterface
e.Entity
Level int
}
func New(name string, health int, damagePts int) Monster {
monster := new(Monster)
monster.Entity = e.NewEntity(name, health, damagePts)
monster.Level = 1
return *monster
}
func (monster Monster) Attack(target interface{}) {
t := target.(e.Entity)
t.TakeDamage(monster.DamagePts)
}
func Defend[T e.Entity](target T) {
target.TakeDamage(45)
}
我尝试使用接口,但我有一个错误
内部/pkg/entity/monster/monster。
我在做什么错?
I'm trying to have a base class with some generic method and create an other class who inherit of the base's methods
package entity
import "fmt"
type Entity struct {
Name string
Health int
DamagePts int
}
func NewEntity(name string, health int, damagePts int) Entity {
entity := new(Entity)
entity.Name = name
entity.Health = health
entity.DamagePts = damagePts
return *entity
}
func (entity Entity) Attack(target *Entity) {
target.TakeDamage(entity.DamagePts)
}
func (entity Entity) Defend(target *Entity) {
target.TakeDamage(entity.DamagePts)
}
func (entity *Entity) TakeDamage(damage int) {
entity.Health -= damage
fmt.Println("Entity: ", entity.Name, " took ", damage, " damage")
if entity.Health < 0 {
entity.Health = 0
}
}
func (entity *Entity) ShowHealth() int {
return entity.Health
}
func (entity *Entity) Heal(health int) {
entity.Health += health
if entity.Health > 100 {
entity.Health = 100
}
}
func (entity Entity) IsDead() bool {
return false
}
func (entity Entity) Speak() {
fmt.Println("I am an entity")
}
The other class
package monster
import (
e "magickombat/internal/pkg/Entity"
)
type EntityInterface interface {
TakeDamage(damage int)
}
type Monster struct {
EntityInterface
e.Entity
Level int
}
func New(name string, health int, damagePts int) Monster {
monster := new(Monster)
monster.Entity = e.NewEntity(name, health, damagePts)
monster.Level = 1
return *monster
}
func (monster Monster) Attack(target interface{}) {
t := target.(e.Entity)
t.TakeDamage(monster.DamagePts)
}
func Defend[T e.Entity](target T) {
target.TakeDamage(45)
}
I try to use interfaces but I've an error
internal/pkg/Entity/Monster/Monster.go:30:9: target.TakeDamage undefined (type T has no field or method TakeDamage)
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如其他人指出的那样,存在多个问题。
内部 类型约束应该是接口。尝试以下操作:
并不能真正提供通常的OOP继承。
但是,一个实现了与接口的多态性,请参见下面的示例。
在
输出:
As others pointed out, there are multiple issues.
Here the issue is you used struct
Entity
as type constraint, but type constraint should be an interface. Try this:Go doesn't really provide the usual OOP inheritance.
However one achieve polymorphism with interfaces, see example below.
Play it on playground.
Output: