当前位置:网站首页>Go language interface learning notes Continued
Go language interface learning notes Continued
2022-07-03 05:03:00 【CorGi_ eight thousand four hundred and fifty-six】
Last time, I mentioned interface nesting , Empty interface, etc , This time I'll make up .
I just learned Go Language soon , I want to write my understanding of the interface , If there are mistakes or better understanding methods , Please correct me .
One 、 Multiple types / Interface
There is a one to many relationship between interface and type
1.1 Multi type and single interface
seeing the name of a thing one thinks of its function , An interface can implement multiple types , Call of duty is used here cod Let's give you an example :
// Define a Sony platform
type sony struct {
console string
}
// Define a Microsoft platform
type microsoft struct {
console string
}
// Define the interface of a game
type game interface {
cod()
}
//PS Platform cod
func (p sony) cod() {
fmt.Printf(" I am a %s The emperor of Western Europe \n", p.console)
}
//xbox Platform cod
func (x microsoft) cod() {
fmt.Printf(" I am a %s The emperor of Western Europe , I was acquired \n", x.console)
}Here I define SONY and Microsoft Two structural platforms to store their hosts , Defined a game Interface , There's a cod Methods .
And here are two ways , One is SONY Realization cod The interface of , One is Microsoft Realization cod The interface of .
So as to realize whether it is ps5 still Xbox Series X, It can play cod, It's just cod The platform is different :
func main() {
// Declare interface ,cod It's a FPS game
var FPS game
// Instantiate Sony ps5
PS5 := sony{
console: "ps5",
}
// Instantiate Microsoft xbox
XBOX := microsoft{
console: "Xbox Series X",
}
// Calling method
FPS = XBOX
FPS.cod()
FPS = PS5
FPS.cod()
}
1.2 Single type and multiple interfaces
Similar to the above , Direct example :
// Define a FPS Game interface
type FPS interface {
fps()
}
// Define a RPG Game interface
type RPG interface {
rpg()
}
// Define a Microsoft structure
type Microsoft struct {
console string
}
// Define a fps Methods ,xbox Now you can play cod
func (m Microsoft) fps() {
fmt.Printf("cod available now on %s\n", m.console)
}
// Define a rpg Methods ,xbox Now you can play the legend of Breaking Dawn
func (m Microsoft) rpg() {
fmt.Printf("Tales of Arise available now on %s\n", m.console)
}
func main() {
// Statement FPS Interface
var fps FPS
// Statement RPG Interface
var rpg RPG
// Instantiation Xbox host
xbox := Microsoft{
console: "xbox",
}
// call
fps = xbox
rpg = xbox
fps.fps()
rpg.rpg()
}The above realizes Microsoft xbox This structure implements two interfaces ,rpg and fps,xbox Can play cod, Can also play The legend of dawn .
adopt 1.2 Can lead to the next part , Interface nesting .
1.3 Interface nesting
seeing the name of a thing one thinks of its function , One interface is nested with another or more interfaces to create new interfaces , Modify the above code :
type allGames interface {
FPS
RPG
}
type FPS interface {
fps()
}
type RPG interface {
rpg()
}
type Microsoft struct {
console string
}
func (m Microsoft) fps() {
fmt.Printf("cod available now on %s\n", m.console)
}
func (m Microsoft) rpg() {
fmt.Printf("Tales of Arise available now on %s\n", m.console)
}
func main() {
var games allGames
xbox := Microsoft{
console: "xbox",
}
games = xbox
games = xbox
games.fps()
games.rpg()
}
Define another allGames The interface of , hold RPG and FPS Interface nesting .
Finally, just state allGames Can also call FPS and RPG
Two 、 Empty interface
The empty interface is different from the previous interface , It's a little special . An empty interface is an interface without a method defined .
Any type or variable can implement an empty interface , Empty interfaces can be of any type . for instance :
var x interface{}
func main() {
s := "hello world"
x = s
fmt.Println(x)
}An empty interface is defined here , stay main I will string s Assign a value to x Empty interface , At this time x It's a string type . Empty functions can be used as parameters of functions , It can also be used as map Value , image python Define a dictionary and use . Let's not give an example here .
summary :
An essay with a little idea suddenly , The above is just a little bit of my superficial understanding , If you show timidity, please correct !
This is a note with a lot of smuggled goods .
边栏推荐
- [set theory] binary relation (example of binary relation operation | example of inverse operation | example of composite operation | example of limiting operation | example of image operation)
- Preparation for school and professional cognition
- Notes | numpy-07 Slice and index
- The consumption of Internet of things users is only 76 cents, and the price has become the biggest obstacle to the promotion of 5g industrial interconnection
- 1115 counting nodes in a BST (30 points)
- M1 Pro install redis
- Dynamic programming - related concepts, (tower problem)
- 1099 build a binary search tree (30 points)
- 论文阅读_中文NLP_ELECTRA
- Caijing 365 stock internal reference: what's the mystery behind the good father-in-law paying back 50 million?
猜你喜欢

Use Sqlalchemy module to obtain the table name and field name of the existing table in the database

leetcode452. Detonate the balloon with the minimum number of arrows
![[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)](/img/2a/362f3b0491f721d89336d4f468c9dd.jpg)
[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)

The consumption of Internet of things users is only 76 cents, and the price has become the biggest obstacle to the promotion of 5g industrial interconnection

JQ style, element operation, effect, filtering method and transformation, event object

【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令-关闭cmd窗口、退出cmd环境(exit、exit /b、goto :eof)

leetcode406. Rebuild the queue based on height

How to connect the network: Chapter 2 (Part 1): a life cycle of TCP connection | CSDN creation punch in

leetcode435. Non overlapping interval

Learn to use the idea breakpoint debugging tool
随机推荐
First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT
Compile and decompile GCC common instructions
Notes | numpy-10 Iterative array
1118 birds in forest (25 points)
Realize file download through the tag of < a > and customize the file name
leetcode452. Detonate the balloon with the minimum number of arrows
Uipath practice (08) - selector
The process of browser accessing the website
JQ style, element operation, effect, filtering method and transformation, event object
112 stucked keyboard (20 points)
Current market situation and development prospect forecast of global UV sensitive resin 3D printer industry in 2022
Market status and development prospect prediction of the global fire hose industry in 2022
Online VR model display - 3D visual display solution
动态规划——相关概念,(数塔问题)
1095 cars on campus (30 points)
[backtrader source code analysis 5] rewrite several time number conversion functions in utils with Python
[SQL injection point] location and judgment of the injection point
Valentine's day limited withdrawal guide: for one in 200 million of you
Use Sqlalchemy module to obtain the table name and field name of the existing table in the database
Analysis of proxy usage of ES6 new feature