Go language --- object-oriented programming

The language design of Go language is very concise, because Go language does not have object-oriented concepts, so there are no object-oriented concepts in Go language, such as (encapsulation, inheritance, polymorphism, virtual functions, constructors, this pointers, etc.)
Although there is no encapsulation, inheritance, and polymorphism in the Go language, the corresponding functions can also be implemented.
Encapsulation: Implementation through methods
Inheritance: Implementation through anonymous fields
Polymorphism: Implementation through interfaces

method

A method is also a type of function. In essence, a method is a function associated with a special type.
Insert picture description here

type Int int
//a为接收者,Int为接收者的名字
func (a Int)add(b Int)Int{
    
    
	return a+b
}

func main(){
    
    
	var a Int = 3
	res := a.add(2)
	fmt.Println(res)
}

At the same time, you can assign values ​​to structure variables through methods.

type Person struct{
    
    
	Name string 
	Age int 
}

func (P Person)PrintInfo(){
    
    
	//...一些基本操作
}

We can assign values ​​to structures through functions.

type Person struct{
    
    
	Name string 
	Age int 
}

func (P *Person)SetInfo(s string,i int){
    
    
	P.Name = s
	P.Age = i
}
func main(){
    
    
	var p Person
	(&p).SetInfo("bai",20)
	fmt.Println(p.Name,p.Age)
}

The method is similar to an additional parameter, we can regard the receiving method as a parameter.
Insert picture description here
note

  • The custom type cannot be a pointer type
    -
  • The recipient type is different, and there is no error with the same name.
    Insert picture description here

Anonymous field

type person struct{
    
    
	Name string 
	Age int
}

type Student struct{
    
    
	person  //只有类型,没有具体名称
	className string
}

The above Student class contains the anonymous field of person, which is equivalent to inheriting from person.

Initialization method:

  • Sequential initialization
    s1 := Student{person{"bai",20},"xiaan"}
  • Specify member initialization
    s2 := Student{className:"xiaan"}
    s3 := Student{person:person{Name:"bai"},className:"xiaan"}

Structure pointer type anonymous field

type person struct{
    
    
	Name string 
	Age int
}

type Student struct{
    
    
	*person  //指针类型
	className string
}

Initial operation

  • Direct assignment
    s1 := Student{&person{"bai",20},"xiaan"}

  • Initialize first, assign value

var s1 Student
s1.person = new (person)
s1.Name = "bai"
s1.Age = 20
s1.className = "xiaan"

interface

The Go language uses interfaces to achieve the effect of polymorphism.

Definition of the interface:

type 接口名 interface{
    
    
	接口声明
}

For example: the definition of the HelloWorld interface

type Hello interface{
    
    
	HelloWorld() //只声明,不实现
}

Implementation of the HelloWorld interface

//通过自定义类型实现接口
type Student struct{
    
    
	name string 
	age int
}

func (s *Student)HelloWorld(){
    
    
	fmt.Printf("Student [%s %d]",s.name,s.age)
}

//默认类型实现接口
type Int int 
func (i *Int)HelloWorld(){
    
    
	fmt.Printf("I")
}

Calling the HelloWorld interface

//自定义类型调用接口
s := &Student{
    
    "xiao",20}
var h Hello
h = s
h.HelloWorld()

//默认类型调用接口
var i Int
h = i
h.HelloWorld()
The realization of common polymorphism
  • Define a function, the parameter type of the function is the interface type
//多态:只有一个函数,实现不同功能
func Test(h Hello){
    
    
	h.HelloWorld()
}

func main(){
    
    
	s := &Student{
    
    "xiao",20}
	Test(s)
]

Interface inheritance

type Humaner interface{
    
    
	Hello() //定义一个Hello接口	
}

type Person interface{
    
    
	Humaner //匿名字段,接口的继承
	HelloWoeld(str string)
}

//自定义数据类型
type Student struct{
    
    
	name string 
	age int 
}
//Hello接口的实现
func (s *Student)Hello(){
    
    
	fmt.Printf("name= %s age%d=",s.name,s.age)
}	

//HelloWorld接口的实现
func (s *Student)HelloWorld(str string){
    
    
	fmt.Printf("str name=%s age%d ",s.name,s.age)
}

//接口的调用
func main(){
    
    
	var p Person
	s := &Student{
    
    "bai",20} //实例化一个结构体
	p = s
	p.Hello() //调用继承的Hello接口
	p.HelloWoeld() //调用自身的HelloWorld接口
}

Interface conversion

Insert picture description here
Super can be converted to a subset, and a subset cannot be converted to a super set.

var p Person //超集
var h Humaner //子集
p = h //错误
h = p //正确
Empty interface

The empty interface does not contain any methods, so all types can implement the empty interface.
Empty interface can store any type of value

var v1 interface{
    
    } = 1 //将int类型赋值给空接口
var v2 interface{
    
    } = "abc" //将string类型赋值给空接口

An empty interface can also be used as a parameter of a function, which means that the function can receive any type. The common fmt function is an empty interface function

Type query

Since the empty interface function can receive any type of parameter, type query is required. Common type of query method is type assertion

//定义一个空接口切片
i := make([]interface{
    
    },3)
i[0] = 1
i[1] = "abc“
i[2] = Student{"bai",20}


//if -- else类型断言
//for循环:index为下标,data为具体的值分别是i[0],i[1],i[2]
for index,data := range i{
    
    
	//value为具体的值,ok为true时表示判断正确,ok为false表示判断错误
	if value,ok := data.(int) == true{
    
    
	}
}


//switch 类型断言
for index,data := range i{
    
    
	switch value:= data.(type){
    
    
		case  int:
			...
		case string:
			...
		case struct:
			...
	}
}

Guess you like

Origin blog.csdn.net/qq_42708024/article/details/107152795