<>Go object-oriented programming

<> Anonymous field

* Anonymous field , By name , There is no name package main import "fmt" type Person struct { age int
name string sex bool } type MPerson struct { Person // Anonymous field id int } func
main() { var mperson MPerson = MPerson{Person{18, "xiaohua", true}, 1}
fmt.Println(mperson) }
* Anonymous field , It can be understood as MPerson Inherited Person Fields for
* Output of anonymous fields : fmt.Println(mperson.Person)
* Anonymous fields can even be unstructured data types , The form of output is the same as that of anonymous field of structure , See code package main import "fmt" type
Person struct { age int name string sex bool } type MPerson struct { Person int
} func main() { var mperson MPerson = MPerson{Person{18, "xiaohua", true}, 1}
fmt.Println(mperson.Person, mperson.int) }
* Anonymous fields can also be pointers , Pointer type initialization has been mentioned in previous articles , Look directly at the code package main import "fmt" type
Person struct { age int name string sex bool } type MPerson struct { *Person
int } func main() { var mperson MPerson = MPerson{&Person{18, "xiaohua", true},
1} fmt.Println(*(mperson.Person), mperson.int) }
<> Field with the same name

* Look at the code package main import "fmt" type Person struct { age int name string sex
bool } type MPerson struct { Person age int } func main() { var mperson MPerson
= MPerson{Person{18, "xiaohua", true}, 1} fmt.Println(mperson) }
* It can be found that MPerson Of age and Person In age The same name , It can be found through the code , In fact, it is 2 A field with the same name is not a field
* Can contact C++ To understand
* So if we export it now MPerson Of age field , Which is the output age What about it ?
* In fact, the output is MPerson Of age In the above code 1
notes :
If you want to output MPerson In Person In age
You can write that :
fmt.Println(mperson.Person.age)
It can be found that Go and C++ There is a difference , Here it is Person.age, instead of Person::age, Pay attention to it

<> method

* Methods are actually member functions in object-oriented , Let's look at the code first package main import "fmt" type Person struct { age
int id int } func (obj Person)f() { fmt.Println(obj.age) } func main() { person
:= Person{18, 1} person.f() }
notes :
func (obj Person)f()
Indicates that this is related to Person How to do it , Later calls can be made through the Person Object . To use this method

* in fact , The compiler will help you automatically convert pointers and objects , Even if you only have one method to call on the object , You can also call it with a pointer , as follows : package main
import "fmt" type Person struct { age int id int } func (obj Person)f() {
fmt.Println("hello") } func main() { p := &Person{18, 1} p.f() // It's OK to call with pointer }
* As I said before, if you want to change the method struct In value, Pointer must be passed , Because the compiler will help us convert automatically , So let's see if we can use the pointer to call the method for the object
package main import "fmt" type Person struct { age int id int } func (obj
Person)f() { obj.age = 10 } func main() { p := &Person{18, 1} p.f()
fmt.Println(p) }
notes :
The answer is no change
Because the compiler p Into *p, Call again f(), This is the value passing semantics , So it can't be changed , be careful

<> Method inheritance

* inherit package main import "fmt" type Person struct { age int id int } func
(obj Person)f() { fmt.Println("hello") } type MPerson struct { Person name
string } func main() { p := MPerson{Person{18, 1}, "xiaohua"} p.f() }
notes :
MPerson It contains Person Anonymous field for , Not only inherited the members , Also inherited the method

* rewrite package main import "fmt" type Person struct { age int id int } func
(obj Person)f() { fmt.Println("hello") } type MPerson struct { Person name
string } func (obj MPerson)f() { fmt.Println("hello everyone") } func main() {
p := MPerson{Person{18, 1}, "xiaohua"} p.f() }
notes :
Subclasses override methods of the parent class , When a subclass is called, it first finds the method from its own scope by default , If not, go to the parent scope to find it
If you want to call a method with the same name in the parent class , You can write that :
p.Person.f()

<> Method value

* Look at the code first package main import "fmt" type Person struct { age int id int } func
(obj Person)f() { fmt.Println("hello") } func main() { p := Person{18, 1}
function := p.f function() }
* function := p.f This sentence is actually preserved struct Method entry in , It can be understood like a function pointer
<> Method expression

* Look at the code package main import "fmt" type Person struct { age int id int } func
(obj Person)f() { fmt.Println("hello") } func main() { p := Person{18, 1}
function := (Person).f function(p) }
* function := (Person).f This is the method expression , When you call, you need to pass the corresponding variables into it.
* At this time, if I want to pass the pointer in, I can call it successfully as before ? The answer is No , Here is the strict type checking
<> Interface

* An interface type is an abstract type , It does not expose the internal structure and method set of the object it represents , Therefore, the interface type cannot be instantiated
* Interface has only method declarations , There is no way to achieve it , There are no data fields
* The definition of an interface is similar to : type The type name of the interface interface { Method name }
* A variable of an interface type can be assigned to any object that implements an internal method , actually , Go An interface similar to C++ The function of polymorphism package main import
"fmt" type Inter interface { f(data int) } type Person struct { age int name
string } func (person Person)f(data int) { fmt.Println(data) } func main() {
var myinterface Inter p := Person{18, "xiaohua"} myinterface = p
myinterface.f(10) }
notes :
If you want to assign an object to an interface , This object must implement all the methods of the interface , Otherwise, it will report an error

* Interface inheritance , Add the anonymous field of the inherited interface to the interface type Inter interface { f(data int) } type Int2er
interface { Inter f2() }
notes :
In this code , Inter It's a subset , Int2er It's a superset
Supersets can be transformed into subsets , The reverse is wrong
You can use it C++ To understand by analogy ,
Understanding supersets as subclasses , A subset is understood as a parent , Conversion can be understood as an assignment operation

<> Air interface

* Air interface , seeing the name of a thing one thinks of its function , There is no internal method , But an empty interface can receive any type of object value , See code package main import "fmt"
func main() { var i interface{} = 1 fmt.Println(i) i = "hello" fmt.Println(i) }
notes :
so , An empty interface can receive any type of value

* The use of null interface : Because the null interface can receive any type of value , So it works wonders when combined with variable parameters package main import "fmt" func
print(args... interface{}) { for _, data := range args { fmt.Println(data) } }
func main() { print(1, "hello") }

Technology
©2019-2020 Toolsou All rights reserved,
Hikvision - Embedded software written test questions C Language application 0 The length of array in memory and structure is 0 In depth analysis data structure --- The preorder of binary tree , Middle order , Subsequent traversal How to do it ipad Transfer of medium and super large files to computer elementui Shuttle box el-transfer Display list content text too long 2019 The 10th Blue Bridge Cup C/C++ A Summary after the National Games ( Beijing Tourism summary )unity Shooting games , Implementation of first person camera python of numpy Module detailed explanation and application case Study notes 【STM32】 Digital steering gear Horizontal and vertical linkage pan tilt Vue Used in Element Open for the first time el-dialog Solution for not getting element