On the interface and implementation classes of Go language (II.)

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

Extended

The previous article talked about implementing all the methods in an interface.

If you implement more than one interface, it is also very simple, the same way that his method to implement the line.

Background:

There is an animal interface, he has the action of eating and running, suddenly a bird animal, yes, it can fly.

If we modify the interface animal plus the action of flying, this will let the dog also fly. So, we put the methods of the interface out separately.

Package Mainimport ("FMT")//Fly interface type IFly interface {fly ()}//eating interface type IEat interface {Eat ()}//run interface type Irun interface {Ru N ()}//the implementation class of the dog type, "dog" struct {name String}func (dog *dog) Eat () {fmt. Println (Dog.name + "Eat")}func (dog *dog) Run () {fmt. Println (Dog.name + "Run")}//Bird Implementation class type Bird struct {name String}func (Bird *bird) Fly () {fmt. Println (Bird.name + "Fly")}func (bird *bird) Eat () {fmt. Println (Bird.name + "eat")}func main () {var eat IEat = &dog{"Dog"}eat. Eat () Eat = &bird{"Bird"}eat. Eat () var run irun = &dog{"Dog"}run. Run () var fly IFly = &bird{"Bird"}fly. Fly ()}
Run results



Summarize:

Golang implements the interface as long as the implementation class implements all the methods in the interface, we call the implementation class to implement the interface.



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.