go 面向对象继承
package main
import "fmt"
type Person struct {
name string
age int
}
type Student struct {
Person
school string
}
func main() {
p1 := Person{name: "zhangsna", age: 19}
fmt.Println(p1)
s1 := Student{Person: Person{
"lisi",
30,
}, school: "清华"}
fmt.Println(s1)
var s2 Student
s2.Person.name = "王五"
s2.Person.age = 32
s2.school = "北大"
fmt.Println(s2)
var s3 Student
s3.name = "赵六"
s3.age = 23
s3.school = "科技大学"
fmt.Println(s3)
}
go 面向对象中的方法
package main
import "fmt"
type Dog struct {
name string
age int
}
func (dog Dog) eat() {
fmt.Println("Dog has func eat ")
}
type Cat struct {
name string
age int
}
func (cat Cat) eat() {
fmt.Println("Cat has func eat ")
}
func (cat Cat) sleep() {
fmt.Println("Cat has func sleep ")
}
func main() {
dog := Dog{"金毛狗🐶", 30}
dog.eat()
cat := Cat{"布偶猫🐱", 21}
cat.eat()
cat.sleep()
}
go 面向对象中的方法。【高阶使用】方法继承/重写
package main
import "fmt"
type Animal struct {
name string
age int
}
func (a Animal) sleep() {
fmt.Println(a.name, "正在睡觉。。。")
}
func (a Animal) eat() {
fmt.Println(a.name, "正在吃饭。。。")
}
type Dog1 struct {
Animal
}
type Dog2 struct {
Animal
color string
}
func (dog Dog1) bite() {
fmt.Println(dog.name, "正在咬人。。。")
}
type Cat1 struct {
Animal
color string
}
func (cat Cat1) eat() {
fmt.Println("Cat1 重写 父类的 eat 方法:", cat.name, "正在吃猫粮")
}
func main() {
golden_retriever := Dog1{Animal{name: "小金毛", age: 3}}
ragdoll := Cat1{Animal: Animal{name: "布偶", age: 3}, color: "奶白色"}
golden_retriever.eat()
golden_retriever.sleep()
fmt.Println("子类使用自己的方法:")
golden_retriever.bite()
fmt.Println("子类使用父类的属性:", ragdoll.name, ragdoll.age)
fmt.Println("子类使用子类的属性:", ragdoll.color)
}
go 面向对象中的多态
- go 中的多态,需要 go 的interface 接口来实现
package main
import "fmt"
type AnimalInterface interface {
eat()
sleep()
}
type DogInterfaceStruct struct {
name string
}
type CatInterfaceStruct struct {
name string
}
func (d DogInterfaceStruct) eat() {
fmt.Println("多态狗狗🐶:", d.name, "正在吃")
}
func (d DogInterfaceStruct) sleep() {
fmt.Println("多态狗狗🐶:", d.name, "正在睡觉")
}
func (c CatInterfaceStruct) eat() {
fmt.Println("多态骚猫🐱:", c.name, "正在吃")
}
func (c CatInterfaceStruct) sleep() {
fmt.Println("多态骚猫🐱:", c.name, "正在睡觉")
}
func realizePolymorphism(animal AnimalInterface) {
animal.eat()
animal.eat()
}
func main() {
cat := CatInterfaceStruct{name: "布偶猫"}
cat.eat()
cat.sleep()
dog := DogInterfaceStruct{name: "小鸡毛"}
dog.eat()
dog.sleep()
fmt.Println("------")
var animal AnimalInterface
animal = dog
realizePolymorphism(animal)
}
小结 go 和 python对比面向对象
go 中的面向对象
struct : `类的定义 / 类中的类属性 / 对象属性`
- 结构体继承 : python类继承,继承不同的类的属性
- 定义字段: python中类的实例化对象的 `属性`
struct func: `类中的 类的实例化`
- 结构体实现方法: python中类实例化对象的 方法
interface : `类中的方法/对象方法`
- 接口是一堆方法的集合: python中类定义一大堆类方法或者对象方法
interface func `类的初始化,或者多态类的初始化`
- 接口实现方法,传入一个接口对象。 python中类的实例化基类的实例化
python中的面向对象
object 原类
__init__ 属性
- 方法
- 静态方法
- 类方法
- 对象方法
- 属性
- 类属性
- 对象属性
- 魔方函数。即带下划线的函数
- 封装
- 多态
- 继承
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2019-07-02 Python进阶(十二)----re模块