1. package main
    2. import "fmt"
    3. //接口
    4. //本质上是一个指针
    5. type Animal interface {
    6. Sleep()
    7. GetColor() string
    8. }
    9. //具体的类
    10. type Cat struct {
    11. color string
    12. }
    13. func (this *Cat) Sleep() {
    14. fmt.Println("cat sleep...")
    15. }
    16. func (this *Cat) GetColor() string {
    17. return this.color
    18. }
    19. func showAnimal(animal Animal) {
    20. animal.Sleep()
    21. fmt.Println(animal.GetColor())
    22. }
    23. func main() {
    24. var animal Animal
    25. animal = &Cat{"black"} //多态
    26. animal.Sleep()
    27. fmt.Println("-----------------------")
    28. cat:=Cat{"white"}
    29. showAnimal(&cat) //对象地址传递
    30. }