go 3.37 面向对象
创建时间:
阅读:
同名字段和结构体匿名字段
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package main
import "fmt"
type person struct { name string sex byte age int }
type student struct { person id int name string addr string }
func main() { var s student s.name = "mike" s.sex = 'm' s.age = 18 s.addr = "bj" s.person.name = "jack" fmt.Printf("s = %+v\n", s) }
|
结构体指针类型匿名字段
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package main
import "fmt"
type person struct { name string sex byte age int }
type student struct { *person id int addr string }
func main() { s1 := student{&person{"mike", 'm', 18}, 666, "bj"} fmt.Println(s1.name, s1.sex, s1.age, s1.id, s1.addr) var s student s.person = new(person) s.name = "yoyo" s.sex = 'f' s.age = 19 s.addr = "sz" fmt.Println(s.name, s.sex, s.age, s.id, s.addr) }
|
面向过程与面向对象的区别
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| func Add01(a, b int) int { return a + b }
type v int
func (tmp v) Add02(another v) v { return tmp + another }
func main() { var result int a := 10 b := 20 result = Add01(a, b) fmt.Println("result = ", result)
var obj v = 10 r := obj.Add02(100) fmt.Println("obj result = ", r) }
|
方法表达式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package main
import "fmt"
type person struct { name string sex byte age int }
func (p person) setInfoValue() { fmt.Printf("setInfoValue:%p ,%v\n", &p, p) }
func (p *person) setInfoPointer() { fmt.Printf("setInfoPointer:%p ,%v\n", p, p) }
func main() { p := person{"mike", 'm', 18} f1 := p.setInfoPointer f1()
f2 := (person).setInfoValue f2(p)
f3 := (*person).setInfoPointer f3(&p)
}
|
接口定义和实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| type Human interface { sayhi() }
type Student struct { name string id int }
func (tmp *Student) sayhi() { fmt.Printf("Student [%s,%d] sayhi\n", tmp.name, tmp.id) }
type Teacher struct { addr string group string }
func (tmp *Teacher) sayhi() { fmt.Printf("Teacher [%s,%s] sayhi\n", tmp.addr, tmp.group) }
type MyStr string
func (tmp *MyStr) sayhi() { fmt.Printf("MyStr [%s] sayhi\n", *tmp) }
func main() { var i Human s := Student{"mike", 666} i = &s i.sayhi()
t := &Teacher{"bj","go"} i = t i.sayhi()
var str MyStr = "HELLO STR" i = &str i.sayhi() }
|
多态的表现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| package main
import "fmt"
func whoSayHi(i Human) { i.sayhi() }
type Human interface { sayhi() }
type Student struct { name string id int }
func (tmp *Student) sayhi() { fmt.Printf("Student [%s,%d] sayhi\n", tmp.name, tmp.id) }
type Teacher struct { addr string group string }
func (tmp *Teacher) sayhi() { fmt.Printf("Teacher [%s,%s] sayhi\n", tmp.addr, tmp.group) }
type MyStr string
func (tmp *MyStr) sayhi() { fmt.Printf("MyStr [%s] sayhi\n", *tmp) }
func main() { s := &Student{"mike", 666} t := &Teacher{"bj", "go"} var str MyStr = "HELLO STR" whoSayHi(s) whoSayHi(t) whoSayHi(&str)
x := make([]Human, 3) x[0] = s x[0] = t x[0] = &str for _, i := range x { i.sayhi() } }
|
接口继承
4.22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| type Human interface { sayhi() }
type Person interface { sing(lrc string) } type Student struct { name string id int }
func (tmp *Student) sayhi() { fmt.Printf("Student [%s,%d] sayhi\n", tmp.name, tmp.id) }
func (tmp *Student) sing(lrc string) { fmt.Printf("Student [%s,%d] sing %s\n", tmp.name, tmp.id, lrc) }
func main() { var i Person
s := &Student{"mike", 666} i = s i.sayhi() i.sing("学生歌...") }
|
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 rootid@126.com
文章标题:go 3.37 面向对象
本文作者:Shawn
发布时间:2019-05-31, 19:04:26
最后更新:2019-07-30, 17:24:30
原始链接:https://gitrootid.github.io/2019/05/31/golang/go-4-02-面向对象_/
版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。