首页 > 其他分享 >go学习 day207 继承

go学习 day207 继承

时间:2023-03-11 14:33:20浏览次数:38  
标签:student pu 继承 day207 fmt Println func go Name

编写一个学生考试系统

package main

import (
	"fmt"
)

// 编写一个学生考试系统
type student struct {
	Name string
	Age int
	Score int
}

// 将 Pupil 和 Graduate 共有的方法也绑定到 *student
func (s *student) Showinfo()  {
	fmt.Printf("学生的姓名=%v 年龄=%v 成绩=%v",s.Name,s.Age,s.Score)
}
func (s *student) Setscore(score int)  {
	s.Score = score
}
// 给 student 新增加一个方法,那么 pupil 和 graduate 都可以使用该方法
func (s *student) Getsum(n1 int,n2 int) int {
	return n1+n2
}



// 小学生
type Pupil struct {
	student 			// 嵌入student匿名结构体
}
func (p *Pupil) testing()  {
	fmt.Println("小学生正在考试中.......")
}


// 大学生
type Graduate struct {
	student 			// 嵌入student匿名结构体
}
func (p *Graduate) testing()  {
	fmt.Println("大学生正在考试中.......")
}





func main() {
	// 小学生
	pu := Pupil{}
	pu.student.Name = "tom~~"
	pu.student.Age = 12
	pu.testing()
	pu.student.Setscore(98)
	pu.student.Showinfo()
	fmt.Println()
	fmt.Println("小学生的res=",pu.student.Getsum(23,42))


	// 大学生
	fmt.Println()
	big := Graduate{}
	big.student.Name = "marrt"
	big.student.Age = 26
	big.testing()
	big.student.Setscore(88)
	big.student.Showinfo()
	fmt.Println()
	fmt.Println("大学生的res=",big.student.Getsum(12,32))
}


需要注意:

1)简写

点击查看代码
package main

import "fmt"

type A struct {
	Name string
	age int
}

func (a *A) Sayok()  {
	fmt.Println("A Sayok里的",a.Name)
}
func (a *A) Hello()  {
	fmt.Println("A Hello里的",a.Name)
}

type b struct {
	A
}



func main() {
	// 原来写法
	var c b
	c.A.Name = "tom"
	c.A.age = 23
	c.A.Hello()
	c.A.Sayok()

	// 简写
	var d b
	d.Name = "marry"
	d.age = 33
	d.Sayok()
	d.Hello()

}

2)就近原则

点击查看代码


标签:student,pu,继承,day207,fmt,Println,func,go,Name
From: https://www.cnblogs.com/chenlifan/p/17206001.html

相关文章

  • 为什么 Go 语言 struct 要使用 tags
    原文链接:为什么Go语言struct要使用tags在Go语言中,struct是一种常见的数据类型,它可以用来表示复杂的数据结构。在struct中,我们可以定义多个字段,每个字段可以有不......
  • ChIP-seq 分析:GO 功能测试与 Motifs 分析(12)
    动动发财的小手,点个赞吧!1.包加载我们可以使用rGREAT包中提供的GREATBioconductor接口。library(rGREAT)2.GO和功能测试要提交作业,我们可以使用Myc峰的GRan......
  • kernel logo到开机动画之间闪现黑屏(android 5.X)
    在BootAnimation开始绘图之前,会先做一次clearscreen的动作,避免出现前面的图干扰到BootAnimation的显示。通过checkmain_log先确认播放开机动画是哪个function,在对应functi......
  • 1.Django与Ajax
    AJAX简介(异步提交局部刷新)AJAX(AsynchronousJavascriptAndXML)翻译成中文就是“异步的Javascript和XML”。通过使用Javascript语言与服务器进行异步交互,传输的数据为X......
  • Polygon POJ - 1179
       除了维护一个区间最大值,还要一个最小值,(有负数)  #include<iostream>#include<algorithm>#include<cstring>usingnamespacestd;constintN=160......
  • [Go语言Web03]GORM数据库操作
    1.GORM连接MySQL1.1ORM1.1.1什么是ORM1.1.2ORM优缺点优点:提高开发效率缺点:牺牲执行性能牺牲灵活性弱化SQL能力1.2GROM官方文档执行下面命令安装GRO......
  • [Go语言Web02]Gin框架Web开发
    1.Gin框架初识1.1Gin的安装直接参考官方的文档1.1.1安装过程下载并安装gin:$goget-ugithub.com/gin-gonic/gin将gin引入到代码中:import"github.com/......
  • go 安装go环境
    目录go安装go环境mac安装go环境安装包下载配置修改go安装go环境mac安装go环境安装包下载https://golang.google.cn下载后傻瓜式安装,一直点下一步,直至安装结束配置......
  • 【质因数分解算法详解】C/Java/Go/Python/JS/Dart/Swift/Rust等不同语言实现
    关于质因数分解算法的不同语言实现,通过实例来看不同语言的差异什么是质因数算法?即任意一个合数可以分解为多个质数相乘。例如:20=2*2*545=3*3*5210=2*......
  • is的使用 私有属性 私有方法 单继承 多继承 多态
                              多态需要子类重写父类中的方法才会发生 ......