GO_函数_面向对象编程_接口

封装数据和行为

结构体定义

type Employee struct {
    
    
Id string
Name string
Age int
}

实例创建及初始化

e := Employee{
    
    "0", "Bob", 20}
e1 := Employee{
    
    Name: "Mike", Age: 30}
e2 := new(Employee) //注意这里返回的引⽤用/指针,相当于 e := &Employee{}
e2.Id =2" //与其他主要编程语言的差异:通过实例例的指针访问成员不需要使用->
e2.Age = 22
e2.Name = “Rose"

行为(方法)定义

与其他主要编程语言的差异
在这里插入图片描述
Demo:

package objectStruct

import (
	"fmt"
	"testing"
	"unsafe"
)

type Employee struct {
    
    
	Id string
	Name string
	Age int
}

func (e *Employee) String() string {
    
    
	fmt.Printf("Adress is %x", unsafe.Pointer(&e.Name))
	return fmt.Sprintf("ID:%s/Name:%s/Age:%d", e.Id, e.Name, e.Age)
}

//func (e Employee) String() string {
    
    
//	fmt.Printf("Address is %x\n", unsafe.Pointer(&e.Name))
//	return fmt.Sprintf("ID:%s-Name:%s-Age:%d", e.Id, e.Name, e.Age)
//}

func TestCreateEmployeeObject(t *testing.T) {
    
    
	e := Employee{
    
    "11", "liSi", 26}
	e1 := Employee{
    
    Name: "wangWu", Age: 21}
	e2:= new(Employee) // 返回指针
	e2.Id = "3"
	e2.Age = 25
	e2.Name = "yuio"
	t.Log(e)
	t.Log(e1)
	t.Log(e1.Id)
	t.Log(e2)
	t.Logf("e is #{e}")
	t.Logf("e2 is #{e2}")
	/*func (e *Employee) String() string {
		fmt.Printf("Adress is %x", unsafe.Pointer(&e.Name))
		return fmt.Sprintf("ID:%s/Name:%s/Age:%d", e.Id, e.Name, e.Age)
	}*/
	/** result
	=== RUN   TestCreateEmployeeObject
	    struct_test.go:32: {11 liSi 26}
	    struct_test.go:33: { wangWu 21}
	    struct_test.go:34:
	Adress is c00012a400    struct_test.go:35: ID:3/Name:yuio/Age:25
	    struct_test.go:36: e is #{e}
	    struct_test.go:37: e2 is #{e2}
	--- PASS: TestCreateEmployeeObject (0.00s)
	PASS
	PASS
	 */

	/*func (e Employee) String() string {
		fmt.Printf("Address is %x\n", unsafe.Pointer(&e.Name))
		return fmt.Sprintf("ID:%s-Name:%s-Age:%d", e.Id, e.Name, e.Age)
	}*/
	/** result
	=== RUN   TestCreateEmployeeObject
	Address is c0000787f0
	    struct_test.go:32: ID:11-Name:liSi-Age:26
	Address is c0000788b0
	    struct_test.go:33: ID:-Name:wangWu-Age:21
	    struct_test.go:34:
	Address is c0000788e0
	    struct_test.go:35: ID:3-Name:yuio-Age:25
	    struct_test.go:36: e is #{e}
	    struct_test.go:37: e2 is #{e2}
	--- PASS: TestCreateEmployeeObject (0.00s)
	PASS
	 */
}

func TestStructOperations(t *testing.T) {
    
    
	e := Employee{
    
    "0", "yuio", 26}
	fmt.Printf("Address is %x\n", unsafe.Pointer(&e.Name))
	t.Log(e.String())
	/**
	=== RUN   TestStructOperations
	Address is c00012a400
	Adress is c00012a400    struct_test.go:78: ID:0/Name:yuio/Age:26
	--- PASS: TestStructOperations (0.00s)
	PASS
	 */
}

接口

在这里插入图片描述

Go 接口

与其他主要编程语⾔言的差异

  1. 接口为非入侵性,实现不依赖于借口定义
  2. 所以接口的定义可以包含在接口使用者包内

接口变量

在这里插入图片描述

package _interface

import "testing"

// 定义一个interface
type Programmer interface {
    
    
	WriteHelloWorld() string
}

// 定义一个实体类
type GoProgrammer struct {
    
    
}

// 实现接口方法impl
func (g *GoProgrammer) WriteHelloWorld() string {
    
    
	return "fmt.Println(\"Hello World\")"
}
func TestClient(t *testing.T) {
    
    
	var p Programmer
	p = new(GoProgrammer)
	t.Log(p.WriteHelloWorld())
	/**
	=== RUN   TestClient
	    interface_test.go:21: fmt.Println("Hello World")
	--- PASS: TestClient (0.00s)
	PASS
	 */
}

PS:学习笔记,侵删!

猜你喜欢

转载自blog.csdn.net/qq_31686241/article/details/126800566