go怎么实现封装继承多态

题目来源:好未来

答案1:

封装
封装就是把抽象出的字段和字段的操作封装在一起,数据被保护在内部,程序的其他包只有通过被授权的操作(方法)才能对字段进行操作。
实现如下面代码所示,需要注意的是,在golang内,除了slice、map、channel和显示的指针类型属于引用类型外,其它类型都属于值类型。

  • 引用类型作为函数入参传递时,函数对参数的修改会影响到原始调用对象的值;
  • 值类型作为函数入参传递时,函数体内会生成调用对象的拷贝,所以修改不会影响原始调用对象。所以在下面GetName中,接收器使用 this *Person 指针对象定义。当传递的是小对象,且不需要更改调用对象时,使用值类型做为接收器;大对象或者需要更改调用对象时使用指针类型作为接收器。
  1. type Person struct {
  2. name string
  3. age int
  4. }
  5. func NewPerson() Person {
  6. return Person{}
  7. }
  8. func (this *Person) SetName(name string) {
  9. this.name = name
  10. }
  11. func (this *Person) GetName() string {
  12. return this.name
  13. }
  14. func (this *Person) SetAge(age int) {
  15. this.age = age
  16. }
  17. func (this *Person) GetAge() string {
  18. return this.age
  19. }
  20. func main() {
  21. p := NewPerson()
  22. p.SetName("xiaofei")
  23. fmt.Println(p.GetName())
  24. }

继承
当多个结构体存在相同的属性(字段)和方法时,可以从这些结构体中抽象出一个基结构体A,在A中定义这些相同的属性和方法。其他的结构体不需要重新定义这些属性和方法,只需嵌套一个匿名结构体A即可。
在golang中,如果一个struct嵌套了另一个匿名结构体,那么这个结构体可以直接访问匿名结构体的字段和方法,从而实现继承特性。
同时,一个struct还可以嵌套多个匿名结构体,那么该struct可以直接访问嵌套的匿名结构体的字段和方法,从而实现多重继承。
代码如下:

  1. type Student struct {
  2. Person
  3. StuId int
  4. }
  5. func (this *Student) SetId(id int) {
  6. this.StuId = id
  7. }
  8. func (this *Student) GetId() int {
  9. return this.StuId
  10. }
  11. func main() {
  12. stu := oop.Student{}
  13. stu.SetName("xiaofei") // 可以直接访问Person的Set、Get方法
  14. stu.SetAge(22)
  15. stu.SetId(123)
  16. fmt.Printf("I am a student,My name is %s, my age is %d, my id is %d", stu.GetName(), stu.GetAge(), stu.GetId)
  17. }

多态
基类指针可以指向任何派生类的对象,并在运行时绑定最终调用的方法的过程被称为多态。多态是运行时特性,而继承则是编译时特性,也就是说继承关系在编译时就已经确定了,而多态则可以实现运行时的动态绑定。

  1. // 小狗和小鸟都是动物,都会移动和叫,它们共同的方法就可以提炼出来定义为一个抽象的接口。
  2. type Animal interface {
  3. Move()
  4. Shout()
  5. }
  6. type Dog struct {
  7. }
  8. func (dog Dog) Move() {
  9. fmt.Println("I am dog, I moved by 4 legs.")
  10. }
  11. func (dog Dog) Shout() {
  12. fmt.Println("WANG WANG WANG")
  13. }
  14. type Bird struct {
  15. }
  16. func (bird Bird) Move() {
  17. fmt.Println("I am bird, I fly with 2 wings")
  18. }
  19. func (bird Bird) Shout() {
  20. fmt.Println("ji ji ji ")
  21. }
  22. type ShowAnimal struct {
  23. }
  24. func (s ShowAnimal) Show(animal Animal) {
  25. animal.Move()
  26. animal.Shout()
  27. }
  28. func main() {
  29. show := ShowAnimal{}
  30. dog := Dog{}
  31. bird := Bird{}
  32. show.Show(dog)
  33. show.Show(bird)
  34. }