wen*_*ner 2 polymorphism overriding proxy-classes go
package main
import "fmt"
func main() {
t16()
}
type Base struct {
val int
}
func (b *Base)Set(i int) {
b.val = i
}
type Sub struct {
Base
changed bool
}
func (b *Sub)Set(i int) {
b.val = i
b.changed = true
}
func t16() {
s := &Sub{}
s.Base.Set(1)
var b *Base = &s.Base
fmt.Printf("%+v\n", b)
fmt.Printf("%+v\n", s)
}
我想让Sub充当Base,但是当我调用Set时,对于Sub它会标记更改.我知道golang中没有多态或代理,但有没有办法做到这一点,而不影响Base?
更新
我希望当我调用Base.Set它将标记更改,对于用户,他们不知道他们实际上使用Sub,所以我可以监视Base行为.
func t16() {
s := &Sub{}
var b *Base = &s.Base
b.Set(10)
fmt.Printf("%+v\n", b)
fmt.Printf("%+v\n", s)
}