Golang - Object Oriented Inheritance (Anonymous Field), Pointer Type Anonymous Field, Multiple Inheritance

Object-oriented:

  • Process-oriented: A process-centric programming idea that emphasizes that steps, processes, and each step is self-fulfilling
  • Object-oriented: An object-centric programming idea that emphasizes the removal of steps, processes, and specific functions through the command of objects

Summary: If you want to eat, do it yourself is process-oriented, and take-out is object-oriented. We don't care how the specific food is done. We can simplify the complex problems by using Meituan outside.

Object-oriented features:

  • encapsulation
  • inherit
  • polymorphic

Class:

Class is a general term for a series of transactions, and similar transactions must have the same characteristics

  • Class is the abstraction of a class of things with common attributes and behaviors in real life
  • A class is the data type of an object, and a class is a collection of objects with the same properties and behaviors
  • Classes are described by attributes and methods
  • Class is a description of real things

Composition of the class:

Attributes: refers to the characteristics of things, such as: mobile phone things (brand, price, size)
Behavior: refers to the actions that things can perform, such as: mobile phone things (phone calls, text messages)

Relationships between classes and objects:

Class: Class is the abstraction of a class of things in real life that have common attributes and behaviors
Object: Is an entity that can see the real existence of the touch
Simple understanding: class is the description of the object, object is the entity of the class

Inheritance:

  • Inheritance is a relationship between classes that describes how a class obtains member information from another class, where the parent provides member information and the child obtains member information
  • Inherit the attributes and behavior of the parent class so that the child class object can directly have the same attributes and behavior as the parent class. Subclasses have direct access to non-private properties and behaviors in the parent class.
  • Inheritance is the precondition of polymorphism. If there is no inheritance, there will be no polymorphism.
  • There is no inheritance syntax similar to other languages in Golang, but the concept of inheritance is implemented using anonymous fields

Benefits:

1. Enhance code reuse (the same code does not require duplicate writes, only needs to be extracted into the parent class, and subclasses can be used directly)
2. Enhance code maintainability

Disadvantages:

  • Coupling (correlation between code and code is coupling)
  • Reduce code flexibility (subclasses are constrained by inheriting non-private properties and methods from which they must have a parent)

Proximity principle:

Access to a variable in the subclass method is based on the proximity principle, first see if you have it or not, and then look for your dad.

  • This is the whole process:
    Subclass Local Range Finding
    Subclass Member Scope Finding
    Parent Member Scope Find
    If you don't go to your father's father

Use scenarios:

With the same (common) content between classes, you can use inheritance to optimize your code.
For example, the iPhone is a mobile phone brand, huawei is a mobile phone brand, Samsung is also a mobile phone brand, but there is a common feature is the mobile phone, so you can inherit the mobile phone class

Inheritance Format:

Add the name of the parent class to the class structure

Inheritance Initialization Format:

// Initialize All
 Variable Name := Subclass name{Parent name{Value of parent member 1,Value of parent member 2},Value of subclass member 1,Value of subclass member 2}

// Partial Initialization
 Variable Name := Subclass name{Parent name:Parent name{Parent Member:value},Subclass Members:value}

Get the value of the member:

// Get members of subclasses
 Subclass name.Subclass Members

// Members of a parent class can be obtained in either format. The first way is to go to the subclass to find members first, and the subclass to find the parent if it is not.
Subclass name.Parent Member
 Subclass name.Parent Class Name.Parent Member

Modify member values:

Subclass variable name.member name = Member Value

Demonstration:

func main() {
	//Initialize All
	stu := Student{Person{001, "itzhuzhu", 24}, 100}
	//Partial Initialization
	stu2 := Student{Person: Person{id: 001}, score: 100}
	stu.age = 23
	fmt.Println(stu)
	fmt.Println(stu2)
	fmt.Println(stu2.score)
	fmt.Println(stu2.Person.id)
}

/*
	Extract common attributes into a class and inherit them as needed
 **/
type Person struct {
	id   int
	name string
	age  int
}

type Student struct {
	Person // Anonymous field, only type has no name
	score  float64
}

type Teacher struct {
	Person
	salary float64
}

Pointer type anonymous field:

Pointer type is to replace anonymous field, format is to add *

Demonstration:

func main() {
	//Initialize All
	stu := Student{&Person{001, "itzhuzhu", 24}, 100}
	//Partial Initialization
	stu2 := Student{Person: &Person{id: 001}, score: 100}
	stu.age = 23
	fmt.Println(stu)
	fmt.Println(stu2)
	fmt.Println(stu2.score)
	fmt.Println(stu2.Person.id)
}


type Person struct {
	id   int
	name string
	age  int
}

type Student struct {
	*Person
	score float64
}

type Teacher struct {
	*Person
	salary float64
}

Multiple Inheritance:

After multiple inheritance, subclasses can be used directly. Parent member of parent class
Multiple inheritance is the same as java, but all of it is initialized here. It doesn't seem like a good idea to try it out. Just record it first, and only specify initialization

Demonstration:

func main() {
	stu := Student{score: 100}
	stu.id = 001
	stu.name = "itzhuzhu"
	stu.age = 24
	fmt.Println(stu.score)
	fmt.Println(stu.Person.name)
	fmt.Println(stu.Person.age)
	fmt.Println(stu.Person.Object.id)
}

type Object struct {
	id int
}
type Person struct {
	Object
	name string
	age  int
}

type Student struct {
	Person
	score float64
}

type Teacher struct {
	Person
	salary float64
}

Keywords: Go OOP

Added by richardbotw on Thu, 03 Feb 2022 19:37:46 +0200