go基础 三 结构体和oop,接口

结构体:

  Go 语言中数组可以存储同一类型的数据,但在结构体中我们可以为不同项定义不同的数据类型。
  结构体是由一系列具有相同类型或不同类型的数据构成的数据集合。

package main

import "fmt"
type Books struct {
    title string
    author string
    subject string
    book_id int
}
func main() {
    var Book1 Books        /* 声明 Book1 为 Books 类型 */
    var Book2 Books        /* 声明 Book2 为 Books 类型 */

    /* book 1 描述 */
    Book1.title = "Go 语言"
    Book1.author = "www.runoob.com"
    Book1.subject = "Go 语言教程"
    Book1.book_id = 1234

    /* book 2 描述 */
    Book2.title = "Python 教程"
    Book2.author = "www.runoob.com"
    Book2.subject = "Python 语言教程"
    Book2.book_id = 12345

    /* 打印 Book1 信息 */
    fmt.Printf( "Book 1 title : %s
", Book1.title)
    fmt.Printf( "Book 1 author : %s
", Book1.author)
    fmt.Printf( "Book 1 subject : %s
", Book1.subject)
    fmt.Printf( "Book 1 book_id : %d
", Book1.book_id)

    /* 打印 Book2 信息 */
    fmt.Printf( "Book 2 title : %s
", Book2.title)
    fmt.Printf( "Book 2 author : %s
", Book2.author)
    fmt.Printf( "Book 2 subject : %s
", Book2.subject)
    fmt.Printf( "Book 2 book_id : %d
", Book2.book_id)
}

再看案例:

package main

import "fmt"

type Address struct {
    province, city string
}
type Person struct {
    name string
    age int
    address Address
}
func main() {
    // 模拟对象之间的聚合关系
    p := Person{}
    p.name = "Steven"
    p.age = 35
    // 赋值⽅方式 1
    addr := Address{}
    addr.province = "北北京市"
    addr.city = "海海淀区"
    p.address = addr
    fmt.Println(p)
    fmt.Println("姓名:", p.name)
    fmt.Println("年年龄:", p.age)
    fmt.Println("省:", p.address.province)
    fmt.Println("市:", p.address.city)
    fmt.Println("---------------------")
    // 修改 Person 对象的数据,是否影响 Address 对象?
    p.address.city = "昌平区"
    fmt.Println("姓名:", p.name)
    fmt.Println("年年龄:", p.age)
    fmt.Println("省:", p.address.province)
    fmt.Println("市:", p.address.city)
    fmt.Println("---------------------")
    fmt.Println("市:", addr.city) // 没有影响
    // 修改 Address 对象的数据,是否影响 Person 对象?
    addr.city = "⼤大兴区"
    fmt.Println("姓名:", p.name)
    fmt.Println("年年龄:", p.age)
    fmt.Println("省:", p.address.province)
    fmt.Println("市:", p.address.city)
    fmt.Println("---------------------")
    // 赋值⽅方式 2 :
    p.address = Address{
        province: "陕⻄西省",
        city: "⻄西安市",
    }
    fmt.Println(p)
    fmt.Println("姓名:", p.name)
    fmt.Println("年年龄:", p.age)
    fmt.Println("省:", p.address.province)
    fmt.Println("市:", p.address.city)
    fmt.Println("---------------------")
}

oop

  go语言跟java不同,go语言没有继承,多态,只有封装,

接口:

  面向对象世界中的接口的一般定义是“接口定义对象的行为”。它表示让指定对象应该做什么。实现这种行为的方法(实现细节)是针对对象的。

  在Go中,接口是一组方法签名。当类型为接口中的所有方法提供定义时,它被称为实现接口。它与OOP非常相似。接口指定了类型应该具有的方法,类型决定了如何实现这些方法。

  

/* 定义接口 */
type interface_name interface {
   method_name1 [return_type]
   method_name2 [return_type]
   method_name3 [return_type]
   ...
   method_namen [return_type]
}

/* 定义结构体 */
type struct_name struct {
   /* variables */
}

/* 实现接口方法 */
func (struct_name_variable struct_name) method_name1() [return_type] {
   /* 方法实现 */
}
...
func (struct_name_variable struct_name) method_namen() [return_type] {
   /* 方法实现*/
}
package main
import (
    "fmt"
)

type Phone interface {
    call()
}

type NokiaPhone struct {
}

func (nokiaPhone NokiaPhone) call() {
    fmt.Println("I am Nokia, I can call you!")
}

type IPhone struct {
}

func (iPhone IPhone) call() {
    fmt.Println("I am iPhone, I can call you!")
}

func main() {
    var phone Phone

    phone = new(NokiaPhone)
    phone.call()

    phone = new(IPhone)
    phone.call()

}
原文地址:https://www.cnblogs.com/xiufengchen/p/12261004.html