Go Language Learning Notes (3rd)-Object-oriented programming

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. A typical type system consists of:
    • Base type: Byte, int, bool, float, etc.
    • Composite type: Array, struct, pointer
    • Types that can point to any object (any type)
    • Value semantics and referential semantics
    • Object-oriented, that is, all types with object-oriented features
    • Interface
You can add a method to any type, including built-in types, in the Go language
In the example above we define a new type integer, there is no essential difference between integers and int, just add a method less () for the built-in int, which makes the integral type work like a normal class.
There is no hidden this pointer in the Go language 1) The target display passed by the method, not hidden 2) the target that the method imposes does not need to be a pointer, nor does it have to be called this
The go language, like the C language, is a type that is passed by value, and to change the value of a variable, you can only pass pointers in the function.
1. Type System
1.1. Value semantics and referential semanticsThe difference between value semantics and referential semantics is assignment B = a b.modify ()
If the modification of B does not affect the value of a, then this type belongs to the value type. If the value of a is affected, then this type is a reference type.
Most of the types in the go language are value semantics, including: Basic types: Byte, int, bool, float32, float64, and string composite types: array, struct, pointer, etc.
The value semantics of the types in the go language are very thorough.
1.2. Structural BodyThe go language discards a large number of object-oriented features, including inheritance, preserving only the most basic features of the composition.
A combination cannot be an object-oriented feature, because in a procedural programming language such as C, there are also structs and combinations. A combination is simply the basis of a form compound type.
There is no obvious difference between the use of structures in the go language and the C language.
2. InitializeIn the go language, a variable that is not explicitly initialized is initialized to a value of 0 for that type, for example, a value of 0 for a bool type is false, a 0 value of type int is 0, and a string type 0 value is an empty string.
The structure has several initial methods, as follows:

3. Anonymous combinationTo be exact, the go language also provides inheritance, but employs a combination of grammars, so we call it an anonymous combination.
4. VisibilityThe go language is very stingy with keyword additions, no private, protected, public keywords. To make a symbol visible to other packages, you need to define the symbol to start with an uppercase letter.
5. InterfaceThe interface of the go language is not the interface concept provided in other languages. Java is now the interface is intrusive interface, go interface is non-intrusive.
In the go language, when a class implements all the functions required by the interface, we say that the class implements the interface.
In the Go language, the interface assignment is divided into the following two scenarios in the Go language: 1. Assigns an object instance to interface 2. Assigning an interface to another interface

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.