当前位置:网站首页>方法的繼承和重寫
方法的繼承和重寫
2022-06-21 12:10:00 【attempt_to_do】
繼承
如果匿名字段實現了一個method,那麼包含這個匿名字段的struct也能調用該method。
package main
import "fmt"
type Human struct {
name string
age int
phone string
}
type Student struct {
Human //匿名字段
school string
}
type Employee struct {
Human //匿名字段
company string
}
//在human上面定義了一個method
func (h *Human) SayHi() {
fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone)
}
func main() {
mark := Student{
Human{
"Mark", 25, "222-222-YYYY"}, "MIT"}
sam := Employee{
Human{
"Sam", 45, "111-888-XXXX"}, "Golang Inc"}
mark.SayHi()
sam.SayHi()
}
重寫
如果Employee想要實現自己的SayHi,怎麼辦?簡單,和匿名字段沖突一樣的道理,我們可以在Employee上面定義一個method,重寫了匿名字段的方法。
package main
import "fmt"
type Human struct {
name string
age int
phone string
}
type Student struct {
Human //匿名字段
school string
}
type Employee struct {
Human //匿名字段
company string
}
//Human定義method
func (h *Human) SayHi() {
fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone)
}
//Employee的method重寫Human的method
func (e *Employee) SayHi() {
fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name,
e.company, e.phone) //Yes you can split into 2 lines here.
}
func main() {
mark := Student{
Human{
"Mark", 25, "222-222-YYYY"}, "MIT"}
sam := Employee{
Human{
"Sam", 45, "111-888-XXXX"}, "Golang Inc"}
mark.SayHi()
sam.SayHi()
}
边栏推荐
- [deep learning] use deep learning to monitor your girlfriend's wechat chat?
- WPF 使用 MAUI 的自绘制逻辑
- MySQL-DML
- STM32开发之 VS Code + GDB下载调试
- A Kuan food: the battle for "the first share of convenience food" continues
- TypeScript 变量声明 —— 类型断言(Type Assertion)
- Simulated 100 questions of 2022 safety officer-a certificate examination and online simulated examination
- 1108. IP 地址无效化
- [yolov5s target detection] opencv loads onnx model for reasoning on GPU
- 20n10-asemi medium and low voltage MOS tube 20n10
猜你喜欢

i.MX - RT1052时钟及锁相环(PLL)分析
![[Harbin Institute of technology] information sharing for the first and second examinations of postgraduate entrance examination](/img/06/df5a64441814c9ecfa2f039318496e.jpg)
[Harbin Institute of technology] information sharing for the first and second examinations of postgraduate entrance examination

1108. IP address invalidation

Formation harmonyos I

2-zabbix使用自动发现自动添加主机

SSD【目标检测篇】

南京大学 静态软件分析(static program analyzes)-- Intermediate Representation 学习笔记

Apache shardingsphere 5.1.2 release | new driving API + cloud native deployment to create a high-performance data gateway

Customization of power aging test system | overview of charging pile automatic test system nsat-8000

第八章 Web项目测试
随机推荐
WPF 使用 MAUI 的自绘制逻辑
harmonyos培訓一
广东发产品检测券,消费者也有份
Ansible 配置首次ssh免认证的操作说明
Heavyweight, mapstruct 1.5 was released. This time, it finally supports the transformation of map into bean!
2022 special operation certificate examination question bank and online simulation examination for safety management personnel of hazardous chemical business units
STL基本容器测试
i.MX - RT1052时钟及锁相环(PLL)分析
Anchor of SSD_ Box calculation
CPU、MPU、MCU、SoC、MCM介绍
知识点:PCB电路板的几种特殊布线方法
Factory mode implementation
一键打新债到底安不安全呀?是不是靠谱的?
Is pension insurance a financial product? What is the expected return?
knowing和understanding的区别
2-zabbix automatically add hosts using autodiscover
2022 safety officer-b certificate retraining question bank and simulated examination
巨头局终战:即时零售
The k-th small__
SSD的anchor_box计算