【Golang学习笔记】04

要点

  • go的方法
  • interface

go的方法

type Stu struct {
    name string
    age int
}

// 仅属于结构体Stu的方法
func (s *Stu)SetName(name string)  {
    s.name = name
}

func (s *Stu)SetAge(age int) int  {
    s.age = age
    return age
}

func main() {
    var s Stu
    s.SetName("ss")
    s.SetAge(11)
    fmt.Println(s)    // {ss 11}
}
  • (n *tye)中的 *
// 修改的是复制的n,而非原本的n
func (n MyInt)plusNoPtr()  {
    n++
    fmt.Printf("PlusNoPtr:  %p \n", &n)
}

func (n *MyInt)plusPtr()  {
    *n++
    fmt.Printf("Plusptr:  %p \n", n)
}

func (n *MyInt)Show()  {
    fmt.Printf("Num = %d", *n)
}

func main(){
    var num02 MyInt = 1
    num02.plusNoPtr()
    fmt.Printf("PlusNoPtr后:  %p, %d \n",&num02, num02)
    num02.plusPtr()
    fmt.Printf("PlusPtr后:  %p, %d \n",&num02, num02)
}

/*
    PlusNoPtr:  0xc04200e0d8    // 地址与其他三个不一样
    PlusNoPtr后:  0xc04200e0d0, 1 
    Plusptr:  0xc04200e0d0 
    PlusPtr后:  0xc04200e0d0, 2 
*/
// 不带*的话使用的是值,对其进行修改不会影响到原值,因为值传递需要复制整个值,所以应该使用指针。
  • *与非*的自我推导
    go的自我推导, MyInt 和 MyInt* 都可以调用 (MyInt)方法 和 (*MyInt)方法
func main(){
    var num03 *MyInt = new(MyInt)
    *num03 = 1
    num03.plusNoPtr()
    num03.plusPtr()
    fmt.Println(*num03)
    }
  • 在结构体内部使用
    type myMath struct {
        MyInt
    }

    func main(){
        mm := myMath{
        MyInt(1),
    }
    mm.Show()   //直接调用的是 MyInt的方法
    }
  • interface
    如果使用接口,那接口的声明要全部实现,而且参数返回值必须完全一致
    type myInterface interface {
        InFunc()
        Test() string
    }

    type myInt int

    func (n myInt)InFunc()  {
        fmt.Println("MyInt InFunc")
    }

    func (n myInt)Test()string  {
        fmt.Printf("MyInt string \n")
        return ""
    }

    func (n myInt)Test02()  {

    }

    type myStruct struct {

    }

    func (m myStruct)InFunc()  {
        fmt.Println("MyStruct InFunc")
    }

    func (m myStruct)Test()string  {
        fmt.Printf("MyStruct string \n")
        return ""
    }

    func Display(mi myInterface)  {
        mi.InFunc()
        mi.Test()
    }

    func main() {
        var a myInt
        Display(a)

        var b myStruct
        Display(b)
    }

    /*
        MyInt InFunc
        MyInt string 
        MyStruct InFunc
        MyStruct string 
    */
  • 接口的组合
type hand interface {
    grab()
}

type human interface {
    hand
    eat()
    }

type myInt int

func (n myInt)grab()  {

}

func (n myInt)eat()  {

}

func main() {
    var h human
    lh := myInt(1)
    h = lh
    h.grab()
    h.eat()

    var hh hand
    hh = h       // human包含hand
    hh.grab()

    //h = hh 编译不通过
    }
  • interface表示任意类型
    m := make(map[interface{}]interface{})
    m[1] = "a"
    m["2"] = 1

    stu := student{
        "aaa",
        120,
    }
    m[stu] = stu
    fmt.Println(m)    // map[{aaa 120}:{aaa 120} 1:a 2:1]
  • 类型断言
type student struct {
    name string
    age int
}

func main()  {
    var a interface{}
    a = "2"
    a = 1
    fmt.Println(a)

    b := 10
    a = b   // interface可以承载任意类型
    a = 20
//  b = a // cannot use a (type interface {}) as type int in assignment: need type assertion
    b,ok := a.(int) // 使用类型断言
    fmt.Println(b, ok)   // 20 true
}

你可能感兴趣的:(Golang学习笔记)