GoLangStudy:day6
GO day6
golang语法基础(面向对象继承与接口)
1.面向对象:继承
package main
import "fmt"
// 父类Huamn
type Human struct {
name string
sex string
}
// 在子类结构体中写一下父类名,就代表有继承关系
// 与C++等语言不同,go中继承没有公有制一类说法
type SuperHuman struct {
Human//声明继承Human类
level int
}
func (this *Human) Eat() {
fmt.Println("Human.Eat().....")
}
// 重写父类方法
func (this *SuperHuman) Eat() {
fmt.Println("SuperHuman.Eat().....")
}
func (this *Human) Walk() {
fmt.Println("Human.Walk().....")
}
func (this *Human) Print() {
fmt.Println("name =", this.name)
fmt.Println("sex =", this.sex)
}
func main() {
//定义父类
h := Human{"flora", "male"}
//定义子类方法一
sh := SuperHuman{Human{"shird", "male"}, 300}
//定义子类方法二
var sh2 SuperHuman
sh2.name="test"
sh2.sex="female"
sh2.level=1
//父类子类方法调用
fmt.Println("--------Father-------")
h.Walk()
h.Eat()
fmt.Println("---------son---------")
sh.Walk()//父类方法
sh.Eat()//子类方法
}
输出结果:
--------Father-------
Human.Walk().....
Human.Eat().....
---------son---------
Human.Walk().....
SuperHuman.Eat().....
2.面向对象多态的实现与要素
在golang中,接口的实现不需要在子类中写明,而是隐式实现,只要该类实现了接口中所有同名方法,则默认此类继承了接口
package main
import "fmt"
// 接口的定义
type Animal interface {
GetType() string
Sleep()
GetColor() string
}
// 子类一
type Cat struct {
Color string
}
// 实现接口方法
func (c *Cat) GetType() string {
return "Cat"
}
func (c *Cat) Sleep() {
fmt.Println("Cat is sleep")
}
func (c *Cat) GetColor() string {
return c.Color
}
// 子类二
type Dog struct {
Color string
}
// 实现接口方法
func (d *Dog) GetType() string {
return "Dog"
}
func (d *Dog) Sleep() {
fmt.Println("Dog is sleep")
}
func (d *Dog) GetColor() string {
return d.Color
}
//想让继承了animal接口的所有类都能使用同一个方法,可以让接口作为方法传参的数据类型
func ShowAnimal(animal Animal) {
fmt.Println("type:", animal.GetType())
fmt.Println("color:", animal.GetColor())
}
func main() {
var animal Animal
//由于接口本质是指针,所以这里通过取地址赋值
animal = &Cat{"green"}
animal.Sleep()
animal = &Dog{"yellow"}
animal.Sleep()
cat := Cat{"green"}
dog := Dog{"yellow"}
//取地址传参
ShowAnimal(&cat)
ShowAnimal(&dog)
}
输出结果:
Cat is sleep
Dog is sleep
type: Cat
color: green
type: Dog
color: yellow
示意图

3.空接口与类型断言机制
知识点1:空接口的使用
概念:

示例代码:
package main
import "fmt"
// interface{}作为一个空接口,可以接受任何类型的传参
func Anything(arg interface{}) {
fmt.Println("Anything is called.....")
fmt.Println(arg)
}
type Book struct {
title string
}
func main() {
book := Book{"GoLang"}
Anything(book)
Anything(18)
Anything("everything")
}
输出结果:
Anything is called.....
{GoLang}
Anything is called.....
18
Anything is called.....
everything
那如果我们在实际应用中,遇到了不同数据类型需要不同实现的时候,使用空接口又该如何判断传参的数据类型呢?
知识点2:断言机制
package main
import "fmt"
// interface{}作为一个空接口,可以接受任何类型的传参
func Anything(arg interface{}) {
fmt.Println("Anything is called.....")
//断言机制:判断传入参数的数据类型
//第一个返回值value:如果传入参数是指定类型,则value被赋值为传入参数的值,否则value值为指定类型的默认值
//第二个返回值ok:这是一个布尔标记,用于判断传参类型是否为指定类型
value, ok := arg.(int)
if !ok {
fmt.Println("argument is not an int")
} else {
fmt.Println("arg is a int,value=", value)
fmt.Printf("type of arg is %T\n", value)
}
}
type Book struct {
title string
}
func main() {
book := Book{"GoLang"}
Anything(book)
fmt.Println("--------------")
Anything(18)
fmt.Println("--------------")
Anything("everything")
}
输出结果:
Anything is called.....
argument is not an int
--------------
Anything is called.....
arg is a int,value= 18
type of arg is int
--------------
Anything is called.....
argument is not an int
浙公网安备 33010602011771号