面向对象的三大特征:

  1. 封装:隐藏对象的属性和实现细节,仅对外提供公共访问方式
  2. 继承:使得子类具有父类的属性和方法或者重新定义、追加属性和方法等
  3. 多态:不同对象中同种行为的不同实现方式
struct

Go语言没有“类”的概念,也不支持“类”的继承等面向对象的概念。Go语言中通过结构体的内嵌再配合接口比面向对象具有更高的扩展性和灵活性。

结构体和方法

type Employee struct {  
    FirstName   string
    LastName    string
    TotalLeaves int
    LeavesTaken int
}

func (e Employee) LeavesRemaining() {  
    fmt.Printf("%s %s has %d leaves remaining", e.FirstName, e.LastName, (e.TotalLeaves - e.LeavesTaken))
}

func main() {  
    e := employee.Employee {
        FirstName: "Sam",
        LastName: "Adolf",
        TotalLeaves: 30,
        LeavesTaken: 20,
    }
    e.LeavesRemaining()
}

New()函数

Java语言中提供了构造方法创建并初始化对象,在Go语言中一般需要自己实现一个对外可见的New函数

func main() {  
    var e Employee
    e.LeavesRemaining()
}

运行结果:

has 0 leaves remaining
java
NewT(parameters)NewT(parameters)New(parameters)NewT(parameters)

首先修改employee结构体为非导出,并创建一个函数New(),它将创建一个新Employee:

type employee struct {  
    firstName   string
    lastName    string
    totalLeaves int
    leavesTaken int
}

func New(firstName string, lastName string, totalLeave int, leavesTaken int) employee {  
    e := employee {firstName, lastName, totalLeave, leavesTaken}
    return e
}

func (e employee) LeavesRemaining() {  
    fmt.Printf("%s %s has %d leaves remaining", e.firstName, e.lastName, (e.totalLeaves - e.leavesTaken))
}
Employee struct
employeeemployeeemployee
func main() {  
    e := employee.New("Sam", "Adolf", 30, 20)
    e.LeavesRemaining()
}

运行结果:

Sam Adolf has 10 leaves remaining 

因此,虽然Go不支持类,但是结构体可以有效地使用,在使用构造函数的位置,使用New(parameters)的方法即可。


组合与继承

extends

举一个博客文章例子:每个博客都有标题、内容和作者信息。这可以用组合完美地表示出来。

嵌入结构体实现组合

struct
/*
创建了一个author struct,它包含字段名、lastName和bio。添加了一个方法fullName(),将作者作为接收者类型,这将返回作者的全名。
*/
type author struct {  
    firstName string
    lastName  string
    bio       string
}

func (a author) fullName() string {  
    return fmt.Sprintf("%s %s", a.firstName, a.lastName)
}
/*
post struct有字段标题、内容。它还有一个嵌入式匿名字段作者。这个字段表示post struct是由author组成的。现在post struct可以访问作者结构的所有字段和方法。还在post struct中添加了details()方法
*/
type post struct {  
    title     string
    content   string
    author
}

func (p post) details() {  
    fmt.Println("Title: ", p.title)
    fmt.Println("Content: ", p.content)
    fmt.Println("Author: ", p.author.fullName())
    fmt.Println("Bio: ", p.author.bio)
}

func main() {  
    author1 := author{
        "Naveen",
        "Ramanathan",
        "Golang Enthusiast",
    }
    post1 := post{
        "Inheritance in Go",
        "Go supports composition instead of inheritance",
        author1,
    }
    post1.details()
}

运行结果:

Title:  Inheritance in Go  
Content:  Go supports composition instead of inheritance  
Author:  Naveen Ramanathan  
Bio:  Golang Enthusiast  

嵌入结构体的切片

在以上程序的main函数下增加以下代码,并运行

type website struct {  
        []post
}
func (w website) contents() {  
    fmt.Println("Contents of Website\n")
    for _, v := range w.posts {
        v.details()
        fmt.Println()
    }
}

运行报错:

syntax error: unexpected [, expecting field name or embedded type 
structs []post
type website struct {  
        posts []post
}

修改完完整代码如下:

type author struct {  
    firstName string
    lastName  string
    bio       string
}

func (a author) fullName() string {  
    return fmt.Sprintf("%s %s", a.firstName, a.lastName)
}

type post struct {  
    title   string
    content string
    author
}
func (p post) details() {  
    fmt.Println("Title: ", p.title)
    fmt.Println("Content: ", p.content)
    fmt.Println("Author: ", p.fullName())
    fmt.Println("Bio: ", p.bio)
}

type website struct {  
 	posts []post
}
func (w website) contents() {  
    fmt.Println("Contents of Website\n")
    for _, v := range w.posts {
        v.details()
        fmt.Println()
    }
}
func main() {  
    author1 := author{
        "Naveen",
        "Ramanathan",
        "Golang Enthusiast",
    }
    post1 := post{
        "Inheritance in Go",
        "Go supports composition instead of inheritance",
        author1,
    }
    post2 := post{
        "Struct instead of Classes in Go",
        "Go does not support classes but methods can be added to structs",
        author1,
    }
    post3 := post{
        "Concurrency",
        "Go is a concurrent language and not a parallel one",
        author1,
    }
    w := website{
        posts: []post{post1, post2, post3},
    }
    w.contents()
}   

运行结果:

Contents of Website

Title:  Inheritance in Go  
Content:  Go supports composition instead of inheritance  
Author:  Naveen Ramanathan  
Bio:  Golang Enthusiast

Title:  Struct instead of Classes in Go  
Content:  Go does not support classes but methods can be added to structs  
Author:  Naveen Ramanathan  
Bio:  Golang Enthusiast

Title:  Concurrency  
Content:  Go is a concurrent language and not a parallel one  
Author:  Naveen Ramanathan  
Bio:  Golang Enthusiast  

接口与多态

Go中的多态性(Polymorphism)是在接口的帮助下实现的。接口可以在Go中隐式地实现。如果类型为接口中声明的所有方法提供了定义,则该类型实现了这个接口。

任何定义接口所有方法的类型都被称为隐式地实现该接口。

类型接口的变量可以保存实现接口的任何值。接口的这个属性用于实现Go中的多态性。

SquareRectangle
// 正方形
type Square struct {
    side float32
}

// 长方形
type Rectangle struct {
    length, width float32
}
Area()
Area()ShaperSquareRectangleArea()
// 接口 Shaper
type Shaper interface {
    Area() float32
}

// 计算正方形的面积
func (sq *Square) Area() float32 {
    return sq.side * sq.side
}

// 计算长方形的面积
func (r *Rectangle) Area() float32 {
    return r.length * r.width
}
main()Area()
func main() {
    r := &Rectangle{10, 2}
    q := &Square{10}

    // 创建一个 Shaper 类型的数组
    shapes := []Shaper{r, q}
    // 迭代数组上的每一个元素并调用 Area() 方法
    for n, _ := range shapes {
        fmt.Println("图形数据: ", shapes[n])
        fmt.Println("它的面积是: ", shapes[n].Area())
    }
}

/*Output:
图形数据:  &{10 2}
它的面积是:  20
图形数据:  &{10}
它的面积是:  100
*/
Area()

总结

gettersetter