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

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

1. Introduction

an interface refers to an abstraction that an entity provides itself to the outside world (another entity) that separates external communication methods from internal operations so that they can be modified internally without affecting the way other entities interact with the outside world. (from Baidu Encyclopedia)

2. Interfaces for other languages

Interface for Java

Public interface Animal {//define interface    void Eat ();} The public class Dog  {//is the same as the method name in the interface, but does not implement the Animal interface void Eat () {}}public class Cat implements Animal {///implements the Animal interface, And the Eat method must exist  @Override   void Eat () {  }}
Interface for 3.Go languages

Package Mainimport ("FMT")//define an interface with Eat and Run two methods in the interface type Animal interface {Eat () run ()}//defines a dog entity class that implements the Animal interface type dog struct {}func (dog *dog) Eat () {fmt. Println ("I eat Bone")}func (dog *dog) Run () {fmt. Println ("I run very fast")}func Main () {//Call interface var a animala = &dog{}a.eat () A.run ()}
Operation Result:




Related Article

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.