Object-oriented structure of the body 1

Golang object-oriented programming language description:

  1) Golang also supports object-oriented programming (OOP), but the traditional object-oriented programming and differentiated, not a pure object-oriented language. So we say Golang support object-oriented programming features is more accurate.

  2) Golang no class (class), structure of the Go language (struct) and other programming languages ​​class (class) have the same status, you can understand Golang is achieved OOP features based struct.

  3) Golang very simple object-oriented programming, OOP language removed tradition of method overloading, constructors and destructors, this pointer is hidden like.

  4) Golang still have inheritance, encapsulation and polymorphism characteristics of object-oriented programming, and the way to achieve just not the same as other OOP languages, such as inheritance: Golang no extends keyword, inheritance is achieved through an anonymous field.

  5) Golang object-oriented (OOP) is very elegant, is itself part of OOP language type system (type system) by an interface (interface) associated low coupling, is also very flexible. Back will fully appreciate this feature. That oriented programming interface in Goalng is a very important feature.

 

Cats look at a support question:

  Granny Zhang kept two cats: one name is white, 3 years old, white. Also called a floret, 100 years old this year, suit. Write a program, when the user enters the name of the cat, the cat will display the name, age, color. If the kitten wrong name entered by the user is displayed Granny Zhang not only cats.

Using existing technology to solve:

1) the definition of variables to solve alone

func main() {

  1. // process variable
  var cat1Name string = "white"
  var = int. 3 cat1Age
  var cat1Color String = "White"

  var cat2Name string = "小花"
  var cat1Age int = 100
  var cat1Color string = "花色"
}

2) use an array to solve

func main() {

  // array 2. solve
  var catNames [2] string = [ ..] string { " white", "flower"}
  var catAges [2] = int [..] {int. 3, 100}
  var catColers [2 ] string = [..] string { " white", "color"}
}

Disadvantages of the prior art outcomes analysis:

1) using a variable or array to solve the problem of cats, is not conducive to the management and maintenance of data. Because the name, age, color belong to a cat, but here are kept separate.

2) If we want a cat attributes (name, age, color) operation (binding method) not a good deal.

3) What leads me to explain the technology == "structure

 

Relationship between structure and the structure variables (instance / object) is a schematic view:

 

Description of the figure:

  1) The feature extracted from a class of things (such as cats), to form a new data type is a struct.

  2) With this structure, we can create a number of variables (example / object)

  3) things can be cats, can also be a Person, Fish or a utility class. . .

Getting started - an object-oriented (struct) to solve the problem of cats

package main
import (
  "fmt"
)

// define a Cat structure, Cat respective field / attribute information into the management Cat structure
type struct {Cat
  the Name String
  Age int
  Color String
  Hobby String
}


func main() {

  // Granny Zhang kept two cats: one name is white, 3 years old, white.
  // also called a floret, 100 years old this year, suit.
  // Write a program, when the user enters the name of the cat, the cat's name is displayed,
  // age, color. If the kitten wrong name entered by the user,
  // Granny Zhang is displayed not only cats.

  // Use struct to complete the case
  variables // Create a Cat's

  var cat1 Cat   // var a int
  cat1.Name = "小白"
  cat1.Age = 3
  cat1.Color = "白色"
  cat1.Hobby = "吃鱼"
  fmt.Println("cat1=", cat1)

  fmt.Println("猫猫的信息如下:")
  fmt.Println("name=", cat1.Name)
  fmt.Println("age=", cat1.Age)
  fmt.Println("color=", cat1.Color)
  fmt.Println("hobby=", cat1.Hobby)
}

Structures and structure variables (Example) and the difference between contact:

Through the front of the case and explain that we can see:

1) the structure is self-defined data type, represents a class of things

2) variable structure (instance) is a specific, practical, represents a particular variable


Variable structure (example) in memory layout:

 

Structure declaration and use of traps:

How to declare structure:

type structure name {struct
  field1 type
  Field2 type
}

For example:

type Student struct {
  Name string
  Age int
  Score float32
}

'Field / property

basic introduction:

  1) From the conceptual point of view is called or: structure = Field field = attribute (i.e. teaching, the unified called field)

  2) field is part of a structure, usually the basic data types, arrays, or a reference type. For example, we defined earlier Name cats structure string is property


Notes and detailed instructions:

  1) field with variable declaration syntax, example: Field Name field type

  2) type field may be: basic type, an array type or reference

  3) After you create a structure variable, if not assigned to a field corresponds to a value of zero (the default value), the same rule as I said before:

    Boolean is false, 0 is an integer, string is ""
    Default array type and its associated element type, such as score [3] int compared to [0, 0, 0]
    pointer, Slice, and map zero value is nil, that is, has not been allocated.

Case presentation:

package main
import (
  "fmt"
)

// If the field type structure is: pointer, slice, and map zero values are nil, that is, has not been allocated
// If you need to use this field, you need to make, can be used.

struct {the Person type
  the Name String
  Age int
  Scores [. 5] float64
  PTR pointer // int *
  slice [] int // slice
  map1 map [string] string // slice
}

func main() {

  // definition of a structure variable
  var the Person P1
  fmt.Println (P1)
  IF p1.ptr nil == {
    fmt.Println ( "OK1")
  }
  IF p1.slice nil == {
    fmt.Println ( "OK2")
  }
  nil p1.map1 == {IF
    fmt.Println ( "OK3")
  }

  // use Slice, Again, must be the make
  p1.slice = the make ([] int, 10)
  p1.slice [0] = 100

  // use the map, it must first the make
  p1.map1 = the make (Map [String] String)
  p1.map1 [ "key1"] = "Tom"
  fmt.Println (P1)
}

4) Different structure variable fields are independent of each other, change a variable field structure, does not affect the other.

type Monster struct {
  Name string
  Age int
}

func main() {

  monster1 Monster var
  monster1.Name = "Bull Demon"
  monster1.Age = 500

  monster2: = monster1 // structure is a value type, the default value for the copy
  monster2.Name = "Tinospora fine"

  fmt.Println("monster1=", monster1)
  fmt.Println("monster2=", monster2)
}

 

Guess you like

Origin www.cnblogs.com/green-frog-2019/p/11408032.html