Go语言也提供了继承

type person struct{
	name string
	age int
}

type student struct{
	person                     //通过struct匿名字段来继承
    class_id int
}

一个简单的继承关系

package main

type Person struct{
    name string
    age int
}

func(person Person) getNameAndAge() (string , int){
    return person.name,person.age
}

type Student struct{
    Person
    speciality string
}

func(student Student) getSpeciality() string{
    return student.speciality
}

func main(){
    student := new(Student)
    student.name = "zhangsan"
    student.age = 26
    student.speciality = "math"
    name , age := student.getNameAndAge()
    
    speciality := student.getSpeciality()
    
    fmt.Println(name,age) 
}

输出结果:

> Output:
zhangsan 26 math
> Elapsed: 8.047s
> Result: Success

Go的接口

在Go语言中,一个类只需要实现了接口要求的所有函数,我们就说这个类实现了该接口。

接口赋值(非侵入式接口)

把实例对象赋值给接口

package main
import "fmt"

type Animal interface{          //接口
    Fly()
    Run()
}

type Bird struct{
    
}

func (bird Bird)  Fly(){
    fmt.Println("bird is flying!!!")
}

func (bird Bird) Run(){
    fmt.Println("bird is runing!!!")
}

func main(){
    var animal Animal
    bird := new(Bird)
    
    animal = bird
    animal.Fly()
    animal.Run()
}

可以直接调用animal 接口

输出结果:

bird is flying!!!
bird is runing!!!
> Elapsed: 7.075s
> Result: Success

将一个接口赋值给另一个接口

把方法多的,大的接口赋值给包含方法少的接口

赋值等号前面是少的

package main
import "fmt"

type Animal interface{          //接口
    Fly()
    Run()
}

type Animal2 interface{          //接口
    Fly()
}

type Bird struct{
    
}

func (bird Bird)  Fly(){
    fmt.Println("bird is flying!!!")
}

func (bird Bird) Run(){
    fmt.Println("bird is runing!!!")
}

func main(){
    var animal Animal
    var animal2 Animal2
    
    bird := new(Bird)
    animal = bird
    animal2 = animal
    animal2.Fly()
    
}

输出结果:

> Output:
bird is flying!!!
> Elapsed: 1.265s
> Result: Success

类型查询

Any类型

变量可以指向任何类型,为了让第二个参数可以打印任何值。

Go语言中任何对象实例都满足空接口interface{}

var v1 interface{} = 1
var v2 interface{} = "abc"
var v3 interface{} = 2.345
var v4 interface{} = make(map[..]...)

func Printf(fmt string,args...interface{})

例如

package main
import "fmt"

type Animal interface{          //接口
    Fly()
    Run()
}

type Animal2 interface{          //接口
    Fly()
}

type Bird struct{
    
}

func (bird Bird)  Fly(){
    fmt.Println("bird is flying!!!")
}

func (bird Bird) Run(){
    fmt.Println("bird is runing!!!")
}

func main(){
    var v1 interface{}        //接受任何类型的空接口
    v1 = "zhangsan" //v1 = 123
    fmt.Print(v1)
}

类型查询

标准格式

if _,ok := a.(*Integer);ok
{
    ...
}

v1.(type)

例如

func main(){
    var v1 interface{}
    v1 = 6.78
    
    if v, ok := v1.(float64); ok{
        fmt.Println(v, ok)
    } else {
        fmt.Println(v , ok)
    }
}


> Output:
command-line-arguments
6.78 true
> Elapsed: 7.801s
> Result: Success

或者也可以这样用

func main(){
    var v1 interface{}
    v1 = 6.78
    
    switch v1.(type){
        case int:
        case float32:
        case float64:
        	fmt.Println("这是float64")
        case string:
        	fmt.Println("这是string")
    }
}

> Output:
command-line-arguments
这是float64
> Elapsed: 6.094s
> Result: Success