Brother Lian Blockchain technology training sharing the object-oriented summary of Go language

Source: Internet
Author: User

Internet more than 20 years, has reached the crossroads. Before the advent of the blockchain, the internet was known as the classical Internet, and the use of blockchain technology in the Internet only entered the post-Internet era. As a new technology, the blockchain is undoubtedly on the cusp, and its development prospects for the general public will eventually be positive. But at present, because the blockchain technology is in the early stage of development, there are some problems, such as technology maturity and limited application scenario, the Brother Education advises users to carefully consider and discern before choosing professional Go Language + blockchain training institution.

Type $name struct{

PROPERTY01 int

property02 int

}

The methods and interfaces inside the Golang are based on the type created by this type of struct here, which can actually be understood as:


Class $name {

public int property01;

public int property02;

}

A type is a class.


So we're talking about a type of method that implements an interface.


A type is a collection of properties, an interface is a collection of methods


function definition: Func $funcName () () {}


Definition of the method: Func () $funcName () () {}


Func (member variable type) funname (local variable type, local variable type) (return value type) {}


Member variables are defined by type.


The argument list for a function is a local variable that needs to be passed.


The type signature of the Golang method:

1. Indicate which type to add the method to;

2. Specify whether the variable calling this method is a value type or a pointer type, and the variable calling this method must be signed by Type here to decide whether to use a value type or a pointer type, Golang can automatically convert, but you must ensure that the variable can be correctly converted to the corresponding value or pointer. For example, a variable of an interface type cannot be converted to a pointer to a struct.


Inherited:


When the type of a field (anonymous field) of type B is another type A, all the fields owned by type A are implicitly introduced to the currently defined type B. This implements the inheritance. A variable of type B can call all properties and methods of a. In other words, a inherits B.


When defining inheritance, a subclass typically contains an anonymous field with a type that is the parent class. An anonymous field is used to implement inheritance.


Package Main

Import (

"FMT"

)

Type Animal struct {

Name string

Age int

}

Func (Ani *animal) getage () int {

return ANI. Age

}

Type Dog struct {

Animal//animal Anonymous Field

}

Func Main () {

Dog: = dog{animal{"Dog", 10}}

Fmt. Println (dog. Age)

Fmt. Println (dog. Getage ())

}


Override of Method


If a type B implements the method in type a as its property. Then the value of this type B calls the method of its own type B, rather than the method of the property type A.

The code is as follows:


Package Main

Import (

"FMT"

)

Type Animal struct {

Name string

Age int

}

Func (Ani *animal) getage () int {

return ANI. Age

}

Type Dog struct {

Animal//animal Anonymous Field

}

Func (Ani Dog) getage () int {

return ANI. Age + 1

}

Func Main () {

Dog: = dog{animal{"Dog", 10}}

Fmt. Println (dog. Age)

Fmt. Println (dog. Getage ())

}


Interface


1 interface


1) Define the interface type

Defines an interface in which an unassigned method can be implemented.


Type Animal Interface {

Getage () int

}


1) Implementing the interface type

If a type implements all methods of an interface. This interface is implemented by this type.


Type Animal Interface {

Getage () int

}

Type Dog struct {

Name string

Age int

}


Implement the Getage () square rule to implement the animal interface


Func (Ani Dog) getage () int {

return ANI. Age

}

high-energy early warning, brother even education blockchain live course August continues to hit the hot!

The original price of 1188 Yuan of 12 block chain advanced courses, now only need 1 Yuan!

Also can receive free "Go Language foundation actual combat project development" and "Go language Advanced Combat Project development" textbook Two!! Limited time limit!! First come first served!!

Http://www.ydma.cn/open/course/24

Follow brother Lian Blockchain technology public number get more technical dry Goods Oh!!!

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.