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)
}

猜你喜欢

转载自blog.csdn.net/woshiyuanlei/article/details/80322148
今日推荐