SEE: //定義的方法 「獲得()UINT64」 爲價值二進制 FUNC(我二進制)GET(){UINT64回報 UINT64(I) }誰在爲類型轉換定義Get()方法? Golang本身?
我們定義了一個get()方法和顯然b:=二進制(200)執行它, 但那裏之間的連接。我沒有找到Get()的定義,我認爲這個定義覆蓋了一個內部定義,或者我錯了。
package main
import (
"fmt"
"strconv"
)
// This defines a interface with one method: "String() string"
type Stringer interface {
String() string
}
// Defines an unsigned 64 bit number
type Binary uint64
// Defines a method "String() string" for value Binary
func (i Binary) String() string {
return strconv.FormatUint(i.Get(), 2)
}
// Defines a method "Get() uint64" for value Binary
func (i Binary) Get() uint64 {
return uint64(i)
}
// Main program
func main() {
b := Binary(200) // Create Unsigned value of 200
s := Stringer(b) // Calls interface "Stringer" on "b"
fmt.Println(s.String()) // "s" is an Stringer Interface Type
}
「我還沒有找到Get()被定義在哪裏」 - 但是你可以定義它。它就在那裏,在說'定義一個方法'的評論下,獲取()uint64「作爲值Binary'。你爲什麼困惑? – user2357112
'Binary(200)'不會調用'Get',這是類型轉換,因爲'Binary'是'uint64'的別名。 'Stringer(b)'不會調用任何東西,這是自''Binary'實現'Stringer'接口以來的類型轉換。 – creker