1.struct
2.class
3.继承(父类+子类)
4.多态
5.interface
1.struct
package main
type myint int
//定义一个结构体
type book struct{
title string
auth string
}
//传递的是副本
func ChangeBook(book Book){
book.auth="666"
}
//传递的是指针
func ChangeBook2(book *Book){
book.auth="777"
}
func main(){
var book1 Book
book1.title="Golang"
book1.auth="zhang3"
fmt.Printf("%v\n",book1)
changeBook(book1)
fmt.Printf("%v\n",book1)
fmt.Printf("%v\n",book1)
changeBook(&book1)
fmt.Printf("%v\n",book1)
}
2.class
如果类名首字母大写,表示其他包也能够访问。
如果类属性首字母大写,表示该属性是对外能够访问的。否则只能够内部访问。
2.1.对象副本
package main
//对象副本
type Hero struct{
Name string //公有
Ad int
level int //私有
}
func (this Hero) Show(){
fmt.Println("Name=",this.Name)
fmt.Println("Ad=",this.Ad)
fmt.Println("Level=",this.Level)
}
func(this Hero)GetName() string{
return this.Name
}
func(this Hero)SetName(newName string){
this.Name=newName
}
func main(){
hero :=Hero{Name:"zhang3",Ad:100,Level:1}
hero.Show()
hero.SetName("li4")
}
2.2指针就是原来的对象
package main
//对象本身
type Hero struct{
Name string
Ad int
Level
}
func (this *Hero) Show(){
fmt.Println("Name=",this.Name)
fmt.Println("Ad=",this.Ad)
fmt.Println("Level=",this.Level)
}
func(this *Hero)GetName() string{
return this.Name
}
func(this *Hero)SetName(newName string){
this.Name=newName
}
func main(){
hero :=Hero{Name:"zhang3",Ad:100,Level:1}
hero.Show()
hero.SetName("li4")
}
3.继承(父类+子类)
package main
//=======父类=========================
type Human struct{
name string
sex string
}
func(this *Human)Eat(){
}
func(this *Human)walk(){
}
//=======子类===========================
type SuperMan struct{
Human
level int
}
//重定义父类的方法Eat()
func(this *SuperMan)Eat(){
fmt.Println("SuperMan.Eat()...")
}
//子类的新方法
func(this *SuperMan) Fly(){
fmt.Println("SuperMan.Fly()...")
}
func(this *SuperMan)Print(){
fmt.Println("name=",this.name)
fmt.Println("sex=",this.sex)
fmt.Println("level=",this.level)
}
func main(){
h :=Humam("zhang3","female")
h.Eat()
h.Walk()
//定义一个子类对象
s:=SuperMan{Human{"li4","female"},88}
var s SuperMan
s.name="li4"
s.sex="male"
s.level=88
s.Walk()//父类的方法
s.Eat()//子类的方法
s.Fly()//子类的方法
s.Print()
}
4.多态
4.1.有一个父类
4.2有子类,且实现了父类的全部接口方法。
package main
//本质是一个指针
type AnialIF interface{
Sleep()
GetColor() string
GetType() string
}
//具体的类
type Cat struct{
color string
}
func (this *Cat) Sleep(){
fmt.Println("Cat is Sleep")
}
func (this *Cat) GetColor() string{
return this.color
}
func (this *Cat) GetType() string{
return "cat"
}
//具体的类
type Dog struct{
color string
}
func (this *Dog) Sleep(){
fmt.Println("Dog is Sleep")
}
func (this *Dog) GetColor() string{
return this.color
}
func (this *Dog) GetType() string{
return "Dog"
}
func showAnimal(animal AnimalIF){
animal.Sleep() //你传什么,我调用什么 多态
fmt.Println("color=",animal.GetColor())
fmt.Println("kind=",animal.GetType())
}
func main(){
var animal AnimalIF //接口的数据类型,父类指针
animal=&Cat("Green")
animal.Sleep() //调用的是Cat的Sleep()方式
animal=&Dog("Yellow")
animal.Sleep() //调用的是Dog的Sleep()方式
cat := Cat{"Green"}
dog := Dog{"Yello"}
showAnimal(&cat)
showAnimal(&dog)
}
5.interface
断言+万能类型+普通接口
package main
import "fmt"
//interface{}是万能数据类型
func myFunc(arg interface{}){
fmt.Println("myFunc is called...")
fmt.Println(arg)
//给interface()提供“类型断言”机制
value,ok :=arg.(string) //指向的类型是否为string
if !ok{
fmt.Println("arg is not string type")
}else{
fmt.Println("arg is not string type,value=",value)
fmt.Printf("value type is %T\n",value)
}
}