当前位置:网站首页>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 .
边栏推荐
- 2022-02-12 daily clock in: problem fine brush
- @RequestMapping
- [research materials] 2021 China's game industry brand report - Download attached
- [research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
- Market status and development prospect prediction of the global fire extinguisher industry in 2022
- JQ style, element operation, effect, filtering method and transformation, event object
- Handler understands the record
- JDBC database operation
- M1 Pro install redis
- Market status and development prospect prediction of global fermentation acid industry in 2022
猜你喜欢

Thesis reading_ ICD code_ MSMN

Class loading mechanism (detailed explanation of the whole process)

Thesis reading_ Chinese NLP_ ELECTRA

Mobile terminal - uniapp development record (public request encapsulation)
![[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa](/img/a9/92059db74ccde03b84c69dfce35b37.jpg)
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa

On typescript and grammar

"Hands on deep learning" pytorch edition Chapter II exercise

Uipath practice (08) - selector

Basic knowledge of reflection (detailed explanation)

2022-02-12 daily clock in: problem fine brush
随机推荐
雇佣收银员(差分约束)
Notes | numpy-08 Advanced index
联发科技2023届提前批IC笔试(题目)
Current market situation and development prospect forecast of the global fire boots industry in 2022
文献阅读_基于多模态数据语义融合的旅游在线评论有用性识别研究(中文文献)
How to connect the network: Chapter 1 CSDN creation punch in
Learn to use the idea breakpoint debugging tool
I've seen a piece of code in the past. I don't know what I'm doing. I can review it when I have time
Market status and development prospect prediction of the global fire hose industry in 2022
What is UUID
Shuttle + Alluxio 加速内存Shuffle起飞
Market status and development prospect forecast of global button dropper industry in 2022
Market status and development prospect prediction of global fermented plant protein industry in 2022
Valentine's day limited withdrawal guide: for one in 200 million of you
Kept hot standby and haproxy
Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution
1111 online map (30 points)
Realize file download through the tag of < a > and customize the file name
Thesis reading_ ICD code_ MSMN
MySQL winter vacation self-study 2022 12 (3)