zoukankan      html  css  js  c++  java
  • 81_Go基础_1_49 结构体别名

     1 package main
     2 
     3 import "fmt"
     4 
     5 type Person struct {
     6     name string
     7 }
     8 
     9 func (p Person) show() {
    10     fmt.Println("Person--->", p.name)
    11 }
    12 
    13 // 类型别名
    14 type People = Person
    15 
    16 func (p People) show2() {
    17     fmt.Println("People--->", p.name)
    18 }
    19 
    20 type Student struct {
    21     //嵌入两个结构体
    22     Person
    23     People
    24 }
    25 
    26 func main() {
    27     var s Student         //s.name = "王二狗" //ambiguous selector s.name
    28     s.Person.name = "王二狗" //s.show() //ambiguous selector s.show
    29     s.Person.show()       // Person---> 王二狗
    30 
    31     fmt.Printf("%T,%T\n", s.Person, s.People) //main.Person,main.Person
    32 
    33     s.People.name = "李小花"
    34     s.People.show2() // People---> 李小花
    35     s.Person.show()  // Person---> 王二狗
    36 }
  • 相关阅读:
    面试
    二叉树- 二叉树直径
    排序算法
    JAVA编程
    JAVA编程
    JAVA中break和continue的区别
    HTTP的序列化和反序列化
    PL/SQL基础
    G. Game Design
    hdu 6703 array
  • 原文地址:https://www.cnblogs.com/luwei0915/p/15633435.html
Copyright © 2011-2022 走看看