Go No data abstraction design

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Interface is the foundation of the abstract design of Golang, is the interface of the method collection, is a very powerful and canonical pointer, can refer to any of the methods that implement the interface of the set of structs, cannot define attributes, meaning that in the abstract design is not allowed to have data, Make language compilation run more pure and convenient.

All properties are Setter/getter

1234567891011121314151617181920212223242526272829303132333435363738394041424344
 PackageMaintypeSurfacestruct{Skinstring} func (S *surface) Skin() string {return "My skin is"+ S.skin}typeMenInterface{getsurface () Surface}typeEuropeanstruct{Surface Surface} func (P *european) getsurface() Surface {returnP.surface}typeAfricanstruct{Surface Surface} func (P *african) getsurface() Surface {returnP.surface} func introduce(  Men's Men){s: = men. Getsurface ()println(S.skin ())} func main() {vars Surface s = surface{"White"} e: = European{s} introduce (&e) s = surface{"BLACK"} A: = African{s} introduce (&a)}

The polymorphism of the above example is done by Setter/getter in disguise, and the property is defined in interface, and the attribute polymorphism is realized by the method polymorphism.

Parent class method A calls subclass Method B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
 PackageMaintypeMenInterface{Age ()int}typeParentstruct{Men//ImplicitM men//Explicit} func (P *parent) Age() int {return  the} func (P *parent) Display1() {println(P.men.age ())} func (P *parent) Display2() {println(P.m.age ())}typeChildstruct{Parent} func (c *child) Age() int {return  +} func main() {varChild *childvarParent *parent parent = &parent{parent,Nil} child = &child{parent: *parent}println("====parent") Child. Display1 () parent = &parent{child,Nil} child = &child{parent: *parent}println("====child") Child. Display1 () CHILD.M = parentprintln("====parent") Child. Display2 () CHILD.M = Childprintln("====child") Child. Display2 ()}

The previous example implements the B method of calling the parent class or subclass in a method of the parent class, similar to an abstract method. Because go is a static language, there is no powerful lookup mechanism for dynamic languages, and there are no auxiliary high-level polymorphism such as virtual functions (Virtural method) and abstract methods.

Fortunately, Golang has pointers to interface, AKA interfaces, to complete the flexible invocation of some polymorphic methods in the parent class by defining the properties of the interface type in the parent class and dynamically binding the real type of the property when instantiated, increasing the reusability of the public part.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.