The origin of

Object orientation: encapsulation, inheritance, polymorphism.

Compared with process oriented, object oriented adds the concept of object, the so-called object is the process of software development for structural abstraction, corresponding to the real transaction, in order to better describe the development process of transaction, this is a philosophical problem.

Software developed with object-oriented ideas is inherently design strong, such as the bible of object-oriented: design patterns.

But the language has been evolving, blindly pursuing object-oriented, blindly seeking the most design, will only increase the complexity of software.

There are many popular object-oriented languages, such as c++, c#, Java, etc., here take c# for example, c# in object-oriented structural design includes: class, abstract class, interface; Function implementation includes: abstract function, virtual function, function overload; Relationship implementations include inheritance, conventions, and so on. Light understanding of the above a bunch of concepts is enough for you to fumble for a while, if in the real development is more excessive, a small business, through object-oriented can make a huge code structure, if the final design is good, the design is not both time-consuming and bother. Someone may say: small business, what design, a bunch of fierce operation like tiger, others a look is rubbish.

Focus on

Before if you are heavy users of object-oriented, see Go object-oriented certainly think, there is nothing wrong with really is rubbish, but the garbage was just right, though not strong absolute good, including grammar works fine, how simple, how rough, with Go you will feel that work is a happy thing.

Object orientation in Go

interface

Define two interfaces, one Animal and one Dog

type IAnimal interface {
	Eat()
}

type IDog interface {
	Say()
	Sit()
}
Copy the code

Class (Interface body)

Classes are not supported in Go, but you can use structs to simulate the same effects as classes

type Dog struct {
	Name string
	Age int
}

dog:=Dog{Name:"Flower",Age:3}
fmt.Printf("My name is :% S, present :% D months old",dog.Name,dog.Age)

// outMy name is: Xiaohua, now:3Months oldCopy the code

Let the flower introduce itself

Now let’s add a self-introduction method for the flower

func (dog Dog) Say(a)  {
	fmt.Printf("My name is :% S, now :% D months old, Wang! wang!",dog.Name,dog.Age)
}
dog:=Dog{Name:"Flower",Age:3}
dog.Say()

//outMy name is: Xiaohua, now:3Months, wang! wang!Copy the code

Interface implementation

As with other languages, to implement an interface you must implement all the corresponding functions of the interface. In the example above, Xiaoxiao declared a Say() function, but did not declare Sit(), so Dog did not implement the IDog interface.

Sit();

type Dog struct {
	Name string
	Age int
}

func (dog Dog) Say(a)  {
	fmt.Printf("My name is :% S, present :% D months old",dog.Name,dog.Age)
}

func (dog Dog) Sit(a){
	fmt.Printf("I'm sitting down, Wang! wang!")}var dog IDog=Dog{Name:"Flower",Age:3}
dog.Say()
fmt.Println()
dog.Sit()

// outMy name is: Xiaohua, now:3One month old I sat down, Wang! wang!Copy the code

This is all the object-oriented implementation of Go, is not particularly simple, such a language you love it?

Go pits in object-oriented

In other object-oriented systems, objects default to reference types, but in Go, objects are emulated, so they are consistent with C. If C++ is an upgrade to C, Go is at best a development framework for C.

type Dog struct {
	Name string
}

func (dog Dog) RName(name string){
	dog.Name=name
}

func main(a) {
	dog:=Dog{
		Name: "Flower",
	}
	dog.RName("The egg")
	log.Println(dog.Name)
}
// outThe little flowerCopy the code

If the result is not surprising, change the example slightly:

// Just make the following changes
func (dog *Dog) RName(name string){
	dog.Name=name
}

// outTwo eggsCopy the code

By comparing the two examples above, do you understand?

conclusion

  1. Only interface declarations are supported in Go
  2. Implementing an interface means implementing all functions defined in the interface
  3. Go does not support inheritance
  4. The preferred form for defining object methods is func (dog * dog) RName(name String){}, unless you are very clear about the difference.