Golang object-oriented programming-structure

Golang object-oriented programming instructions

  • Golang also supports object-oriented programming (OOP), but it is different from traditional object-oriented programming and is not a pure object-oriented language. Therefore, it is more accurate to say that Golang supports object-oriented programming features.
  • Golang has no classes. Golang's struct has the same address as other programming language classes. It can be understood that Golang implements OOP features based on struct.
  • Golang object-oriented programming is very concise, removing the inheritance of traditional OOP language, method overloading, constructor and destructor, hidden this pointer, etc.
  • Golang still has the characteristics of inheritance, encapsulation and polymorphism of object-oriented programming. The way of knowledge realization is different from other OOP languages, such as inheritance. Golang does not have the extends keyword, and inheritance is achieved through anonymous fields.
  • Golang Object-Oriented (OOP) is very elegant. OOP itself is part of the language type system. It is associated with interfaces, low coupling, and very flexible. Interface-oriented programming in Golang is a very important feature.

Structures and structure variables (examples)

1. Schematic diagram of the relationship

Insert picture description here
Description:

  • Extract the characteristics of a class of things (such as cats) to form a new data type, which is a structure.
  • Through this structure, we can create multiple variables (instances/objects).
  • Things can be cats, Person, Fish, etc. or a tool.

2. The difference and connection between the two

package main

import(
	"fmt"
)

type Person struct{
    
    
	Name string
	Age int
}

func main(){
    
    
	//创建一个person变量
	var person Person
	person.Name = "Casey"
	person.Age = 22
	fmt.Println("person =",person)

}

Operation result:
Insert picture description here
the difference and connection between the two:

  • The structure is a self-defined data type that represents a class of things.
  • Structure variables (instances) are concrete, actual, and represent a concrete variable.

The layout of structure variables in memory

package main

import(
	"fmt"
)

type Person struct{
    
    
	Name string
	Age int
}

func main(){
    
    
	//创建一个person变量
	var person Person
	person.Name = "Casey"
	person.Age = 22
	fmt.Printf("person = %v\n",person)
	fmt.Printf("person.Name的地址为:%v\n",&person.Name)
	fmt.Printf("person.Age的地址为:%v\n",&person.Age)

}

operation result:
Insert picture description here

Insert picture description here

How to declare a structure

基本语法:
type 结构体名 struct{
    
    
   field1 type
   field2 type
}
例:
type Student struct{
    
    
  Name string
  Age int
  Score float32
}

Field/attribute

1. Basic introduction

In terms of concept or name: structure field = attribute = field. A component of the field structure, generally basic data, arrays, or reference types.

2. Matters needing attention

  • The field declaration syntax is the same as that of variables.
  • The type of the field can be: basic type, array or reference type
  • After creating a structure variable, if no value is assigned to the field, a zero value is given to the game (default value)
  • The fields of different structure variables are independent and do not affect each other. The change of one structure variable field does not affect the other. The structure is a value type
package main

import(
	"fmt"
)

type Person struct{
    
    
	Name string
	Age int
}

func main(){
    
    
	//创建一个person变量
	var person Person
	person.Name = "Casey"
	person.Age = 22

	var person1 = person
	person1.Name = "Jerry"
	fmt.Printf("person = %v\n",person)
	fmt.Printf("person1 = %v\n",person1)
	

}

operation result:
Insert picture description here

Structure initialization

package main

import(
	"fmt"
)

type Person struct{
    
    
	Name string
	Age int
}

func main(){
    
    
	//结构体初始化
	//方式1
	var person Person
	person.Name = "Casey"
	person.Age = 22
	fmt.Printf("person = %v\n",person)

	//方式2,使用键值对初始化
	var person1 = Person{
    
    
		Name : "Tom",
		Age : 30,
	}
	fmt.Printf("person1 = %v\n",person1)
	
	//方式3
	var person2 *Person = new(Person)
	(*person2).Name = "Jack"
	//说明:go的设计者为了程序员使用方便,底层会对person.Age = 22
	//进行处理,会给person2加上取值运算(*person2).Age = 22
	person2.Age = 23
	fmt.Println("person2 =",*person2)

	//方式4
	var person3 *Person = &Person{
    
    
		Name : "Danny",
	}
	person3.Age = 35
	fmt.Println("person3 =",*person3)

}

Operation result:
Insert picture description here
Description:

  • The third and fourth methods return structure pointers.
  • The standard way for structure pointers to access fields should be: (*structure name).field name
  • Go has been simplified, and also supports structure pointers and field names . The bottom layer of the go compiler has been optimized. The structure pointer. Field name will be converted to (* Structure name). Field name.

Precautions for the use of structure

  • All fields of the structure are continuous in memory.
  • The structure is a type defined separately by the user, and the conversion with other types must have exactly the same fields (name, number and type).
package main

import(
	"fmt"
)

type A struct{
    
    
	Num int
}

type B struct{
    
    
	Num int
}

func main(){
    
    
	var a A
	var b B 
	a = A(b)
	fmt.Println(a,b)
}

operation result:
Insert picture description here

  • The structure is redefined by type (equivalent to aliasing). Golang considers it to be a new data type, but it can be forced to each other.
    Insert picture description here
    Insert picture description here

Link to the blogger's homepage: https://blog.csdn.net/weixin_44736475
Originality is not easy, I hope you can support
it. If the article is helpful to you, remember to click three links! ❤️❤️❤️

Guess you like

Origin blog.csdn.net/weixin_44736475/article/details/114162407