当前位置:网站首页>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 .
边栏推荐
- Uipath practice (08) - selector
- 移动端——uniapp开发记录(公共请求request封装)
- Caijing 365 stock internal reference: what's the mystery behind the good father-in-law paying back 50 million?
- [backtrader source code analysis 5] rewrite several time number conversion functions in utils with Python
- 第十九届浙江省 I. Barbecue
- [research materials] 2022q1 game preferred casual game distribution circular - Download attached
- Thesis reading_ Chinese medical model_ eHealth
- document. The problem of missing parameters of referer is solved
- Review the old and know the new: Notes on Data Science
- How to connect the network: Chapter 1 CSDN creation punch in
猜你喜欢

联发科技2023届提前批IC笔试(题目)
![[research materials] annual report of China's pension market in 2021 - Download attached](/img/24/622aeeb38de16ac84128b362ceeddb.jpg)
[research materials] annual report of China's pension market in 2021 - Download attached

Handler understands the record

Preparation for school and professional cognition

论文阅读_中文NLP_ELECTRA

Valentine's day limited withdrawal guide: for one in 200 million of you

Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)

leetcode406. Rebuild the queue based on height

Promise

SSM framework integration
随机推荐
SSM framework integration
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
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
Unity tool Luban learning notes 1
How to connect the network: Chapter 2 (Part 1): a life cycle of TCP connection | CSDN creation punch in
"Hands on deep learning" pytorch edition Chapter II exercise
[SQL injection point] location and judgment of the injection point
Class loading mechanism (detailed explanation of the whole process)
[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology
Market status and development prospect prediction of the near infrared sensor industry of the global Internet of things in 2022
String matching: find a substring in a string
Market status and development prospects of the global IOT active infrared sensor industry in 2022
移动端——uniapp开发记录(公共请求request封装)
[SQL injection] joint query (the simplest injection method)
cookie session jwt
Notes | numpy-10 Iterative array
Blog building tool recommendation (text book delivery)
Do you know UVs in modeling?
Dynamic programming - related concepts, (tower problem)
Day 51 - tree problem