Object-Oriented Programming chapter
Go language object-oriented programming, not like the traditional object-oriented programming has inheritance, encapsulation, polymorphism, function overloading and other concepts;
Go language Object-oriented programming is very simple and elegant, structure (struct), method, interface (interface)
Seamless, supporting object-oriented programming together
First, struct struct
Go language support basic type combination function-struct structure type, struct structure type seemingly simple, actually can achieve very powerful complex function
1. Definition
Type <name> struct {}
Type Rect struct {
X, y float64
width, height float64
}
2. Initialization
There are several ways to initialize 2.1:
Rect1: = new (Rect)//Returns a pointer to type
Rect2: = &rect{}//initialized to an empty struct type
RECT3: = &rect{1, 1, 2, 2}//sequence-by-step implicit initialization
Rect4: = &rect{width:2, height:2}//display initialization
PS:1) When the structure is initialized, it is suggested to take the address symbol for the structure, the main reason is that the struct type is the value pass, and the struct is modified within other function methods, and the struct is not affected.
2) variables that are not explicitly initialized are initialized to 0 values of that type
2.2 The creation of objects can also be done using a global creation function, which represents "constructors", generally named Newxxx
Func newxxx (x, y, width, height float64) *rect {
Return &rect{x, y, width, height}
}
3. Anonymous structure
3.1 Anonymous structure is a struct with no name of type
3.2 Definition and initialization
3.2.1 Direct Definition and initialization
A: = &struct {
Name string
Age int
}{
Name: "Xuww",
AGE:20,
}
3.2.2 in the form of an embedded structure
Type X struct {
Name string
}
Type Y struct {
x//with the X type as the field name
Age int
}
S: = &y {x:x{"Xuww"}, age:20}
3.2.3 to embed anonymous structures
Type Y struct {
X struct {
Name string
}
Age int
}
4. Anonymous fields
4.1 Anonymous fields are fields that have no name for the type
An anonymous field is essentially a field that defines a type as its name
5. Embedding structure
Although there is no inheritance and polymorphism in the go language, it is possible to implement similar functions by embedding the structure
Type Base struct {
Name string
}
Func (b *base) Foo () {
Fmt. Println (B.name, "from Base Foo ()")
}
Func (b *base) Bar () {
Fmt. Println (B.name, "from Base Bar ()")
}
Type Sub struct {
Base//"Inherit" Base all properties
Age int
}
Func (S *sub) bar () {//In the Sub method, override the Bar () function, polymorphic
S.name = "Hello"
Fmt. Println (S.name, "from Sub Bar ()")
}
6. Same name field
The same name field cannot appear at the same level
The same name field can appear at different levels
Type A struct {
The name level in b//struct A is higher than the name level in struct B, so you can have the same name
c//B C struct name is the same level, the same name field will be faulted
Name string
}
Type B struct {
Name string
}
Type C struct {
Name string
}
Lower-level Same-name fields are hidden and need to be displayed when using the call
Similar: A.b.name
7. Anti-Quote
After the field of the struct's type declaration, we can add a label enclosed by the anti-quote "'" for reflect use:
Second, the method
Go language Object-oriented programming is very simple and intuitive, through the method applied to the target (object) display transmission, you can clearly see whose method,
Unlike C + + in Object-oriented programming, the this pointer is hidden.
1. Overview of methods
1.1 Methods and functions are similar, just more receiver, the compiler is based on receiver, to determine whose method
1.2 In the go language, there is no method overload
1.3 Receiver also follows normal parameter rules, and if it is passed as a value type, only one copy (copy) of the object can be obtained.
So if you need to modify the object, you must pass it with the pointer.
1.4 In the Go language, you can add an appropriate method to any type, including built-in types, but not pointer types.
2. Method Value and Method Expresion
Method Value
Receiver has been declared, calling the method as a variable or object to invoke a function
Method Expression
Call a method by type, use a variable or object as receiver, and pass it as the first parameter to the method
Type IT int
Func (It *it) Print () {
Fmt. Println ("IT Method")
}
var a IT
A.print ()//method Value
(*it). Print (&a)//method Expression
3. Method name Conflicts
Embedded structures have field name collisions, and methods also have name collisions.
The principle of processing is similar to a field name conflict, namely:
If the external structure and the embedded structure have the same name method, the method of calling the external structure is preferred;
If the same object has the same method (receiver + function name), the compilation will error
Third, the interface
In other object-oriented programming languages, the implementation class needs to explicitly declare which interface it implements, which is not conducive to development,
But in the go language, as long as a type implements all the methods of that interface, that interface is implemented, without showing which interface the declaration implements, which is called Structural Typing
1. Interface definition
An interface is a collection of one or more method signatures, with only method declarations, no implementations, no data fields
Type USB Interface {
Phone () string
Connect ()
}
2. Assigning values to interfaces
There are two types of interface assignment:
Assigning an object instance to an interface
Assigning an interface to another interface
2.1 Assigning an object to an interface
Type A interface {...}
Type B struct {
Name string
}
Assume that type B implements the A interface
var a = b{"HI"}
or var a = &b{"HI"}
PS: When assigning an object to an interface, a copy occurs, and a pointer to a copy of the song is stored inside the interface, and the black wire
2.2 Assigning an interface to another interface
As long as two interfaces have the same method list (the order does not matter), then they are equal, can be assigned to each other;
You can convert the owning superset interface to a subset interface
3. Embedded interface (somewhat like inheritance)
Interfaces can be anonymously embedded in other ports
Type USB Interface {
Phone () string
CONNECT//Anonymous interface embedding
}
Type CONNECT Interface {
Connect ()
}
4. Interface Query
Query whether an object instance pointed to by one interface implements another interface
Query whether an object instance pointed to by an interface is of a certain type
The interface query uses the Ok-pattern mode to determine
Type A Interface {
Read ()
Write ()
}
Type B Interface {
Read ()
}
Type File struct {
Name string
}
Func (f *file) Read () {
Fmt. Println (F.name, "Read")
}
Func (f *file) Write () {
Fmt. Println (F.name, "Write")
}
var a = &file{"Hi"}
var b b
If V, OK: = A. (B); OK {//query whether the object instance pointed to by the A interface implements the B interface
Fmt. Println ("A has implement B interface")
b = V
}
If _, OK: = A. (*file); OK {//query whether the object instance pointed to by the A interface is a *file type
Fmt. Println ("*file has implement A interface")
}
5. Type Query
In the go language, you can directly query the type of object instances that the interface points to
Use the Switch-case mode to determine
For example:
var v1 interface{} = ...
Switch V: = v1. (type) {
Case int://Judgment Type
...//V is the value of V1
Case string:
Default
...
}
PS: The empty interface is often used in conjunction with Type-switch
6. Any type---NULL interface
Null interface is very useful in the go language
Because it is an empty interface, any type implements an empty interface interface{},
So say interface{} is any type
Variable type
function parameter type
return value type
Using an empty interface allows for greater scalability of the program
An empty interface is typically used in conjunction with Type-switch
Go language oriented Programming