• 多态


    package main
    import (
        "fmt"
    )
    
    //声明/定义一个接口
    type Usb interface {
        //声明了两个没有实现的方法
        Start() 
        Stop()
    }
    
    
    //声明/定义一个接口
    type Usb2 interface {
        //声明了两个没有实现的方法
        Start() 
        Stop()
        Test()
    }
    
    
    
    type Phone struct {
    
    }  
    
    //让Phone 实现 Usb接口的方法
    func (p Phone) Start() {
        fmt.Println("手机开始工作。。。")
    }
    func (p Phone) Stop() {
        fmt.Println("手机停止工作。。。")
    }
    
    type Camera struct {
    
    }
    //让Camera 实现   Usb接口的方法
    func (c Camera) Start() {
        fmt.Println("相机开始工作~~~。。。")
    }
    func (c Camera) Stop() {
        fmt.Println("相机停止工作。。。")
    }
    
    
    //计算机
    type Computer struct {
    
    }
    
    //编写一个方法Working 方法,接收一个Usb接口类型变量
    //只要是实现了 Usb接口 (所谓实现Usb接口,就是指实现了 Usb接口声明所有方法)
    func (c Computer) Working(usb Usb) {
    
        //通过usb接口变量来调用Start和Stop方法
        usb.Start()
        usb.Stop()
    }
    
    func main() {
    
        //测试
        //先创建结构体变量
        computer := Computer{}
        phone := Phone{}
        camera := Camera{}
    
        //关键点
        computer.Working(phone)
        computer.Working(camera) //
    }
    

      

  • 相关阅读:
    scipy.spatial.distance.cdist
    关于hstack和Svstack
    numpy.hstack(tup)
    numpy.random.uniform(记住文档网址)
    Python集合(set)类型的操作
    python+Eclipse+pydev环境搭建
    python数据挖掘领域工具包
    LVS 命令使用
    CMD mysql 备份脚本
    Windos Server Tomcat 双开配置
  • 原文地址:https://www.cnblogs.com/sunlong88/p/11180763.html
Copyright © 2020-2023  润新知