Go Language Learning (v)-Object-oriented programming

Source: Internet
Author: User
Tags uppercase letter what interface

The main point is "Class" and interface & and other traditional language is not the same place a lot of, intermittent look for several days

Here's my practice code.

GoStudy0219 Project Main.go/*go Language Learning-object-oriented programming (1) Definition and initialization of a type's value semantics and reference semantic struct (class) in Go */package mainimport ("FMT") func main () { The initialization of several "classes" v1: = &character{"Tom", 100, 10, 100}//This defines the V1 actually when the "class" Address V2: = new (character)//equivalent to v4:=&character{ }v2.name = "Judy" v2.hp = 50v2.magic = 150v2.attack = 30v3: = &character{name: "Jim", hp:150}//Note is curly brace v3.magic = 30v3. Attack = 20fmt. Println (v1) fmt. Println (v2) fmt. Println (v3) fmt. Println ("*********************")//Use the class's function v1.addhp (. fmt). Println (v1) fmt. Println (V1.hpislessthan (*V2)) fmt. Println (v2) fmt. Println ("*********************")//other type var v5 age = 100fmt. Println (V5) V5.add (FMT). Println (V5) V5.erroradd (FMT). Println (V5) fmt. Println ("*********************")//value semantics and reference semantics//basic data type int,float such as value semantics//arrays and slices are values semantics//map,channel and interface are reference semantics}type Age IntFunc (This *age) add (target age) {*this + target}func (this age) Erroradd (target age) {this + = target}//struct (in other languages The same class status) type character struct {name stringhp float32attack float32magic Float32}func (This charActer) Hpislessthan (target character) bool {//this character does not change thistarget.attack = 66return this.hp < Target.hp}func (This *character) ADDHP (value float32) {//this *character will change thisthis.hp + = value}

GoStudy0223 Project Main.go/*go Language Learning-object-oriented programming (2) anonymous combination of accessibility */package mainimport ("FMT") func main () {FMT. Println ("Hello world!") var stu *student = &student{1001, *&person{"Tom", ten, True}}fmt. Println (Stu) Stu.greet ()//anonymous combination, can directly invoke the method of the anonymous function FMT. PRINTLN (Stu.name)///anonymous combination can directly invoke the properties of the anonymous function stu.walk ()//The implementation of the polymorphic overload var tea *teacher = &teacher{55, "Judy", *&pers on{"Tom", ten, False}}fmt. Println (tea.name)}type person struct {name Stringage Intisman bool}func (this *person) greet () {fmt. Println ("Hello")}func (this *person) walk () {fmt. PRINTLN ("Walk Slow")}type student struct {studentid Intperson//anonymous combination}func (this *student) walk () {//= polymorphic overload, external call walk not Then execute the walk function in the anonymous function this.person.walk ()///According to the requirements, implement the "base class" function, FMT. Println ("Walk quick")}type teacher struct {teacherid intname string//person inside name equivalent to be hidden, external call teacher.name, Perso The name in n does not work person}/* the anonymous combination is equivalent to the name of the member variable with the type name (minus the package name part), so avoid this situation if you have an error in a type that is similar to person and Xxx.person, so be aware of this *//* An uppercase letter indicates an out-of-package accessible, lowercase first letter indicating that the package cannot be accessed. In the same package, regardless of the first lettercan be accessed in uppercase or lowercase */ 

GoStudy0224 Project Main.go/*go Language Learning-object-oriented Programming (3) interface */package mainimport ("FMT") func main () {FMT. Println ("Hello world!") var per = &person{"Tom", 50}fmt. Println (per)///assignment of the interface//assignment with the object var v1 iadd = per//This is actually the address of the type V1. ADD () fmt. Println (per)///Assignment var v2 iperson = Pervar V3 IWalk = v2//function "full" interface can be assigned as a function "less" interface, if the function is the same in several interfaces, then these interfaces can convert each other v3. Walk () If v4, OK: = v2. (Iread); OK {//detection, Interface V2 can be converted to Iread interface V4.read ()}if v5, OK: = v2. (*person); OK {//Detect, Interface v2 The object type pointed to is Personfmt.println (v5)}var v6 interface{} = per//interface{} is an any type, the null interface switch V7: = V6. (type) {//The detection interface points to the type of object case int:fmt. Println (V7, "int") default:fmt. Println (V7, "other")}var v8 IPerson2 = perv8. Read ()}type person struct {name Stringage Int}func (this *person) Add (value int) {This.age + = Value}func (this *person) W Alk () {fmt. Println ("Walk")}func (this *person) Speak () {fmt. Println ("Speak")}func (this *person) Read () {fmt. Println ("read")}//define interface type IWalk interface {Walk ()//define interface function, omit func keyword}type iread interface {read ()}type ispeak InterfAce {Speak ()}type Iadd Interface {ADD (value int)}type IPerson interface {Walk () Read () Speak ()}type IPerson2 Interface {//Connect The combination of the port is equivalent to the previous interface ispeakireadiwalk}/* the interface of the traditional language is a contract, the class that uses the interface must implement all methods of the interface, the same name interface under different namespaces is also treated as two different interfaces eg. C # Java needs to consider what interface to define, in which class to implement the interface go language, do not need to consider the above problem * *

Go Language Learning (v)-Object-oriented programming

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.