1. Object oriented representation and encapsulation in go language
Like Go language, it has the idea of facing objects. We can directly feel the definition and encapsulation of a class in Go language through an example.
package main import "fmt" type Human struct { Name string Gender string } // Bind some methods to attributes func (this *Human) SetName(name string) { this.Name = name; } func (this *Human) GetName() string{ return this.Name } func (this *Human) Show() { fmt.Println("this Human name is ", this.Name) fmt.Println("this Human gender is ", this.Gender) } func main() { // Declare a Human class object man := Human{Name: "tom", Gender: "female"} man.Show(); }
Operation results:
this Human name is tom
this Human gender is female
This is a simple way to define a class. I define a Human class here and declare a Human class object named man in the main method. Here are some small details. First, (this *Human) instead of (this Human), which ensures that the final operation of the method is the object calling the method, not the copy of the object. The first character of each method is capitalized, which means that the method is open to the public (visible to other packages). This is similar to the role of the public keyword in java. Similarly, if the first character is a lowercase letter, it means that it is not open to the public (invisible to other packages), and the same is true for attribute values (Name and Gender) (capital letters with the first character are open to the public, and lowercase letters are not open to the public).
In addition to the above code, the initialization method of the object can also be declared first and then assigned, such as:
package main import "fmt" type Human struct { Name string Gender string } // Bind some methods to attributes func (this *Human) SetName(name string) { this.Name = name; } func (this *Human) GetName() string{ return this.Name } func (this *Human) Show() { fmt.Println("this Human name is ", this.Name) fmt.Println("this Human gender is ", this.Gender) } func main() { // Declare a Human class object var man Human man.Name = "tom" man.Gender = "female" man.Show() }
Operation results:
this Human name is tom
this Human gender is female
The same effect can be achieved.
2. Object-oriented inheritance in go language
In order to better illustrate the characteristics of inheritance, we list the best understood relationship between animals and cats and dogs. First, we define an Animal class.
package main import "fmt" type Animal struct { Name string Gender string } func (this *Animal) Walk() { fmt.Println("the animal is walk") } func (this *Animal) Run(){ fmt.Println("the animal is run") } func (this *Animal) Show() { fmt.Println("this Animal name is ", this.Name) fmt.Println("this Animal gender is ", this.Gender) }
Next, we define a dog class, which should have the basic characteristics of Animal.
type Dog struct { Animal // Indicates that it inherits the Animal class Color string } func (this *Dog) Run(){ fmt.Println("the dog run fast") } func (this *Dog) Bark() { fmt.Println("dog is barking") }
There is an overridden method Run and a unique method Bark. In addition, there is an additional attribute Color. Let's write a test to see what methods the Dog class objects have.
func main() { dog1 := Dog{Animal: Animal{Name: "yellowdog", Gender: "female"}, Color:"yellow"} dog1.Walk() dog1.Run() dog1.Bark() dog1.Show() }
Operation results:
the animal is walk
the dog run fast
this Animal name is yellowdog
this Animal gender is female
You can see that the dog1 object not only has the methods of the Dog class, but also has the methods of the Animal class, and calls the new Run method that overrides it. For the initialization method of the object that inherits a class, in addition to the above methods, you can also use the method of declaration before assignment
func main() { var dog1 Dog dog1.Animal = Animal{Name: "yellowdog", Gender: "female"} dog1.Color = "yellow" dog1.Walk() dog1.Run() dog1.Bark() dog1.Show() }
In conclusion, go language also has the idea of facing objects, and the class name {attribute name: value,} can be used to create the object to be changed For inheriting a class, you can directly use the class as an attribute.