Deep understanding of object orientation of GO language

  • 2020-06-03 06:44:11
  • OfStack

preface

As anyone with C++ language experience knows, object orientation includes three basic characteristics: encapsulation, inheritance, and polymorphism. Encapsulation means that the data and functions run are bound in 1. In C++, this pointer is mainly used to complete it. Inheritance means that class can inherit attributes and functions from each other. Polymorphic is mainly to use the interface of Unified 1 to handle common logic. Each class only needs to implement its own callback function according to the interface.

As the master Go language, it is not natural to do nothing with object orientation. Compared with C++, Java, C# and other object-oriented languages, it is simpler and easier to understand.

There is no Class in go language like C++ or Java language. It only contains structure like C language. It USES structure and pointer and other features to complete the function of a class. In fact, in object-oriented languages like C++ and Java, the underlying implementation of the class is the structure, and the reference of the object is the pointer, but the language encapsulates them. However, this makes it hard for many people to understand object orientation when they first come across it.

Here is how to write the object orientation in go:

So if we want to define an Rect in Java, we can figure out the area, so we should write it this way


public class Rect {
 public int x;
 public int y;
 public int Area() {
 return x*y;
 }
}

That's easy. So what does Go do? There is no such thing as a class in Go. All classes are represented by structs, so to write a class, we first need to define a struct:


type Rect struct {
 x, y int
}

This is a struct of Rect, so in a class, there should be not only variables, but also member functions. So the member function of go is written as follows:


func (r *Rect) Area() int {
 return r.x*r.y
}

The function of this member function is to compute the area. Obviously, the value of this member function applies to the structure of Rect, thus achieving the so-called encapsulation. So, how do we create and initialize the instance of the class

The go language offers many ways:


rect :=new(Rect)
rect :=&Rect{}
rect :=&Rect{1,2}
rect :=&Rect{x:3,y:4}

So, in the 1 pass case, go will initialize the member variable to 0 by default if the size is not specified, bool Types of for false .

What about constructors?

We could write it this way:


func NewRect(x,y int) *Rect {
 return &Rect{x,y}
}

In fact, this is also the real operation of new1, but go shows it to us.

Here, we seem to have a question, that is, java, C++ description of visibility, go language does not exist public The go language directly chooses to use alphabetic case control.

A variable that starts with an uppercase letter is visible to other packages, or lowercase if you want it to be invisible, but the visibility control in go is only for packages, not classes, that is, classes in the same package are visible. At this point we can see why the output statement is written like this:


fmt.Println("hello world")

Because this function is visible to other packages.

conclusion

The above is the whole content of GO language object orientation. I hope the content of this article can be helpful for you to learn or use GO language. If you have any questions, please feel free to leave a message.


Related articles: