Golang Object-oriented

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

Inherited

Package Mainimport ("FMT") type people struct {name string age int weight Int}type Student struct {P Eople Specialty string}//Define Sayhi method on people Structfunc (P people) Sayhi () {fmt. PRINTLN (1)}func main () {p: = people{"Syy", 1, 2} s: = student{people{"Syy", 1, 2}, "Seecialty"} p.sayhi () S.S Ayhi ()}

The main function of the following method is to define a Sayhi method on the people structure.

Define Sayhi method on people Structfunc (P people) Sayhi () {fmt. PRINTLN (1)}


The people structure is defined, but student also has this method. Or it can be said that "people can say hello, students can certainly say hello."

So the Sayhi method of Strudent is inherited from people.


Replication


With the code above, we know the inheritance, and then we add the following code to the code.

Define Sayhi method on Student structfunc (S Student) Sayhi () {fmt. PRINTLN (2)}

The purpose of this code is to add a Sayhi method to the student structure.

By running, you can see that the output is 2 instead of the original 1

Therefore, the method of people Sayhi was sayhi by student method.

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.