Go语言面向对象笔记

news/2024/7/3 12:26:57 标签: 多态, golang

文章目录

继承

  1. 通过匿名字段来实现继承,使用结构体嵌套结构体
type person struct {
	name string
	age int
	sex string
}
//结构体嵌套结构体
type Student struct {
	//通过匿名字段实现继承操作
	person //结构体名称作为结构体成员
	id int
	score int
}

func main() {
	var stu Student
	stu.id=1
	stu.score=100
	stu.name="王哈"
	stu.age=18
	stu.sex="女"
	//stu.person.sex="男"
	fmt.Print(stu)
}
  1. 指针匿名继承

和上面的区别就在于,如果按照上面的写法,我们不写new 的一步操作,就会造成空指针异常,因为我们使用的person是一个空地址。所以需要将其实现new出来才能对属性字段操作。

type person1 struct {
	name string
	age int
	sex string
}
type student1 struct {
	*person1 //指针匿名字段
	id int
	score int
}
func main() {
	var stu student1
	stu.person1 = new(person1)
	stu.name="王哈"
	fmt.Print(stu.name)//王哈
}

  1. 多重继承
type TestA struct {
	age int 
}
type TestB struct {
	TestA
}
type TestC struct {
	TestB
}
func main() {
	var c TestC
	c.TestB.TestA.age=15
}
  1. 接口的继承
type interA interface {//子集

}
type interB interface {//超集
	interA
}

多态

和其他的oop的概念相同

  1. 使用接口实现多态
package main

import "fmt"

type personD struct {
	name string
	sex string
	age int
}

type studentD struct {
	personD
	score int
}
type teacherD struct {
	personD
	subject string
}

func (s *studentD) say(){
	fmt.Println("student")
}
func (t *teacherD) say(){
	fmt.Println("teacher")
}
type personer interface {
	say()
}
// 多态实现
//说白了就是通过接口,作为函数参数,去实现接口的统一处理
func sayHello(p personer)  {
	p.say()
}
func main() {
	var p personer
	p=&studentD{}
	sayHello(p)//student
	p=&teacherD{}
	sayHello(p)//teacher
}

http://www.niftyadmin.cn/n/784145.html

相关文章

java中的重写 Orerride

区分子类方法中重名的三种变量 继承中成员方法的访问特点; 在父子类的继承关系中,创建子类对象,访问成员方法的规则; 创建的对象是谁,就优先用谁,如果没有则向上找。 注意事项 无论是成员方法还是成员变量…

[Spark]-结构化流之用法篇(待重修)

4.用法 结构化流使用Datasets和DataFrames.从Spark2.0开始,Spark-SQL中的Datasets和DataFrames,就已经能很好表示静态(有界)数据,动态(无界)数据 4.1 数据源 结构化流提供了四种不中断数据源 file-system,kafka,socket.rate-source    4.1.1 socket 从一个socket连接中读取 …

java继承中构造方法的特点及super,this关键字的用法

继承中构造方法的特点 1.子类构造方法中有一个默认隐含的“super”调用,所以一定是先调用父类构造方法,后执行子类构造方法。 2.可以通过super 关键字在子类构造方法调用父类重载构造方法。 3.【重写】super 的父类构造调用,必须是子类构造方…

函数概述

先举一例&#xff1b; 这个场景用C语言函数怎么实现&#xff1f; 上代码 #include <stdio.h> #include <stdlib.h> int main() { chuMenQianzhunbei();zaiLuShang();jianMian();huiJia();system("pause");return 0; } void chuMenQianzhunbei(){pr…

Go语言异常处理笔记

文章目录1.error接口2. panic函数3. Defer延迟调用4. recover拦截错误1.error接口 Go语言里引入了一个关于错误处理的标准模式&#xff0c;也就是error接口&#xff0c;它是Go语言内建的接口类型. error可以返回的一般是小错误&#xff0c;这些错误不是致命的&#xff0c;换句…

python基础学习5-常用函数模块、操作数据库、发邮件、写日志、写excel

1 函数map和filter #map()和filter循环帮你调用函数的 1.1 函数map import osimport timedef makdir(dir_name): if not os.path.isdir(dir_name): os.mkdir(dir_name) return Truedir_names [android,ios,java,tomcat,python,php] res map(ma…

函数返回值和参数

代码&#xff1b; #include <stdio.h> #include <stdlib.h> void maiMi(){ // void 代表无返回值&#xff0c;不需要return&#xff0c;void代表返回值的类型是无类型&#xff0c;return要写&#xff0c;之不过return后面不加变量printf("去买米\n");r…

Go语言goroutine笔记

文章目录一、基本概念1. 进程和线程2. 并发和并行3. 线程和协程4. 互斥锁和读写锁5. 实践6. goRoutine之间的通信7.GoRoutine与Channel结合一、基本概念 在学习Goroutine编程思想之前&#xff0c;先来了解几个关键的概念&#xff1a; 1. 进程和线程 进程是程序在操作系统中的…