一、面向对象程序设计思想
GoObject OrientedOO

1.1 对象

GoObject

1.2 对象的状态

\quad 每一个对象都具有状态,对象通常用数值来描述它的状态。

1.3 对象的操作

GoMethodMethodReceiver

1.4 面向对象编程的好处

  • 封装
  • 继承
  • 多态
二、Method 方法

2.1 Method 的基本定义

func (recv receiver_type)funcName(param1 param1Type, ...)(return1 returnType1, ...){
	......
}
type Rectangle struct { // 矩形对象
	width  int
	height int
}

func (rectangle Rectangle) area() int { // 函数area()是Rectangle对象的一个方法
	return rectangle.width * rectangle.height // 计算矩形面积
}

func main() {
	rect := Rectangle{3, 4}
	fmt.Println(rect.area()) // 12
}

2.2 多个 Method 可以同名

MethodMethodMethodReceiverReceiverReceiverReceiverMethodMethodMethod.
type Rectangle struct { // 矩形对象
	width  int
	height int
}

type Circle struct { // 圆形对象
	radius float32
}

func (rectangle Rectangle) area() int { // 函数area()是rectangle对象的一个方法
	return rectangle.width * rectangle.height // 计算矩形面积
}

func (circle Circle) area() float32 { // 函数area()是circle对象的一个方法
	return circle.radius * circle.radius * math.Pi // 计算圆形面积
}

func main() {
	rectangle := Rectangle{3, 4}
	circle := Circle{5}
	fmt.Println(rectangle.area()) // 12
	fmt.Println(circle.area()) // 78.53982
}

2.3 指针作为 Receiver

ReceiverReceiverReceiverReceiver
GothisC++JavaC#thisPythonselfthis
type Coordiante struct {
	x int
	y int
}

func (coordiante Coordiante) swap() {
	coordiante.x, coordiante.y = coordiante.y, coordiante.x
	// 值传递:普通类型作为Receiver,在方法swap()中对对象属性值的操作并不会改变原实例的值
	fmt.Println(coordiante.x, coordiante.y) // 8 6
}

func (coordiante *Coordiante) swap2() {
	coordiante.x, coordiante.y = coordiante.y, coordiante.x
	// 引用传递:指针类型作为Receiver,在方法swap()中对对象属性值的操作会直接影响到原实例的值
	fmt.Println(coordiante.x, coordiante.y) // 8 6
}

func main() {
	coordiante := Coordiante{6, 8}
	coordiante.swap()
	fmt.Println("值传递:", coordiante.x, coordiante.y) // 值传递: 6 8
	(&coordiante).swap2()
	fmt.Println("引用传递:", coordiante.x, coordiante.y) // 引用传递: 8 6
}

2.4 匿名 Receiver

type Object struct {
	id   int
	name string
}

func (Object) msgbox() {
	fmt.Println("This is a object!")
}

func (*Object) msgBox() {
	fmt.Println("This is a object!")
}

func main() {
	obj := Object{}
	ptr := &obj
	obj.msgbox()
	ptr.msgBox()
}

2.5 Method 的继承

GoMethodMethodStructMethod
type People struct {
	name  string
	phone string
}

type Teacher struct {
	People
	department string
}

type Student struct {
	People
	school string
}

func (people People) sayHi() {
	fmt.Printf("Hi, I'm %s, you can call me on %s.\n", people.name, people.phone)
}

func main() {
	teacher := Teacher{People{"张三", "010-11001"}, "Computer Science"}
	teacher.sayHi()
	student := Student{People{"李四", "010-22002"}, "Tsinghua University"}
	student.sayHi()
}
Hi, I'm 张三, you can call me on 010-11001.
Hi, I'm 李四, you can call me on 010-22002.

2.6 Method 的重写

StudentsayHi()MethodStudentMethod
type People struct {
	name  string
	phone string
}

type Teacher struct {
	People
	department string
}

type Student struct {
	People
	school string
}

func (people People) sayHi() {
	fmt.Printf("Hi, I'm %s, you can call me on %s.\n", people.name, people.phone)
}

func (student Student) sayHi() {
	fmt.Printf("Hi, I'm %s, I study in %s, call me on %s.\n", student.name, student.school, student.phone)
}

func main() {
	teacher := Teacher{People{"张三", "010-11001"}, "Computer Science"}
	teacher.sayHi()
	student := Student{People{"李四", "010-22002"}, "Tsinghua University"}
	student.sayHi()
}

输出:

Hi, I'm 张三, you can call me on 010-11001.
Hi, I'm 李四, I study in Tsinghua University, call me on 010-22002.