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
多态
基本要素:
- 有一个父类(接口)
- 有子类(实现了父类的全部接口方法)
- 父类类型的变量(指针)指向(引用)子类的具体数据变量
//本质是一个指针
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