zoukankan      html  css  js  c++  java
  • Google资深工程师深度讲解Go语言面向对象(四)

    一.结构体和方法

    面向对象

    • go语言仅支持封装,不支持继承和多态
    • go语言没有class,只有struct结构体

    结构的创建

    • 不论地址还是结构本身,一律使用.(点)来访问成员
    • 使用自定义工厂函数
    • 注意返回了局部变量的地址
    • go语言没有构造函数

    结构创建在堆上?栈上:   不需要知道 垃圾回收

    使用指针作为方法接受者

    • 只有使用指针才可以改变结构内容
    • nil指针也可以调用方法 

    值接受者vs指针接受者

    • 要改变内容必须使用指针接受者
    • 结构过大也考虑使用指针接受者
    • 一致性:如有指针接受者,做好都是指针接受者(建议)
    • 值接受者是go语言特有,指针接受者其他语言都有
    • 值/指针接受者均可接受值/指针

    package main
    
    import "fmt"
    
    type treeNode struct {
    	value       int
    	left, right *treeNode //指针
    }
    
    //自定义工厂函数
    func createNode(value int) *treeNode {
    	return &treeNode{value: value} //返回局部变量的地址
    }
    
    /**
    go语言所有参数传值
    为结构定义方法
    显示定义和命名方法接受者
     */
    //值接受
    func (node treeNode) print() {
    	fmt.Print(node.value," ")
    }
    
    //指针接受
    func (node *treeNode) setValue(value int) {
    	if node==nil {
    		fmt.Println("setting value to nil node ")
    		return
    	}
    	node.value = value
    }
    
    //子树遍历
    //中式遍历:先左再中再右
    
    func (node *treeNode)traverse()  {
    	if node==nil {
    		return
    	}
    	node.left.traverse()
    	node.print()
    	node.right.traverse()
    
    }
    
    
    func main() {
    	var root treeNode
    
    	root = treeNode{value: 3}
    	root.left = &treeNode{}             //左子树 取地址
    	root.right = &treeNode{5, nil, nil} //右子树
    	root.right.left = new(treeNode)
    	//fmt.Println(root)
    	root.left.right = createNode(2)
    
    	root.right.left.setValue(4)
    	root.right.left.print()
    	fmt.Println()
    
    	//root.print()
    	//root.setValue(100)
    	//
    	//var pRoot *treeNode
    	//pRoot.setValue(200)
    	//pRoot=&root
    	//pRoot.setValue(300)
    	//pRoot.print()
    
    	fmt.Println()
    
    	root.traverse()//0 2 3 4 5
    	//pRoot:=&root
    	//pRoot.print()
    	//pRoot.setValue(200)
    	//pRoot.print()
    
    	/*nodes:=[]treeNode{
    		{value:3},
    		{},
    		{6,nil,&root},
    	}
    	fmt.Println(nodes)*/
    
    }
    

    二,包和封装

    针对包来说

    • 名字一般使用CamelCase
    • 首字母大写:public 公有
    • 首字母小写:private 私有

    包:

    • 每个目录一个包
    • main包包含可执行入口(main 函数)
    • 为结构定义的方法必须放在同一个包内
    • 可以是不同的文件
    package main
    
    import "fmt"
    
    func main() {
    	//one:=0
    	one,two:=1,2
    	one,two=two,one //交换变量值
    	fmt.Println(one,two)
    }
    

    如何扩充系统类型或者别人的类型

    • 定义别名
    • 使用组合

    queue.go

    package queue
    
    type Queue []int
    
    func (q *Queue) Push(v int) {
    	*q = append(*q, v)
    }
    
    func (q *Queue) Pop() int {
    	head := (*q)[0]
    	*q = (*q)[1:]
    	return head
    }
    
    func (q *Queue) IsEmpty() bool {
    	return len(*q) == 0
    }
    

    mian.go 文件

    package main
    
    import (
    	"../../queue"
    	"fmt"
    )
    
    func main() {
    	q := queue.Queue{1} //队列先进先出
    
    	q.Push(2)
    	q.Push(3)
    	fmt.Println(q.Pop())     //1
    	fmt.Println(q.Pop())     //2
    	fmt.Println(q.IsEmpty()) //false
    	fmt.Println(q.Pop())     //3
    	fmt.Println(q.IsEmpty()) //true
    
    }
    

    四.gopath环境变量

    • 默认在~/go(unix,linux),%userprofile%\go(windows):
    • 例如GOROOT=/usr/local/go #gosetup   GOPATH=/Users/liutao/go #gosetup
    • 官方推荐:所有项目和第三方库都放在同一个gopath下
    • 也可以将每个项目放在不同的gopath

    赞赏码

    非学,无以致疑;非问,无以广识

  • 相关阅读:
    ip地址查询python3小工具_V0.0.1
    安恒 堡垒机 明御®运维审计与风险控制系统 部署中遇到的问题
    zping ping包工具20180605.exe测试版
    zping ping包工具20180524.exe测试版
    XSS练习平台-XSS Challenges
    温暖而又美妙的语言
    无法连接仓库:Error performing git command: /usr/local/src/git-2.9.5 ls-remote -h https://gitee.com/skynetInfo/auto-webui HEAD
    jenkins安装方式及启动配置
    Can't open file '/opt/svn/path/db/txn-current-lock': Permission denied
    centos部署maven私服
  • 原文地址:https://www.cnblogs.com/lxwphp/p/15452748.html
Copyright © 2011-2022 走看看