In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the object-oriented example analysis in GO language, which is very detailed and has certain reference value. Friends who are interested must finish it!
Object-oriented in GO language
Actually, GO is not a pure object-oriented programming language. It does not provide the keyword class, only the struct type.
In java or C #, struct cannot have member functions. However, struct in the Go language can have "member functions". Methods can be added to the structure, similar to the implementation of a class.
Personally, I think the object-oriented Go language is actually simpler and easier to understand.
Anyone who has studied java or C # should know that there are three basic characteristics of object orientation: encapsulation, inheritance, and polymorphism. I will not elaborate on their definition here. Let's take a direct look at how object-oriented is implemented in the go language.
1. Package characteristic
The mechanism that Golang distinguishes between public and private attributes is whether the method or property is capitalized, if the uppercase method is public, and if the first letter is lowercase, it is private.
Package main
Import "fmt"
Type Person struct {name string}
Func (person * Person) setName (name string) {person.name = name}
Func (person * Person) GetInfo () {fmt.Println (person.name)}
Func main () {p: = Person {"zhangsan"} p.setName ("lisi") p.GetInfo ()} 2. Inherit property
The inheritance method of the GO language is anonymous combination: the Woman structure contains the anonymous field Person, so the properties in Person also belong to the Woman object.
Package main
Import "fmt"
Type Person struct {name string}
Type Woman struct {Person sex string}
Func main () {woman: = Woman {Person {"wangwu"}, "female"} fmt.Println (woman.name) fmt.Println (woman.sex)} 3. Polymorphic characteristic package main
Import "fmt"
Type Eater interface {Eat ()}
Type Man struct {}
Type Woman struct {}
Func (man * Man) Eat () {fmt.Println ("Man Eat")}
Func (woman * Woman) Eat () {fmt.Println ("Woman Eat")}
Func main () {var e Eater woman: = Woman {} man: = Man {}
E = & woman e.Eat ()
E = & man e.Eat ()} above is all the content of the article "object-oriented sample Analysis in GO language". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.