golang 接口(interface)

接口基础操作

package main

import(
	"fmt"
)
//声明接口
type Usb interface{
   //声明需要实现的方法
   //方法名(参数列表)返回值列表
   start()
   stop()
}
//接口执行工作
type Working struct{

}

func (Working Working) GetWorking(usb Usb){
	usb.start()
	usb.stop()
}


//方法实现start 和 stop 方法
type Phone struct{

}

func (phone Phone) start(){
	fmt.Println("手机连接开始。。。")
}

func (phone Phone) stop(){
	fmt.Println("手机链接结束。。。")
}


type Computer struct{

}

func (computer Computer) start(){
	fmt.Println("电脑连接开始。。。")
}

func (computer Computer) stop(){
	fmt.Println("电脑链接结束。。。")
}



func main(){
	working := Working{}
	phone := Phone{}
	computer := Computer{}
	//方法以形参的方式传入GetWorking中
	working.GetWorking(phone)
	working.GetWorking(computer)
	/*
	手机连接开始。。。
	手机链接结束。。。
	电脑连接开始。。。
	电脑链接结束。。。
	*/
}

注意事项

1.生成接口内不可以写属性(字段)

2.在继承多个接口时 父级接口中不可以有俩个相同的方法

接口案例:

package main

import(
	"fmt"
)
//声明一个结构体
type Monkey struct{
	Name string
}

func (this Monkey) climbing(){
	fmt.Println(this.Name," 生来就会爬树。。")
}


//继承Monkey
type LittleMonkey struct{
	Monkey
}

//接口
type FlyAble interface{
	 Flying()
}
//实现接口方法
func (this *LittleMonkey) Flying(){
	fmt.Println(this.Name," 努力学会了飞翔。。")
}

type FishAble interface{
	FishAble()
}

func (this *LittleMonkey) FishAble(){
	fmt.Println(this.Name," 努力又学会了游泳。。")
}





func main(){
	//创建一个实例
    LittleMonkey := LittleMonkey{
			Monkey{
				Name:"tom",
			},
	} 
	// LittleMonkey.Monkey.Name = "tom"
	//调用继承得climbing()的方法
	LittleMonkey.climbing()
	LittleMonkey.Flying()
	LittleMonkey.FishAble()
	/*
	结果
	many  生来就会爬树。。
	many  努力学会了飞翔。。  
	many  努力又学会了游泳。。
	*/
}

接口sort案例:

package main

import(
	"fmt"
	"math/rand"
	"sort"
)

type Student struct{
	Name string
	Age int
	Score float64
}

type HeroSlice []Student

//切片数量
func (hs HeroSlice) Len() int{
	return len(hs)
}

//排序规则
func (hs HeroSlice) Less(i, j int) bool{
	return hs[i].Score > hs[j].Score 
}

//数据对换
func (hs HeroSlice) Swap(i, j int){
	hs[i],hs[j] = hs[j],hs[i]
}




func main(){
    //添加切片数据
	var stu HeroSlice
	for i:= 0; i < 10; i++{
		//给结构体添加数据
		stu1 := Student{
           Name : fmt.Sprintf("tom~%d",rand.Intn(100)),
		   Age : rand.Intn(100),
		   Score : float64(i+1) + 0.1,
		}
		stu = append(stu,stu1)
	}

	for _,v := range stu{
		fmt.Println(v)
	}
	sort.Sort(stu)
	fmt.Println("排序后结果---------")
	for _,v := range stu{
		fmt.Println(v)
	}

	/*
	结果:
	{tom~81 87 1.1}
	{tom~47 59 2.1}
	{tom~81 18 3.1}
	{tom~25 40 4.1}
	{tom~56 0 5.1}
	{tom~94 11 6.1}
	{tom~62 89 7.1}
	{tom~28 74 8.1}
	{tom~11 45 9.1}
	{tom~37 6 10.1}
	排序后结果---------
	{tom~37 6 10.1}
	{tom~11 45 9.1}
	{tom~28 74 8.1}
	{tom~62 89 7.1}
	{tom~94 11 6.1}
	{tom~56 0 5.1}
	{tom~25 40 4.1}
	{tom~81 18 3.1}
	{tom~47 59 2.1}
	{tom~81 87 1.1}
	*/
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值