Pseudo-Inheritance of anonymous combination in Golang
Source: Internet
Author: User
This article is also published in the Personal csdn blog:https://blog.csdn.net/ggq89/article/details/82084338> "The object-oriented mechanism of Go language is different from the general language. It does not have a class hierarchy, or even a class, and simply constructs complex objects by combining (rather than inheriting) simple objects. --"Go Language Bible" # 1. struct embedding and anonymous member go languages provide a different ' struct embedding ' mechanism that allows one struct to contain an ' anonymous member ' of another struct type, so that nested X.D.E.F members in an anonymous member chain can be accessed through a simple point operator, x.f. The Go language has a feature that lets us declare only one member's data type and not the name of the member, which is called an anonymous member. The data type of an anonymous member must be a named (not anonymous) type or a pointer to a named type. ' Gotype Circle struct {pointradius int} type Wheel struct {circlespokes int} ' ' Because of an anonymous embedded attribute, We can directly access the member variables of the inline type without giving the complete path: ' ' Govar w wheelw.x = 8//equivalent to W.circle.point.x = 8w. Y = 8//equivalent to W.circle.point.y = 8w. Radius = 5//equivalent to W.circle.radius = 5w. Spokes = 20 "the same rule, the inline type method is also promoted to the external type of the method. # 2. Anonymous combinations are not inherited # # 2.1 The recipient of the method has not changed > when we embed a type, the method of this type becomes the method of the outer type, but when it is called, the recipient of the method is the inner type (the embedded type), not the outer type. -effective Go ' gotype Job struct {Command string*log. Logger}func (Job *job) Start () {job. Log ("Starting now ...") ...//do something job. Log ("started.")} "Above this job example, even after the combination of calls into the job." Log (...), but the receiver of the log function is still log. Logger pointers, so it is not possible to access other member methods and variables of the job in log. # 2.2 Inline type is not a base class if the reader is based on ' class 'To implement the object-oriented language is familiar, it may be inclined to consider ' inline type ' as a base class, while ' external type ' is considered its subclass or inheriting class, or ' external type ' is considered ' an ' is a ' ' embedded type '. But it is wrong to understand this. "' Gotype point struct{X, Y float64}type coloredpoint struct {pointcolor color. Rgba}func (P point) Distance (Q-point) float64 {dX: = q.x-p.xdy: = Q.y-p.yreturn Math. SQRT (DX*DX + dy*dy)} "notice the call to the distance method in the example above. Distance has a parameter of type point, but Q is not a point class, so although Q has an inline type of point, we also have to explicitly select it. If you try to pass the Q directly, you will see the error: "' Gored: = color. rgba{255, 0, 0, 255}blue: = Color. rgba{0, 0, 255, 255}var p = coloredpoint{point{1, 1}, Red}var q = coloredpoint{point{5, 4}, blue}fmt. Println (P.distance (Q.point))//"5" p.distance (q)//compile Error:cannot use Q (coloredpoint) as point "a coloredpoint and is not a point, but Coloredpoint ' has a ', and it has the distance method introduced from the point class. In fact, from the perspective of implementation, the inline field instructs the compiler to generate an additional wrapper method to delegate the already declared method, and the following form is equivalent: "' Gofunc (P coloredpoint) Distance (q point) float64 { return P.point.distance (q)} "" When Point.distance is called by the wrapper method generated by the above compiler, its receiver value is p. Point, not p. # 2.3 Anonymous conflicts (duplicate field) and implicit names Anonymous members also have an implicit name,The name of the member variable with its type name (minus the package name part). Therefore, you cannot include two anonymous members of the same type at the same level, which can cause name collisions. "' Gotype Logger struct {level int}type MyJob struct {*logger Name string *log. Logger//duplicate field Logger} "The following two points all indirectly indicate that the anonymous combination is not inherited: * Anonymous members have implicit names * Anonymous may conflict (duplicate field)---* * Reference article: * * * Effective Go # embedding:https://golang.org/doc/effective_go.html#embedding* Golang A question that inherits pointers and non-pointers: https://segmentfault.com/q/ 1010000002687684/a-1020000002688273* [Golang note] Anonymous combo: https://www.cnblogs.com/heartchord/p/5254564.html116 times click
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.