golang 中的多态用法

package main

import "fmt"


/**
 *多态用法
 */
type Draw interface{
     Paint()
}


type Circular struct{
     Name string 
}

type Triangular struct{
     Name string
}


func (c *Circular) Paint(){
     fmt.Println("c:",c.Name)
}

func (t *Triangular) Paint(){
     fmt.Println("t:",t.Name)
}

func main(){
     var  draw Draw

	 draw = &Circular{"画一个圆形"}
	 draw.Paint()
	 draw = &Triangular{"画一个三角形"}
	 draw.Paint()
}

你可能感兴趣的:(go)