我试图更好地理解接口,而不是为什么s
没有领域Width
。我的例子在这里:
package main
import "fmt"
type shapes interface {
setWidth(float64)
}
type rect struct {
Width float64
}
func (r *rect) setWidth(w float64) {
r.Width = w
}
var allShapes = map[string]shapes{
"rect": &rect{},
}
func main() {
r := &rect{}
r.setWidth(5)
fmt.Println(r.Width) // this works
for _, s := range allShapes {
s.setWidth(7)
fmt.Println(s.Width) // why not???
}
}
为什么r
有宽度但s
没有宽度?我得到的确切错误是:
s.Width undefined (type shapes has no field or method Width)
shapes
接口是*rect
实现的对象,但不是具体类型*rect
。就像任何界面一样,它是一组允许任何满足其要求的类型通过的方法,例如给它添加临时访客标签以使其上楼。
例如,在Go的建筑物中,如果有一只猴子(或代表它的价值,它是一只海豚)可以行动并做人类可以做的一切,那么他可以越过警卫并上电梯。但是,这并不能使他从基因上成为人类。
Go是静态类型的,这意味着即使具有相同基础类型的两个类型在没有类型声明或有意识地转换类型的情况下也无法动态转换或强制转换为彼此。
var a int
type myInt int
var b myInt
a = 2
b = 3
b = a // Error! cannot use a (type int) as type myInt in assignment.
b = myInt(a) // This is ok.
和我一起想象一下这种情况:
type MyInt int
type YourInt int
type EveryInt interface {
addableByInt(a int) bool
}
func (i MyInt) addableByInt(a int) bool {
// whatever logic doesn't matter
return true
}
func (i YourInt) addableByInt(a int) bool {
// whatever logic doesn't matter
return true
}
func main() {
// Two guys want to pass as an int
b := MyInt(7)
c := YourInt(2)
// Do everything an `EveryInt` requires
// and disguise as one
bi := EveryInt(b)
ci := EveryInt(c)
// Hey, look we're the same! That's the closest
// we can get to being an int!
bi = ci // This is ok, we are EveryInt brotherhood
fmt.Println(bi) // bi is now 2
// Now a real int comes along saying
// "Hey, you two look like one of us!"
var i int
i = bi // Oops! bi has been made
// ci runs away at this point
}
现在回到您的场景-想象一个*circle
实现的实现shapes
:
type circle struct {
Radius float64
}
func (c *circle) setWidth(w float64) {
c.Radius = w
}
*circle
完全可以通过,shapes
但是它没有Width
属性,因为它不是*rect
。接口不能直接访问基础类型的属性,而只能通过已实现的方法集进行访问。为了访问属性,需要在接口上进行类型断言,以便实例成为具体的类型:
var r *rect
// Verify `s` is in fact a `*rect` under the hood
if r, ok := s.(*rect); ok {
fmt.Println(r.Width)
}
这就是为什么像Go这样的静态类型的语言总是比动态类型的语言快的原因的核心,而动态类型的语言几乎总是使用某种反射来为您动态处理类型强制。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句