go_study/bilibili/aceid/struct/pair2/test.go
2024-11-18 12:28:32 +08:00

41 lines
622 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import "fmt"
type Reader interface {
ReadBook()
}
type Writer interface {
WriteBook()
}
// 具体类型
type Book struct {
}
func (this *Book) ReadBook() {
fmt.Println("Read a Book")
}
func (this *Book) WriteBook() {
fmt.Println("Write a Book")
}
func main() {
// b: pair<type: Book, value: book{}地址>
b := &Book{}
// r: pair<type: , value: >
var r Reader
// r: pair<type: Book, value: book{}地址>
r = b
r.ReadBook()
var w Writer
// r: pair<type: Book, value: book{}地址>
w = r.(Writer) // 此处的断方为什么会成功因为w r的具体type是一致的
w.WriteBook()
}