8.golang create methods and interfaces

main Package 

Import (
"FMT"
"Math"
)

type struct {Movie
the Name String
Rating float32
}

/ *
creation methods: Create function to create a method with almost, but behind a func more recipients, the recipient may be referenced structure a method of forming current collector of a structure body
and the associated method of any structure, able to use his own method of concentration, so that an effective method of creating and packaging function library code can be formed
by this method, we can is understood as a category below (in golang structure), all the methods (all methods associated with this structure)
receiver parameter is declared as a pointer reference difference (that is, in the recipient, whether with an asterisk * values and reference symbol ): If you need to modify the original structure, use the pointer; if desired operation structure, without modifying the original structure, the value is to use
the method declaratively: type ... struct {}

: Interface specifies a set of methods, which It is a powerful way to achieve modular. The interface may be viewed as a blueprint methodology, which describes all the methods focused, but not implemented them.
Interface a variety of ways, polymorphic wanted. Polymorphism is a variety of forms, so that it can achieve a variety of interfaces.
Mode interface declaration: type ... interface {}

various functions and methods:
Multi parameter specifies a recipient, the method can be invoked on the data type, thereby increasing the degree of code reuse and modularity
Implement the interface methods in the interface does not contain a gram, you can add additional methods to the implementation of the interface, but this applies only to the structure, does not apply to the interface.

* /

Type struct {Sphere
the Radius float64
}

FUNC (S * Sphere) jiOne () // circle area float64 {
return float64 (. 4) + * Math.PI (s.Radius s.Radius *)
}

FUNC (S * Sphere ) jiTwo () float64 {// circle volume
radiusCubed: s.Radius * * = s.Radius s.Radius
return (float64 (. 4) / float64 (. 3)) * + radiusCubed Math.PI
}

// define an interface: PowerOn function, an interface function is described a method signature PowerOn: takes no parameters and returns the type of error
type interface Robot {
PowerOn () error
}

// implemented, the interface is a type of interface to be passed to the function as an argument
FUNC (A * Sphere) PowerOn is () {error
IF to true {
T: = ERR
return T
}
}

the Boot FUNC (R & lt Robot) {error
return r.PowerOn ()
}

FUNC main () {
S: = {Sphere the Radius:. 5}
fmt.Println (s.jiOne ())
fmt.Println (s.jiTwo ())
}

// this method is called stated
func (m * Movie) summary ( ) string {// (m * Movie) func --- after more than one parameter receiver, the receiver is a type of method, where the point structure of Movie pointer
// summary the method name, parameters, and returned to the type
var A String = "ABC"
return A
}

FUNC summary2 (m * Movie) {// String summary2 functions and methods herein summary statements equivalent.
A: = m.Name
return A
}

Guess you like

Origin www.cnblogs.com/iifeng/p/11488921.html