加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang学习笔记[2] 面向对象OOP

发布时间:2020-12-16 18:47:47 所属栏目:大数据 来源:网络整理
导读:// one project main.gopackage mainimport "fmt"//为int类型起个别名Integertype Integer intfunc (a Integer) More(b Integer) Integer {return a - b}func Modify_arr(arr [3]int) {//数组是值类型,传进函数里面的是拷贝的那份arr[0] = 500}func Modify_
// one project main.go
package main

import "fmt"

//为int类型起个别名Integer
type Integer int

func (a Integer) More(b Integer) Integer {
	return a - b
}

func Modify_arr(arr [3]int) {
	//数组是值类型,传进函数里面的是拷贝的那份
	arr[0] = 500
}
func Modify_slice(arr []int) {
	//切片是引用类型,传进函数里面可以直接修改
	arr[0] = 500
}

//定义Rect结构体
type Rect struct {
	x,y          float64
	width,height float64
}

//为Rect结构体增加计算面积的方法
func (r *Rect) Area() float64 {
	return r.width * r.height
}

//创建初始化Rect的函数
func NewRect(x,y,width,height float64) *Rect {
	return &Rect{x,height}
}

//声明Base
type Base struct {
	Name string
}

//为Base添加funca和funcb
func (base *Base) funca() {
	fmt.Println("funca")
}
func (base *Base) funcb() {
	fmt.Println("funcb")
}

//继承Base的Children
type Children struct {
	Base
	Son string
}

//为children添加funcc方法
func (children *Children) funcc() {
	fmt.Println("funcc")
}

//定义Flie结构体并实现四个方法
type File struct{}

func (f *File) Read(buf []byte) (n int,err error) {
	return n,err
}
func (f *File) Write(buf []byte) (n int,err
}
func (f *File) Seek(off int64,whence int) (pos int64,err error) {
	return pos,err
}
func (f *File) Close() error {
	return nil
}

//接口
type IFile interface {
	Read(buf []byte) (n int,err error)
	Write(buf []byte) (n int,err error)
	Seek(off int64,err error)
	Close() error
}
type IReader interface {
	Read(buf []byte) (n int,err error)
}
type IWriter interface {
	Write(buf []byte) (n int,err error)
}
type IClose interface {
	Close() error
}

//接口赋值
func (a Integer) Less(b Integer) bool {
	return a < b
}
func (a *Integer) Add(b Integer) {
	*a += b
}

//定义接口LessAdder
type LessAdder interface {
	Less(b Integer) bool
	Add(b Integer)
}

func main() {
	//Integer拥有int类型所有的方法,且拥有int类型没有的Less和More方法
	var v1 Integer = 1
	//调用Interger的Less方法
	if v1.Less(50) {
		fmt.Println(v1," less 50")
	}

	//测试数组
	v2 := [3]int{1,2,3}
	Modify_arr(v2)
	fmt.Println(v2) //[1 2 3]

	//测试切片
	v3 := []int{1,3}
	Modify_slice(v3)
	fmt.Println(v3) //[500 2 3]

	//测试new出来的结构体是什么类型
	testRect1 := new(Rect)
	testRect1.x = 500
	testRect2 := testRect1
	testRect2.x = 300
	fmt.Println(testRect1,testRect2) //&{300 0 0 0} &{300 0 0 0}

	//创建初始化Rect类型
	rect1 := new(Rect)
	fmt.Println(rect1)

	rect2 := &Rect{}
	fmt.Println(rect2)

	rect3 := &Rect{0,100,200}
	fmt.Println(rect3)

	rect4 := &Rect{width: 200,height: 400}
	fmt.Println(rect4)

	rect5 := NewRect(20,20,200,200)
	fmt.Println(rect5)

	//Base
	base1 := &Base{"yunxuan"}
	base1.funca()
	base1.funcb()

	children1 := &Children{*base1,"peixuan"}
	children1.funca()
	children1.funcc()

	/*
		实现了一个接口里面的方法就视为实现了这个接口
		File实现了这些接口
	*/
	var file1 IFile = new(File)
	file1.Close()
	var file2 IReader = new(File)
	file2.Read([]byte{})
	var file3 IWriter = new(File)
	file3.Write([]byte{})

	var v4 Integer = 1
	var v5 LessAdder = &v4
	v4.Add(v4)
	v5.Add(v4)

}




笔记出自于 http://blog.csdn.net/zhouyunxuan

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读