当前位置:网站首页>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 .
边栏推荐
- leetcode435. Non overlapping interval
- document. The problem of missing parameters of referer is solved
- Shell script -- condition judgment
- ZABBIX monitoring of lamp architecture (2): ZABBIX basic operation
- Force GCC to compile 32-bit programs on 64 bit platform
- [luatos sensor] 2 air pressure bmp180
- Use Sqlalchemy module to obtain the table name and field name of the existing table in the database
- Current market situation and development prospect forecast of the global fire boots industry in 2022
- [batch dos-cmd command - summary and summary] - CMD window setting and operation command - close CMD window and exit CMD environment (exit, exit /b, goto: EOF)
- Notes | numpy-10 Iterative array
猜你喜欢

Basic knowledge of reflection (detailed explanation)

Cross platform plug-in flutter for displaying local notifications_ local_ notifications

leetcode406. Rebuild the queue based on height
![[luatos sensor] 1 light sensing bh1750](/img/70/07f29e072c0b8630f92ec837fc12d5.jpg)
[luatos sensor] 1 light sensing bh1750

论文阅读_ICD编码_MSMN

The programmer resigned and was sentenced to 10 months for deleting the code. JD came home and said that it took 30000 to restore the database. Netizen: This is really a revenge
![[USACO 2009 Dec S]Music Notes](/img/e6/282a8820becdd24d63dcff1b81fcaf.jpg)
[USACO 2009 Dec S]Music Notes
![[tools run SQL blind note]](/img/c3/86db4568b221d2423914990a88eec2.png)
[tools run SQL blind note]

Mobile terminal - uniapp development record (public request encapsulation)
![[luatos sensor] 2 air pressure bmp180](/img/88/2a6caa5fec95e54e3fb09c74ba8ae6.jpg)
[luatos sensor] 2 air pressure bmp180
随机推荐
Class loading mechanism (detailed explanation of the whole process)
Learning record of arouter principle
论文阅读_清华ERNIE
leetcode452. Detonate the balloon with the minimum number of arrows
Preparation for school and professional cognition
Keepalived热备与HAProxy
Distinguish between releases and snapshots in nexus private library
String matching: find a substring in a string
Thesis reading_ Chinese medical model_ eHealth
最大连续子段和(动态规划,递归,递推)
Mobile terminal - uniapp development record (public request encapsulation)
Market status and development prospects of the global automatic tea picker industry in 2022
leetcode406. Rebuild the queue based on height
Market status and development prospect prediction of the near infrared sensor industry of the global Internet of things in 2022
On typescript and grammar
[research materials] annual report of China's pension market in 2021 - Download attached
1087 all roads lead to Rome (30 points)
Thesis reading_ Chinese NLP_ ELECTRA
Handler understands the record
1118 birds in forest (25 points)