The object of Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Object orientation of Golang

The go language does not follow the many concepts in traditional object-oriented programming, such as inheritance, virtual functions, constructors and destructors, hidden this pointers, and so on.

Class method

In the go language, you can add appropriate methods to any type, including built-in types, but not pointer types.

As in the following example, a new type of integer is defined, which, like int, adds a new method to its built-in int type less ()

int  func (a integer) less (b integer) bool {    < b}func main () {    = 1     if a.less (2) {        FMT. Println ("less then 2")    }   }

As you can see, the go language does not c++/java the hidden this pointer in objects of a custom type, but rather explicitly declares the object to which it belongs when defining a member method.

If you want to modify the value of an object, you can pass the object's pointer

Func (A *Ingeger) Add (b Integer) {    *a + = b}func Main ()    {= 1     a.add (3)    FMT . Println ("a =", a)     //  a = 4}

If the Add method does not use pointers, then a returns the same result because the parameters of the Go language function are also based on value passing.

As I said before, the go language has no concept of constructors, which is usually done using a global function. For example:

Func newrect (x, y, width, height float64) *Rect {    &rect{x, y, width, height}}   func main () { C15/>rect1:= Newrect (1,2,10,20)    FMT. Println (rect1.width)}

Anonymous combination

The go language provides inheritance, but employs a combination of syntax, which we call anonymous combinations, such as:

type Base struct {    *base) Set (MyName string) {    =*base) Get () string {    return Base.name}type Derived struct {    Base    intint) {    return Derived.name, Derived.age}func Main () {    b:= &derived{}    b.set ("Sina")    FMT. Println (B.get ())}

example, in which the base type defines the Get () and set () two methods, and the derived type inherits the base class and overwrites the Get () method, the derived object calls the set () method, the corresponding method of the base class is loaded, and when the Get () method is called, the Loads a method overridden by a derived class.

If the combined type and the combined type contain members of the same name, will there be a problem? You can refer to the following example:

type Base struct {    name string    intint) {    = myname    = Myage}type Derived struct {    Base    name String}func main () {    b:= &derived{}< C15/>b.set ("Sina", "the")    FMT. Println ("b.name =", B.name, "\TB.") Base.name = ", b.base.name)    FMT. Println ("b.age =", B.age, "\TB.") Base.age = ", b.base.age)}

Value semantics and referential semantics

The difference between value semantics and referential semantics is the assignment of values, such as

b = AB. Modify ()

If the modification of B does not affect the value of a, then this type is a value type, and 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, String, and so on;
    • Composite types, such as Arry, struct, pointer, etc.;

The array in C is particularly special, when passing an array through a function is based on referential semantics, but is based on value semantics when the struct defines an array variable. In the go language, there is no difference between an array and a primitive type, which is a purely value type, for example:

int {ab[= 1]++fmt. Println (A, b)   //[1 2 3] [1 3 3]

From the result, the B=a assignment statement is a complete copy of the array contents, and to express the reference, you need to use the pointer:

int {b[= &A//cited semantic1]++fmt. Println (A, b)   //[1 3 3] [1 3 3]

Interface

Interface is a set of abstract methods (methods that are not specifically implemented/methods that contain only method name parameter return values), and if all methods in Interface are implemented, that interface is implemented by that class/object.

Interface format:

Type InterfaceName Interface {      // method list  }  

Interface can be implemented by arbitrary objects, and a type/object can implement multiple Interface;
Method overloading is not supported;

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.