Golang继承,指针与非指针的区别

package main

import "fmt"
import "time"

type cc interface{
   MyPrint()
   MyPrint2()
}

type cc1 struct{
   a int
}
type cc2 struct{
   *cc1
}
type cc3 struct{
   cc1
}

func(c *cc1)MyPrint(){
   fmt.Println("cc1 myprint.")
}

func(c cc1)MyPrint2(){
   fmt.Println("--------cc1 myprint2.")
}

func(c *cc2)MyPrint(){
   fmt.Println("cc2 myprint.")
}

func(c *cc3)MyPrint(){
   fmt.Println("cc3 myprint.")
}


func main(){
   defer func() {
      if err := recover(); err != nil{
         fmt.Println("recover err:", err)
         duration := time.Duration(10)*time.Second
         time.Sleep(duration)
      }
   }()

   c := &cc1{}
   c.MyPrint()
   (*c).MyPrint()

   c2 := &cc2{}
   c2.MyPrint()
   (*c2).MyPrint()
   c22 := cc2{}
   c22.MyPrint()
   //c2.MyPrint2() //error
   //(*c2).MyPrint2() //error
   //c22.MyPrint2() //error

   c3 := &cc3{}
   c3.MyPrint()
   (*c3).MyPrint()
   c33 := cc3{}
   c33.MyPrint()
   c3.MyPrint2()
   (*c3).MyPrint2()
   c33.MyPrint2()

   duration := time.Duration(300)*time.Second
   time.Sleep(duration)
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页