以下 Go 代码:
package main
import "fmt"
type Polygon struct {
sides int
area int
}
type Rectangle struct {
Polygon
foo int
}
type Shaper interface {
getSides() int
}
func (r Rectangle) getSides() int {
return 0
}
func main() {
var shape Shaper = new(Rectangle)
var poly *Polygon = new(Rectangle)
}
导致此错误:
cannot use new(Rectangle) (type *Rectangle) as type *Polygon in assignment
我不能像在 Java 中那样将 Rectangle 实例分配给 Polygon 引用。这背后的原理是什么?
慕雪6442864
相关分类