Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
31
Task/Inheritance-Multiple/Go/inheritance-multiple-1.go
Normal file
31
Task/Inheritance-Multiple/Go/inheritance-multiple-1.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
// Example of composition of anonymous structs
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Two ordinary structs
|
||||
type camera struct {
|
||||
optics, sensor string
|
||||
}
|
||||
|
||||
type mobilePhone struct {
|
||||
sim, firmware string
|
||||
}
|
||||
|
||||
// Fields are anonymous because only the type is listed.
|
||||
// Also called an embedded field.
|
||||
type cameraPhone struct {
|
||||
camera
|
||||
mobilePhone
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Struct literals must still reflect the nested structure
|
||||
htc := cameraPhone{camera{optics: "zoom"}, mobilePhone{firmware: "3.14"}}
|
||||
|
||||
// But fields of anonymous structs can be referenced without qualification.
|
||||
// This provides some effect of the two parent structs being merged, as
|
||||
// with multiple inheritance in some other programming languages.
|
||||
htc.sim = "XYZ"
|
||||
fmt.Println(htc)
|
||||
}
|
||||
47
Task/Inheritance-Multiple/Go/inheritance-multiple-2.go
Normal file
47
Task/Inheritance-Multiple/Go/inheritance-multiple-2.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
// Example of composition of interfaces.
|
||||
// Types implement interfaces simply by implementing functions.
|
||||
// The type does not explicitly declare the interfaces it implements.
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Two interfaces.
|
||||
type camera interface {
|
||||
photo()
|
||||
}
|
||||
|
||||
type mobilePhone interface {
|
||||
call()
|
||||
}
|
||||
|
||||
// Compose interfaces. cameraPhone interface now contains whatever
|
||||
// methods are in camera and mobilePhone.
|
||||
type cameraPhone interface {
|
||||
camera
|
||||
mobilePhone
|
||||
}
|
||||
|
||||
// User defined type.
|
||||
type htc int
|
||||
|
||||
// Once the htc type has this method defined on it, it automatically satisfies
|
||||
// the camera interface.
|
||||
func (htc) photo() {
|
||||
fmt.Println("snap")
|
||||
}
|
||||
|
||||
// And then with this additional method defined, it now satisfies both the
|
||||
// mobilePhone and cameraPhone interfaces.
|
||||
func (htc) call() {
|
||||
fmt.Println("omg!")
|
||||
}
|
||||
|
||||
func main() {
|
||||
// type of i is the composed interface. The assignment only compiles
|
||||
// because static type htc satisfies the interface cameraPhone.
|
||||
var i cameraPhone = new(htc)
|
||||
// interface functions can be called without reference to the
|
||||
// underlying type.
|
||||
i.photo()
|
||||
i.call()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue