Go language Learning (11) object-oriented programming-type system

Source: Internet
Author: User

1. Type System Introduction

Support for object-oriented programming the Go language is designed to be simple and elegant. The simple point is that the go language does not follow
Many concepts in traditional object-oriented programming, such as inheritance, virtual functions, constructors and destructors, hidden this
Needles and so on.
A typical type system typically contains the following basic elements:

    • Basic types, such as Byte, int, bool, float, etc.;
    • Composite types, such as arrays, structures, pointers, etc.;
    • Can point to the type of any object (any type);
    • Object-oriented, that is, all types with object-oriented features (such as member methods);
    • Interface.

Since the Java language has been called the purest object-oriented language since its inception, we will first speak of the type system in the Java language.
In the Java language, there are two completely separate types of systems: a set of value type systems, primarily basic types, such as Byte,
int, Boolean, char, double, and so on, these types are based on value semantics; one set is an object type that is rooted in type
Systems, which can define member variables and member methods, can have virtual functions, are based on referential semantics, and only allow creation on the heap
(by using the keyword new). The any type in the Java language is the root of the entire object type system--java.lang.object
Type, only instances of the object type system can be referenced by the type objects. Value types want to be referenced by Object type, need to be boxed
(boxing) procedures, such as int types, need to be boxed into an Integer type. In addition, only types in the object type system can
To implement an interface, by allowing the type to inherit from the interface being implemented.

In contrast, most types in the go language are value semantics and can contain corresponding action methods. When you need
, you can "add" new methods to any type (including built-in types). While implementing an interface, you do not need to
This interface is inherited (in fact, the go language does not support the inheritance syntax in object-oriented thinking at all), just implement the interface
All the required methods. Any type can be referenced by any type. The any type is an empty interface, or interface{}.

2. Adding a method to a type

In the go language, you can add an appropriate method to any type, including built-in types, but not pointer types.
For example:

package  Mainimport  ( "FMT" ) // Define a new type  type  Integer int  func  Main () {//use a custom type definition variable  var  a Integer = 3  if  a.less (4 ) {FMT. Println (A, "less 4" )}}//define method, caller is an integer type, Receive integer type, return bool value  func  (a integer) less (b integer) bool  {return  a < b}  

In this example, we define a new type of Integer, which is not fundamentally different from int, except that it is a built-in
The int type adds a new method less (). This enables the integer to be used just like a normal class.
The method above is called by type. Method Name (parameter list), for example:
a.Less(4)
This method of invocation is an object-oriented embodiment.

He and the process have obvious differences, as follows to modify the process-oriented method definition.

func Integer_Less(a,b integer)bool{    return a < b}

The invocation is a method name (parameter list), for example:
Integer_Less(a,2)

3. modifying objects with pointers

The object-oriented in the go language is the most intuitive and requires no additional cost. If a requirement object must be passed with a pointer,
This can sometimes be an extra cost, because objects are sometimes small (such as 4 bytes) and passing with pointers is not a good deal.
You must use a pointer only when you need to modify the object. It is not a go language constraint, but a natural constraint.
As an example:

package mainimport(    "fmt")typeintfunc main(){    var a Integer = 3    a.Add(3)    fmt.Println("a=",a)//a=6}//通过Integer的指针调用    func (a *Integer) Add(b Integer){     //修改变量a的指针对应的值}

运行该程序,得到的结果是: a=6 。
If you implement a member method, the caller is not a pointer but a value (that is, the incoming Integer, not the *integer)
As shown below:

func (a Integer) Add(b Integer) {    a += b}

那么运行程序得到的结果是a=3
That is to maintain the original value, this requires special attention.
The reason is that because the go language is the same as the C language, the types are all based on value. To modify the value of a variable, you can only
Passing pointers.

4. Value semantics and referential semantics;

The difference between value semantics and referential semantics is the assignment of values
If the modification of B does not affect the value of a, then this type belongs to the value type. If it affects the value of a,
Then this type is a reference type.
Most types in the go language are based on value semantics, including:

    • Basic types, such as Byte, int, bool, float32, float64, and string;
    • Composite types, such as array, struct (struct), and pointer (pointer).

There is no difference between an array and a primitive type in the go language, which is a purely value type, such as:

var[3]int{1,2,3}var b = ab[1]++fmt.Println(a, b)

The results of this program are as follows:
[1 2 3] [1 3 3]。
This indicates that the B=a assignment statement is a complete copy of the array contents. To express a reference, you need to use the pointer:

var[3]int{1, 2, 3}var b = &ab[1]++fmt.Println(a, *b)

The results of this program are as follows:
[1 3 3] [1 3 3]
This indicates that the B=&a assignment statement is a reference to the contents of the array. The type of variable B is not [3]int, but *[3]int type.

There are 4 types in the go language that are more specific and look like reference types, as shown below.
1. Array slice: An interval that points to an array.
2.map: Very common data structure, provide key value query ability.
3.channel: Communication facilities between actuators (Goroutine).
4. Interface (interface): An abstraction of a set of types that satisfy a contract.
However, this does not affect the use of the Go language type as value semantics.

An array slice is essentially an interval, and you can roughly represent []t as:

typestruct {    first *T    lenint    capint}

Because the inside of an array slice is a pointer to an array, it is not surprising that you can change the array elements that you point to. Array slices
The assignment of the type itself is still a value semantics.

5. Structural body

The Go language structure (struct) has the same status as other language classes (class), but the go language discards the following
A large number of object-oriented features, preserving only the most basic features of the combination (composition).
The combination is not even an object-oriented feature, because in a procedural programming language such as C, there are also structures and
Combination. A combination is just the basis for forming a composite type.
As we said above, all the Go language types (except pointer types) can have their own methods. In this context,
The structure of the go language is just a common compound type, which is bland. For example, we want to define a rectangular type:

typestruct {    float64    float64}//然后我们定义成员方法 Area() 来计算矩形的面积:funcfloat64 {    return r.width * r.height}

You can see that the use of the structure in the go language is not significantly different from the C language.

Go language Learning (11) object-oriented programming-type system

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.