当前位置:网站首页>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 .
边栏推荐
- 联发科技2023届提前批IC笔试(题目)
- C language self-made Games: Sanzi (tic tac toe chess) intelligent chess supplement
- Literature reading_ Research on the usefulness identification of tourism online comments based on semantic fusion of multimodal data (Chinese Literature)
- Flutter monitors volume to realize waveform visualization of audio
- 1111 online map (30 points)
- 1106 lowest price in supply chain (25 points)
- 1119 pre- and post order traversals (30 points)
- Unity tool Luban learning notes 1
- sql语句模糊查询遇到的问题
- The current market situation and development prospect of the global gluten tolerance test kit industry in 2022
猜你喜欢

Prepare for 2022 and welcome the "golden three silver four". The "summary of Android intermediate and advanced interview questions in 2022" is fresh, so that your big factory interview can go smoothly

Keepalived热备与HAProxy

并发操作-内存交互操作

Celebrate the new year together

Thesis reading_ Chinese medical model_ eHealth
![[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

Learn to use the idea breakpoint debugging tool
![[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)

leetcode435. Non overlapping interval

Do you know UVs in modeling?
随机推荐
并发操作-内存交互操作
RT thread flow notes I startup, schedule, thread
[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
Shuttle + alluxio accelerated memory shuffle take-off
JDBC database operation
论文阅读_中文医疗模型_ eHealth
文献阅读_基于多模态数据语义融合的旅游在线评论有用性识别研究(中文文献)
Notes | numpy-08 Advanced index
Objects. Requirenonnull method description
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
Retirement plan fails, 64 year old programmer starts work again
Market status and development prospect prediction of the global fire hose industry in 2022
[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree
[research materials] 2022q1 game preferred casual game distribution circular - Download attached
The process of browser accessing the website
Analysis of proxy usage of ES6 new feature
Uipath practice (08) - selector
Shuttle + Alluxio 加速内存Shuffle起飞
Dynamic programming - related concepts, (tower problem)
cookie session jwt