Go object-oriented thought: encapsulation, inheritance and polymorphism

Time:2021-10-27

How do go implement encapsulation, inheritance, and polymorphism

  • Encapsulation: go realizes the public and private of class methods and attributes through the case of the initial letter
  • Inheritance: use the embedded method to inherit the structurestructCombine
  • Polymorphism: Usinginterfacerealization

Encapsulation: go simulates classes through the method of structs

package objects

//Simulate classes by means of structs

type User struct {
    Id int8
    Name string
    Age int8 // indicates private and public by initial

}
//Determine whether to access externally according to the case of the first letter of the method
func (user *User) GetUser() string {

    return user.Name + "pl";
}

We write a class that inherits user information. Userinfo inherits the user structure through u and implements class inheritance through nesting

type UserInfo struct {
    U user // inherits the Uset structure
    Record string // experience
    State float32 // height
    Sex int8 // gender
    Introduction string // Introduction
    Avatar string // Avatar
}

func (userinfo *UserInfo) GetUserInfo() *UserInfo  {
    user := new(UserInfo)
    user.Avatar ="xxx"
    return user
}

Polymorphic implementation complete code

package objects

//Simulate classes by means of structs
type User struct {
    Id int8
    Name string
    Age int8 // indicates private and public by initial

}
//Interface class
type UserInterface interface {
    GetUserInfo() *UserInfo
}
//Simulate classes by means of structs
type User struct {
    Id int8
    Name string
    Age int8 // indicates private and public by initial

}
//Determine whether to access externally according to the case of the first letter of the method
func (user *User) GetUser() string {

    return user.Name + "pl";
}

type UserInfo struct {
    U user // inherits the Uset structure
    Record string // experience
    State float32 // height
    Sex int8 // gender
    Introduction string // Introduction
    Avatar string // Avatar
}

func (userinfo *UserInfo) GetUserInfo() *UserInfo  {
    user := new(UserInfo)
    user.Avatar ="xxx"
    return user
}

As shown in the above code:UserInfoStructure inheritsUserInterfaceInterface and ImplementationGetUserInfo()Method of

Go object-oriented thought: encapsulation, inheritance and polymorphism

Let’s print this methodGetUserInfoto writemain.goEntry file

package main

import (
    "fmt"
    //Namespace introduction similar to PHP
    "object/objects"
)

func main(){
    //Instantiate this structure
    user :=new(objects.UserInfo)
    //Reference the method corresponding to this structure and print it
    fmt.Println(user.GetUserInfo())
}

implementgo run main.go

Go object-oriented thought: encapsulation, inheritance and polymorphism
As shown in the figure, the execution is successful (note that code 1 fragment and code 2 fragment are the same file in the object directory)

This work adoptsCC agreement, reprint must indicate the author and the link to this article

If you don’t become a Daniel, don’t change your signature