golang学习第三节
文章目录
cpp选手学习go的笔记
一、map
1.map的三种声明方式
func main(){
var mymap1 map[string]string
// 判断map是否为空map
if(mymap1==nil){
fmt.Println("xxxxx\n")
}
//分配空间
mymap1 = make(map[string]string, 10)
mymap1["one"] = "1"
mymap1["second"] = "2"
fmt.Println(mymap1)
// 通过推导类型直接声明
mymap2 := make(map[int]string)
mymap2[1] = "cpp"
mymap2[2] = "java"
fmt.Println(mymap2)
mymap3 := map[string]string{
"one": "cpp",
"two": "jvvv",
}
fmt.Println(mymap3)
}
2.map基本操作
func printmap(citymap map[string]string){
for key,value := range citymap{
fmt.Println("key = ",key)
fmt.Println("value = " ,value)
}
}
func changemap(citymap map[string]string){
citymap["US"] = "LD" // 会影响外部传入的map
}
func main(){
citymap :=make(map[string]string)
//添加
citymap["china"] = "beijing"
citymap["japan"] = "tokyo"
citymap["usa"] = "newyork"
printmap(citymap)
//删除
delete(citymap,"usa")
//修改
citymap["japan"] = "ribenren"
fmt.Println("+++++++++++++")
printmap(citymap)
fmt.Println("------------------------")
changemap(citymap) // 引用传值
printmap(citymap)
}
二、struct结构体
type Book struct{
title string
name string
}
func changebook(book Book){
book.name = "xxxxxx" // 修改的是副本,对传入的数据无影响
}
func changebook2(book *Book){
book.title = "77777" // 指针传递,修改会对原本的数据也同样进行修改
}
func main(){
var book1 Book
book1.title = "abc"
book1.name = "luxun"
fmt.Printf("%+v\n", book1)
fmt.Println("-------------------")
changebook(book1);
fmt.Printf("%+v\n", book1)
fmt.Println("-------------------")
changebook2(&book1)
fmt.Printf("%+v\n", book1)
fmt.Println("-------------------")
}
三、Go面向对象类的表示与封装
1.面向对象表示
// 首字母大写表示此类对外部包可使用,若为小写只对当前包内使用
type Human struct{
Height int // 成员变量首字母大写表示共有,小心为私有
Name string
}
// 通过this Human绑定Human类
func (this Human)show(){
fmt.Println("name is ",this.Name)
fmt.Println("height is ",this.Height )
}
// 此处用指针进行传递,否则是对副本进行修改
func (this* Human)Setheight(h int){
this.Height = h
}
func main(){
human := Human{Name : "zhangsan", Height : 120}
human.show()
//name is zhangsan
//height is 120
fmt.Println("===============")
human.Setheight(100)
human.show()
//name is zhangsan
//height is 100
}
2.继承
type Human struct{
Height int
Name string
}
type Student struct{
Human // 继承父类写在第一行
Weight int //声明自己的成员变量
}
//绑定父类
func (this Human)CallType(){
fmt.Println("this is human class")
}
//绑定子类
func (this Student)CallType(){
fmt.Println("this is student type")
}
func (this Human) Getname()string{
return this.Name
}
func main(){
stu1 := Student{Human{180,"lsi"},130} //声明并初始化一个子类
human1 := Human{180,"zhangsan"}
//同名函数的调用
human1.CallType() //输出:this is human class
stu1.CallType() //输出:this is student class
fmt.Println(human1.Getname())//父类调用自己的方法
fmt.Println(stu1.Getname())//子类依然可以调用自己的方法
}
3.多态
//声明一个interface接口,里面包含若干个方法
type AnimalIF interface {
Sleep()
GetColor() string
GetType() string
}
type Cat struct{
color string
}
//子类若是想要实现接口中的某一方法,就必须将该接口中的所有方法都实现
func (this *Cat) Sleep(){
fmt.Println("cat is sleeping")
}
func(this *Cat) GetColor()string{
return this.color
}
func(this *Cat) GetType()string{
return "Cat"
}
func main(){
var animal AnimalIF
animal = &Cat{"Blue"}
// 另外的声明方式 animal := &Cat{"Blue"}
// 自动推导为AnimalIF类型
animal.Sleep()
fmt.Println(animal.GetColor())
fmt.Println(animal.GetType())
}
四、interface 万能类型
package main
import "fmt"
// 万能类型
func myfunc(arg interface{}){
fmt.Println(arg)
//interface 提供断言,可用于判断类型
value,ok := arg.(string)
if !ok {
fmt.Println("arg is not a string")
}else {
fmt.Println("arg is string, the value is ",value)
fmt.Printf("value type is %T\n",value)
}
}
func main(){
myfunc(100)
myfunc("abc")
//100
//arg is not a string
//abc
//arg is string, the value is abc
//value type is string
}
1234




被折叠的 条评论
为什么被折叠?



