The method of object-oriented programming in Go language

Source: Internet
Author: User
Tags hypot
This is a creation in Article, where the information may have evolved or changed. #GO语言面向对象编程之方法 (UP) #<font size=5>, a programmer who has learned C + + or Java, should be familiar with object-oriented programming. We all know the three basic features of object-oriented programming: encapsulation, inheritance, polymorphism. In the go language, many concepts of object-oriented programming are simplified, such as inheritance, virtual functions, constructors, destructors, hidden pointers, and so on. For people who have been exposed to object-oriented programming, the Go language's object-oriented programming is more straightforward to understand. </font># the definition of a #方法 (class) <font size = 4> in a function declaration, a variable is placed before its name, which is a method. This additional parameter attaches the function to this type, which is equivalent to defining an exclusive method for the type. </font>package mainimport "math" import "FMT" type point struct {x float64y float64}func tradistance (P1 point, P2 poin T) float64 {return math. Hypot (p1.x-p2.x, P1.y-p2.y)}func (P1 point) objdistance (P2 point) float64 {return math. Hypot (p1.x-p2.x, P1.y-p2.y)}func main () {p1: = point{10, 20}p2: = point{20, 10}old_len: = Tradistance (P1, p2) New_len: = P1 . Objdistance (p2) fmt. PRINTLN ("The result of the old method is", Old_len) fmt. PRINTLN ("The result of OBJ method is", New_len)}<font color = red> in the go language, it is convenient for us to define some additional behaviors for some simple values, strings, slice, and maps. A method can be declared to any type, as long as it is not a pointer or a interface. </font>****# #指针对象引入指针对象的原因在于GO语言和C语言一样, is based on the value of the transfer, what meaning, is that you call the function, the value of the object is copied, so it is impossible to modify the value inside the object, see the following example:Type Shape struct {length float64height float64weight float64}func (S Shape) changeheight (value float64) {s.height = value }triangle: = Shape{1, 3, 4}triangle. Changeheight (Ten) fmt. PRINTLN (triangle.height)//Output 3func (s *shape) changeheightwithpoint (value float64) {s.height = Value}triangle: = Shape{1 , 3, 4}//(&triangle). Changeheightwithpoint (Ten)//triangle. Changeheightwithpoint (Ten) fmt. PRINTLN (Triangle.height)///The above two call methods can change the value of the object for the second why both methods can be called? Because the go language does the implicit conversion for us, of course, we can also use pointers to invoke non-pointers, like the above section triangle. Changeheight (10) can also be written (&triangle). Changeheight (10), the same go language will also help you implicitly convert to non-pointers. # #通过嵌入式结构体扩展类型 (similar to inheritance) the embedding and anonymous functions of structs are mentioned earlier. Let's look at one of the following examples: type point struct {x float64y float64}type Shape struct {pointlength float64height float64weight float64}func ( P1 point) objdistance (P2 point) float64 {return math. Hypot (p1.x-p2.x, p1.y-p2.y)}p1: = point{10, 20}p2: = point{20, 10}triangle: = Shape{p1, 1, 3, 4}len: = triangle. Objdistance (p2) fmt. Println (LEN) shape can be called directly to the function of the point object, see here is not the sense that this is the C + + inheritance, then we look at the following example: P1: POINT{10, 20}p2: = point{20, 10}triangle: = Shape{p1, 1, 3, 4}triangle2: = SHAPE{P2, 1, 3, 4}fmt. Printf ("The Point2 value in Triangle2 x=%f,y=%f", triangle2.x, triangle2.y) len: = triangle. Objdistance (Triangle2) This time the compiler will have the following error: <font color = Red>cannot use Triangle2 (type Shape) as type, point in argument To Triangle. Point.objdistance</font> for function objdistance, the parameter point type cannot be used with the shape type, even if the shape type has direct access to the variable of point. But for C + + or Java, Subclasses can be passed as arguments, such as the following code: #include <iostream> using namespace std;class parent{public:int x = ten, y = 20;}; Class Child:public parent{public:int m,n;}; int Test (Parent s) {cout << s.x <<s.y;} int main () {child childobj;test (childobj);//compile and run no problem} so the "inheritance" of the go language is not equivalent to other object-oriented language inheritance. # #方法值和方法表达式方法的值: Func (P1 point) objdistance (P2 point) float64 {return math. Hypot (p1.x-p2.x, p1.y-p2.y)}p1: = point{10, 20}p2: = point{20, 10}p1func: = P1. Expression for the Objdistancep1func (P2) Method: Func (P1 point), Objdistance (P2 point) float64 {return math. Hypot (p1.x-p2.x, p1.y-p2.y)}p1: = point{10, 20}P2: = point{20, 10}p2func: = Point.objdistancep2func (P1, p2) The expression of the method looks at one more parameter than the function, because the expression of the method takes the first parameter as the receiver, The arguments that follow are the true parameters of the function. # #封装在C + +, we use the public,private,protected keyword as the permission setting for variables or functions, but in the go language, as mentioned earlier, the go language is only case-sensitive, if you want to expose a function or variable to the outside, then capitalize the first letter, The opposite is lowercase. </font>291 Times Click  
Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.