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)
}

親切

GO 言語におけるクラスの主な実装は、構造領域を通じてメソッドをバインドすることです。

ここでは主な主要な頭文字のケースを示します。

  • クラス名の最初の文字が大文字の場合、他のパッケージも Public と同様のメソッドにアクセスできることを意味します。
  • クラスのプロパティの最初の文字が大文字の場合、そのプロパティは外部からアクセスできることを意味します。それ以外の場合は、クラスの内部からのみアクセスできます。
package main

import "fmt"

//如果类名首字母大写,表示其他包也能够访问
type Hero struct {
    
    
	//如果说类的属性首字母大写, 表示该属性是对外能够访问的,否则的话只能够类的内部访问
	Name  string
	Ad    int
	level int
}

//this *Hero 要加指针才能是 对象,否则只是一个副本
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()
}

クラスの継承

type Father struct {
	Hero
	Ad    int
	level int
}
//类的继承直接加入结构体里面就好了

インターフェース

親クラスはインターフェイスに適用され、サブクラスは継承する必要がなく、すべてのメソッドを直接実装するだけです。

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)
}

インターフェース{}は「型アサーション」のメカニズムを提供します

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)
}

おすすめ

転載: blog.csdn.net/god_zzZ/article/details/120961262