golang 类/type
Contents
golang 类/type
|
|
类声明
type Poem struct {
Title string
Author string
intro string
}
这样就声明了一个类,其中没有public、protected、private的的声明。golang用另外一种做法来实现属性的访问权限: 属性的开头字母是大写的则在其它包中可以被访问,否则只能在本包中访问。类的声明和方法亦是如此。
类方法声明
func (poem *Poem) publish() {
fmt.Println(“poem publish”)
}
或者
func (poem Poem) publish() {
fmt.Println(“poem publish”)
}
实例化对象
实例化对象有好几种方式
poem := &Poem{}
poem.Author = “Heine”
poem2 := &Poem{Author: “Heine”}
poem3 := new(Poem)
poem3.Author = “Heine”
poem4 := Poem{}
poem4.Author = “Heine”
poem5 := Poem{Author: “Heine”}
http://kangkona.github.io/oo-in-golang
http://www.01happy.com/golang-oop/
https://code.tutsplus.com/zh-hans/tutorials/lets-go-object-oriented-programming-in-golang-cms-26540
http://hackthology.com/golangzhong-de-mian-xiang-dui-xiang-ji-cheng.html
http://hackthology.com/golangzhong-de-mian-xiang-dui-xiang-ji-cheng.html
Author -
LastMod 2017-02-17