当前位置: 首页 > news >正文

Golang面向对象

继承的基本用法

package mainimport "fmt"type Human struct {name stringsex  string
}func (this *Human) Eat() {fmt.Println("Human is eating")
}func (this *Human) Walk() {fmt.Println("Human is walking")
}type Superman struct {Human //SuperMan继承了Human类的方法level int
}//重写父类的Eat方法
func (this *Superman) Eat() {fmt.Println("Superman is eating")
}//子类的新方法
func (this *Superman) Fly() {fmt.Println("Superman is flying")
}func (this *Superman) Print() {fmt.Println("Superman is ", this.name, " and level is ", this.level)
}func main() {h := Human{name: "John", sex: "Male"}h.Eat()h.Walk()superman := Superman{Human{"Superman", "Male"}, 10}superman.Eat()  //调用子类的Eat方法superman.Walk() //调用父类的Walk方法superman.Fly()  //调用子类的Fly方法superman.Print()var s Supermans.name = "Superman200"s.sex = "Male1"s.level = 11s.Eat()s.Walk()s.Fly()s.Print()
}
-----------------------------------------------------------------
PS D:\GoProject\firstGoProject> go run firstGoProject.go
Human is eating
Human is walking
Superman is eating
Human is walking
Superman is flying
Superman is  Superman  and level is  10   
Superman is eating
Human is walking
Superman is flying
Superman is  Superman200  and level is  11

多态

基本要素:

  1. 有一个父类(接口)
  2. 有子类(实现了父类的全部接口方法)
  3. 父类类型的变量(指针)指向(引用)子类的具体数据变量
//本质是一个指针
type AnimalIF interface {Sleep()GetColor() string //返回颜色GetType() string  //返回种类
}//-----------------------------------------//定义具体的类
type Cat struct {color string //颜色
}//全部重写接口的方法表示实现接口,如果接口中的一个方法没有重写,则表示该接口不能被实现
func (c *Cat) Sleep() {println("Cat is sleeping")
}func (c *Cat) GetColor() string {return c.color
}func (c *Cat) GetType() string {return "Cat"
}//定义具体的类
type Dog struct {color string //颜色
}func (c *Dog) Sleep() {println("Dog is sleeping")
}func (c *Dog) GetColor() string {return c.color
}func (c *Dog) GetType() string {return "Dog"
}//-----------------------------------------func showAnimal(animal AnimalIF) {animal.Sleep() //调用的是AnimalIF的Sleep方法,多态的现象fmt.Println(animal.GetColor())fmt.Println(animal.GetType())
}func main() {var animal AnimalIF //接口的数据类型,父类指针animal = &Cat{"white"}animal.Sleep() //调用的是Cat的Sleep方法,多态的现象fmt.Println(animal.GetColor())fmt.Println(animal.GetType())animal = &Dog{"yellow"}animal.Sleep() //调用的是Dog的Sleep方法,多态的现象fmt.Println(animal.GetColor())fmt.Println(animal.GetType())//-----------------------------------------------cat := Cat{"white"}showAnimal(&cat) //传入的是Cat的指针,调用的是Cat的Sleep方法,多态的现象dog := Dog{"yellow"}showAnimal(&dog) //传入的是Dog的指针,调用的是Dog的Sleep方法,多态的现象}
PS D:\GoProject\firstGoProject> go run firstGoProject.go
Cat is sleeping
white
Cat
Dog is sleeping
yellow
Dog
Cat is sleeping
white
Cat
Dog is sleeping
yellow
Dog

interface 通用万能类型

interface{}

  • 空接口
  • int、string、float32、struct...都实现了interface{}
  • 可以用interface{}类型  引用任意的数据类型
  • 通过 “类型断言” 可以判断此时引用的底层数据类型
import "fmt"//interface{}是万能的类型,可以用来表示任何类型。
func myFunc(arg interface{}) {fmt.Println("myFunc is called...")fmt.Println(arg)//interface{} 如何区分 此时引用的底层数据类型?//interface{} 提供“类型断言”的机制//通过类型断言,可以判断接口变量的底层数据类型,并执行相应的操作。value, ok := arg.(string)if ok {fmt.Printf("value type is %T\n", value)} else {fmt.Println("arg is not a string")}
}type Book struct {auth string
}func main() {book := Book{"Golang"}myFunc(book)myFunc(100)myFunc("Hello,World!")myFunc(3.14)
}
----------------------------------------
PS D:\GoProject\firstGoProject> go run firstGoProject.go
myFunc is called...
{Golang}
arg is not a string
myFunc is called...
100
arg is not a string
myFunc is called...
Hello,World!
value type is string
myFunc is called...
3.14
arg is not a string
http://www.dtcms.com/a/466208.html

相关文章:

  • 从零开始学Wordpress建站杭州建筑工程网
  • 网站品牌词如何优化wordpress 微博客
  • Spring中Date日期序列化与反序列化中格式设置
  • 网站切图怎么收费wordpress前端怎么写
  • 关于网站开发的技术博客智能家庭app下载
  • LLM学习笔记5——本地部署ComfyUI和Wan2.1-T2V-1.3B文生视频模型
  • 广州番禺网站制作公司哪家好网站公司 北京
  • 内网穿透技术保障物联网设备数据安全传输的核心机制与实践
  • 文档输入内容,算术运算,字符串的索引
  • 如何构建高质量的分布式系统监控体系
  • xml网站地图生成器别人 网站 粘贴 html 推广
  • redis 配置学习笔记
  • AI 指数走势透视:红利期或将延续?
  • 光影魔术手4.6.1.670|win中文|图像修改编辑器|安装教程
  • 利用小波分解进行模糊检测pywt.dwt2
  • 试用平台网站建设上海建设安全生产协会网站
  • 用《大前端++》项目特征解析雷总小米汽车的内忧远虑
  • 网络爬虫也叫做网络机器人
  • 台州网站制作 外贸wordpress数据清除
  • 视频网站中滑动列表怎么做的网页设计与制作实训报告的综合优化
  • 昌平手机网站建设北京传媒公司排行榜
  • 专注集团网站建设做网站网址
  • Flink Checkpoint 通用调优方案三种画像 + 配置模板 + 容量估算 + 巡检脚本 + 告警阈值
  • 做一个企业的网站怎么做网站开发合同怎么写
  • 大望路网站建设影楼微网站建设
  • 【滑动窗口专题】第二讲:无重复字符的最长子串
  • 如何简单的并且又能大幅度降低任务队列的锁粒度、提高吞吐量?
  • GPTL(General Purpose Timing Library)使用教程
  • 网站制作先做数据库还是前台重庆新闻论坛新闻评论
  • 【终端使用MySQL】MySQL 数据库核心操作全解析:从入门到性能优化