In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the object-oriented detailed explanation of GO language". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the object-oriented explanation of the GO language.
Preface
Friends who have learned C++ language know that object-oriented mainly includes three basic features: encapsulation, inheritance and polymorphism. Encapsulation, which refers to the binding of running data and functions together, is mainly accomplished through this pointers in C++; inheritance means that class can inherit properties and functions from each other; polymorphism, mainly using a unified interface to deal with general logic, each class only needs to implement its own callback function according to the interface.
As an aggregator, the Goto language will not do nothing on object-oriented. Compared with C++, Java, C # and other object-oriented languages, its object-oriented is simpler and easier to understand.
There is no such kind of Class in the go language as that in the Java language, it only contains structures like those in the C language, uses features such as structures and pointers to complete the function of a class, and skillfully uses pointers and structures, not only with the object-oriented of go, but also with the help of structures for operations such as map in the go language. In fact, to put it bluntly, in object-oriented languages such as C++ and Java, the underlying implementation of the class is the structure, and the reference to the object is the pointer, but the language encapsulates them. However, this makes many people do not understand these things when they are first exposed to object-oriented.
Let's talk about how the object-oriented is written in go:
If we want to define a Rect in Java, we can find its area. We should write this.
Public class Rect {public int x; public int y; public int Area () {return xeroy;}}
It's simple, so what do you do in the Go language? There is no such thing as a class in Go. All classes are represented by structs, so to write a class, we first have to define a struct:
Type Rect struct {x, y int}
This is a Rect structure, so in a class, there must be not only variables, but also member functions, so the member functions of go are written as follows:
Func (r * Rect) Area () int {return r.x*r.y}
The function of this member function is to calculate the area. Obviously, the value of this member function is applicable to the structure of Rect, so it realizes the so-called encapsulation, so how do we create and initialize an instance of the class?
The go language provides many ways:
Rect: = new (Rect) rect: = & Rect {} rect: = & Rect {1Magol 2} rect: = & Rect {XRO 3pl YRO 4}
Then, in one case, if you do not specify the size of the member variable, the go language initializes the member variable as false if the member variable is of type 0recoverbool.
What about the constructor?
We can write like this:
Func NewRect (x _ int y int) * Rect {return & Rect {x _ r _ y}}
In fact, this is also the real operation when we usually new an object, but go really shows it to us.
Seeing here, we still seem to have a question, that is, for the description of visibility in java and C++, there are no keywords such as public in go language, but we directly choose to use letter case control in go language.
A variable that begins with an uppercase letter is visible to other packages, or lowercase letters if you want it to be invisible, but the visibility control in the go language is only for packages, not for classes, that is, classes under the same package are visible. At this point, we can know why the output statement is written like this:
Fmt.Println ("hello world")
Because this function is visible to other packages.
At this point, I believe you have a deeper understanding of the "object-oriented detailed interpretation of the GO language". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.