Detailed structure of golang / member method implementation / inheritance Portfolio / details of

Structure

Go object-oriented languages ​​is realized by the structure, the structure is a value type, can be used to create new.

definition

Structure is defined as follows:

type identifier struct{
    filed1 type
    filed2 type
}

filed is the name of the field structure, type is the type of the field, if they did not know names, you can use _ to placeholder. Any type field may even structure itself, or may be function or interface.

initialization

Similarly initialization c ++, can be assigned directly after statements can also be assigned a new keyword, the operation follows

package main

type ListNode struct {
	Val  int
	Next *ListNode
}
func main() {
    //直接声明
	var node ListNode
	node.Val =1
    //new出来
    node2:=new(ListNode)
	node2.Val=2
}

Member method

Definition of structures in the body, to achieve an external

When you define, you can find, type may be func, so it provides a channel for members of the realization method structure.

package main

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
	toString func()
}
func doSth() {
	fmt.Println("do something")
}
func main() {
	var node ListNode
	node.Val =1
	node.toString=doSth
	node.toString()
}

Although the finish was found that quite outrageous, to say that a member of the method, but can not access the node properties must pass before they can go.

Structure in vitro method for true members

package main

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}
//值传递下的成员方法,无法改变node真正的属性
func (node ListNode)setVal(val int) {
	node.Val=val
}
//引用传递下的成员方法,可以改变node的真正属性
func (node *ListNode)setVal2(val int) {
	node.Val=val
}
func main() {
	var node ListNode
	node.Val =1
	node.setVal(2)
	fmt.Println(node.Val)
	(&node).setVal2(2)
	fmt.Print(node.Val)
}

Auxiliary string field

Auxiliary string in the string field is the same line, can only be obtained with a field declaration by reflection

package main

import (
	"fmt"
	"reflect"
)

type Person struct {
	Name string "Name"
	Age  int    "Age"
	Sex  int    "Sex"
}

func main() {
	person := Person{}
	person.Name = "Tim"
	person.Age  = 10
	person.Sex  = 1

	for i := 0; i < 3; i++ {
		refTag(person, i)
	}
}

func refTag(p Person, i int) {
	pType := reflect.TypeOf(p)
	iFiled := pType.Field(i)
	fmt.Println(iFiled.Tag)
}

reflect.TypeOf can obtain the correct type of a variable, if a structure is variable, it may be the index field by Filed structure, can then use the Tag property.

Anonymous field

go structure also supports a special field, anonymous field. These fields are not explicitly name only field types, field types at this time is the name of the field, the field also can be an anonymous structure. So the same kind of anonymous field can only have one.

type Test1 struct {
    t1 int
    t2 int
}

type Test struct {
    a int
    b string
    bool
    Test1
}

func main() {
    test := new(Test)
    test.a = 1
    test.b = "2"
    test.bool = true
    test.t1 = 2
    test.t2 = 3
    
    fmt.Println(test)
}

inherit

Embedded in a structure to another structure, known as a combination of
the difference, and combinations anonymous
if a nested struct another anonymous structure, this structure can be directly structured body anonymous access method, thereby achieving inheritance
If a nested struct another famous structure [], then the model is called a combined
method if a more anonymous struct nested structure, then the structure can access multiple anonymous structures directly in order to achieve multiple inheritance

package main

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}
//组合
type TreeNode struct {
	lnode ListNode
}
//继承
type GraphNode struct {
	ListNode
} 
//值传递下的成员方法,无法改变node真正的属性
func (node ListNode)setVal(val int) {
	node.Val=val
}
//引用传递下的成员方法,可以改变node的真正属性
func (node *ListNode)setVal2(val int) {
	node.Val=val
}
func main() {
	var treeNode TreeNode
	treeNode.lnode.setVal2(1)
	var graphNode GraphNode
	graphNode.setVal2(2)
}
Published 16 original articles · won praise 3 · Views 1349

Guess you like

Origin blog.csdn.net/weixin_40631132/article/details/105208402