1. 程式人生 > 其它 >golang 10. 結構體 物件 介面 struct class interface

golang 10. 結構體 物件 介面 struct class interface

struct.go

package main

import "fmt"

//宣告一種行的資料型別 myint, 是int的一個別名
type myint int

//定義一個結構體
type Book struct {
	title string
	auth  string
}

func changeBook(book Book) {
	//傳遞一個book的副本
	book.auth = "666"
}

func changeBook2(book *Book) {
	//指標傳遞
	book.auth = "777"
}

func main() {
	/*
		var a myint = 10
		fmt.Println("a = ", a)
		fmt.Printf("type of a = %T\n", a)
	*/

	var book1 Book
	book1.title = "Golang"
	book1.auth = "zhang3"

	fmt.Printf("%v\n", book1)

	changeBook(book1)

	fmt.Printf("%v\n", book1)

	changeBook2(&book1)

	fmt.Printf("%v\n", book1)
}

class1.go

package main

import "fmt"

//如果類名首字母大寫,表示其他包也能夠訪問
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 是呼叫該方法的物件的一個副本(拷貝)
	this.Name = newName
}
*/
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 是呼叫該方法的物件的一個副本(拷貝)
	this.Name = newName
}

func main() {
	//建立一個物件
	hero := Hero{Name: "zhang3", Ad: 100}

	hero.Show()

	hero.SetName("li4")

	hero.Show()
}

class2.go

package main

import "fmt"

type Human struct {
	name string
	sex  string
}

func (this *Human) Eat() {
	fmt.Println("Human.Eat()...")
}

func (this *Human) Walk() {
	fmt.Println("Human.Walk()...")
}

//=================

type SuperMan struct {
	Human //SuperMan類繼承了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 := Human{"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()
}

interface1.go

package main

import "fmt"

//本質是一個指標
type AnimalIF 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{"Yellow"}

	showAnimal(&cat)
	showAnimal(&dog)
}

interface2.go

package main

import "fmt"

//interface{}是萬能資料型別
func myFunc(arg interface{}) {
	fmt.Println("myFunc is called...")
	fmt.Println(arg)

	//interface{} 改如何區分 此時引用的底層資料型別到底是什麼?

	//給 interface{} 提供 “型別斷言” 的機制
	value, ok := arg.(string)
	if !ok {
		fmt.Println("arg is not string type")
	} else {
		fmt.Println("arg is string type, value = ", value)

		fmt.Printf("value type is %T\n", value)
	}
}

type Book struct {
	auth string
}

func main() {
	book := Book{"Golang"}

	myFunc(book)
	myFunc(100)
	myFunc("abc")
	myFunc(3.14)
}

/*
myFunc is called...
{Golang}
arg is not string type
myFunc is called...
100
arg is not string type
myFunc is called...
abc
arg is string type, value =  abc
value type is string
myFunc is called...
3.14
arg is not string type
*/