首页 > 编程语言 > 详细

go语言练习:接口

时间:2018-05-18 17:39:36      阅读:163      评论:0      收藏:0      [点我收藏+]
package main

import (
	"fmt"
)

type Run interface {              //这个接口的名字命名成Car更直观一点,除了distance方法外,后面可以加上百公里加速之类的方法
	distance() float64
    //celebrate() float64 } type Bwm struct { speed float64 name string t float64 } type Bens struct { speed float64 name string t float64 } func (bwm Bwm) distance() float64 { return bwm.speed*bwm.t } func (bens Bens) distance() float64 { return bens.speed*bens.t } func main() { x:=Bwm{100,"bwm",10} y:=Bens{120,"bens",10} fmt.Println(x.name,"has run:",Run.distance(x),"km in",x.t,"hours") fmt.Println(y.name,"has run:",Run.distance(y),"km in",y.t,"hours") }

  

go语言练习:接口

原文:https://www.cnblogs.com/chenadong/p/9057067.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!