golang面向对象思想和实现

golang中并无明确的面向对象的说法,实在要扯上的话,能够将struct比做其它语言中的class。php

类声明

type Poem struct {
	Title  string
	Author string
	intro  string
}


这样就声明了一个类,其中没有public、protected、private的的声明。golang用另一种作法来实现属性的访问权限:属性的开头字母是大写的则在其它包中能够被访问,不然只能在本包中访问。类的声明和方法亦是如此。java

类方法声明

func (poem *Poem) publish() {
	fmt.Println("poem publish")
}

或者golang

func (poem Poem) publish() {
	fmt.Println("poem publish")
}

和其它语言不同,golang声明方法和普通方法一致,只是在func后增长了poem *Poem这样的声明。加*和没有加*的区别在于一个是传递指针对象,一个是传递值对象。函数

实例化对象

实例化对象有好几种方式spa

poem := &Poem{}
	poem.Author = "Heine"
	poem2 := &Poem{Author: "Heine"}
	poem3 := new(Poem)
	poem3.Author = "Heine"
	poem4 := Poem{}
	poem4.Author = "Heine"
	poem5 := Poem{Author: "Heine"}

实例化的时候能够初始化属性值,若是没有指明则默认为系统默认值设计

加&符号和new的是指针对象,没有的则是值对象,这点和php、java不一致,在传递对象的时候要根据实际状况来决定是要传递指针仍是值。指针

tips:当对象比较小的时候传递指针并不划算。code

构造函数

查看官方文档,golang并无构造函数一说。若是必定要在初始化对象的时候进行一些工做的话,能够自行封装产生实例的方法。对象

func NewPoem(param string, p ...interface{}) *Poem

示例:继承

func NewPoem(author string) (poem *Poem) {
    poem = &Poem{}
    poem.Author = author
    return
}
 
poem6 := NewPoem("Heine")

继承

确切的说golang中叫作组合(composition)

type Poem struct {
    Title  string
    Author string
    intro  string
}
 
type ProsePoem struct {
    Poem
    Author string
}

ProsePoem属性中声明了Poem,表示组合了Poem的属性和方法。能够像以下方式调用:

prosePoem := &ProsePoem{}
prosePoem.author = "Heine"

若是其中属性有冲突,则之外围的为主。

type ProsePoem struct {
    Poem
    Author string
}

当访问Author的时候默认为ProsePoem的Author,若是须要访问Poem的Author属性可使用prosePoem.Poem.Author来访问。

prosePoem := &ProsePoem{}
prosePoem.Author = "Shelley"
prosePoem.Poem.Author = "Heine"
fmt.Println(prosePoem)

从输出中能够很直观看到这一点。

&{{ Heine } Shelley}

方法的继承和属性一致,这里再也不罗列。经过组合的话能够很好的实现多继承。

方法重载

方法重载就是一个类中能够有相同的函数名称,可是它们的参数是不一致的,在java、C++中这种作法广泛存在。golang中若是尝试这么作会报从新声明(redeclared)错误,可是golang的函数能够声明不定参数,这个很是强大。

func (poem *Poem) recite(v ...interface{}) {
    fmt.Println(v)
}

其中v …interface{}表示参数不定的意思,其中v是slice类型,fmt.Println方法也是这样定义的。若是要根据不一样的参数实现不一样的功能,要在方法内检测传递的参数。

接口

关于面向对象中还一个重要的东西就是接口了,golang中的接口和其它语言都不太同样,是golang值得称道设计之一。详细了解接口还须要一段时间,下次再分享吧。

相关文章
相关标签/搜索