golang面向对象总结
erlang_team · · 16655 次点击 · · 开始浏览- type $name struct{
- property01 int
- property02 int
- }
Golang里面的方法和接口都是基于这里type *** struct创建的类型,这里其实可以理解为:
- class$name {
- public int property01;
- public int property02;
- }
类型就是类。
所以我们说是类型的某个方法,类型实现了某个接口。
类型是属性的集合,接口是方法的集合
函数的定义:func $funcName ( ) ( ){}
方法的定义:func ( )$funcName ( ) ( ){}
Func (成员变量 类型) funname(局部变量 类型,局部变量 类型) (返回值类型) {}
成员变量是通过type来定义的。
函数的参数列表是需要传递的局部变量。
继承:
当一个类型B的某个字段(匿名字段)的类型是另一个类型 A的时候,那么类型 A所拥有的全部字段都被隐式地引入了当前定义的这个类型B。这样就实现了继承。B类型的变量就可以调用A的所有属性和方法。也就是说A继承了B。
定义继承时,子类中一般都含有一个类型是父类的匿名字段。匿名字段就是用来实现继承的。
- package main
- import (
- "fmt"
- )
- type Animal struct {
- Name string
- Age int
- }
- func (ani *Animal) GetAge() int {
- return ani.Age
- }
- type Dog struct {
- Animal //Animal匿名字段
- }
- func main() {
- dog := Dog{Animal{"dog", 10}}
- fmt.Println(dog.Age)
- fmt.Println(dog.GetAge())
- }
方法的重写
如果一个类型B实现了作为其属性的类型A中的方法。那么这个类型B的值调用方法的时候调用的是自己类型B的方法,而不是属性类型A的方法。
代码如下:
- package main
- import (
- "fmt"
- )
- type Animal struct {
- Name string
- Age int
- }
- func (ani *Animal) GetAge() int {
- return ani.Age
- }
- type Dog struct {
- Animal //Animal匿名字段
- }
- func (ani Dog) GetAge() int {
- return ani.Age + 1
- }
- func main() {
- dog := Dog{Animal{"dog", 10}}
- fmt.Println(dog.Age)
- fmt.Println(dog.GetAge())
- }
接口
1 接口
1)定义接口类型
定义接口,接口中可以有未实现的方法。
- type Animal interface {
- GetAge() int
- }
1)实现接口类型
如果某个类型实现了接口的所有方法。则这个类型实现了这个接口。
- type Animal interface {
- GetAge() int
- }
- type Dog struct {
- Name string
- Age int
- }
//实现GetAge()方法则实现了Animal接口
- func (ani Dog) GetAge() int {
- return ani.Age
- }
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
- type $name struct{
- property01 int
- property02 int
- }
Golang里面的方法和接口都是基于这里type *** struct创建的类型,这里其实可以理解为:
- class$name {
- public int property01;
- public int property02;
- }
类型就是类。
所以我们说是类型的某个方法,类型实现了某个接口。
类型是属性的集合,接口是方法的集合
函数的定义:func $funcName ( ) ( ){}
方法的定义:func ( )$funcName ( ) ( ){}
Func (成员变量 类型) funname(局部变量 类型,局部变量 类型) (返回值类型) {}
成员变量是通过type来定义的。
函数的参数列表是需要传递的局部变量。
继承:
当一个类型B的某个字段(匿名字段)的类型是另一个类型 A的时候,那么类型 A所拥有的全部字段都被隐式地引入了当前定义的这个类型B。这样就实现了继承。B类型的变量就可以调用A的所有属性和方法。也就是说A继承了B。
定义继承时,子类中一般都含有一个类型是父类的匿名字段。匿名字段就是用来实现继承的。
- package main
- import (
- "fmt"
- )
- type Animal struct {
- Name string
- Age int
- }
- func (ani *Animal) GetAge() int {
- return ani.Age
- }
- type Dog struct {
- Animal //Animal匿名字段
- }
- func main() {
- dog := Dog{Animal{"dog", 10}}
- fmt.Println(dog.Age)
- fmt.Println(dog.GetAge())
- }
方法的重写
如果一个类型B实现了作为其属性的类型A中的方法。那么这个类型B的值调用方法的时候调用的是自己类型B的方法,而不是属性类型A的方法。
代码如下:
- package main
- import (
- "fmt"
- )
- type Animal struct {
- Name string
- Age int
- }
- func (ani *Animal) GetAge() int {
- return ani.Age
- }
- type Dog struct {
- Animal //Animal匿名字段
- }
- func (ani Dog) GetAge() int {
- return ani.Age + 1
- }
- func main() {
- dog := Dog{Animal{"dog", 10}}
- fmt.Println(dog.Age)
- fmt.Println(dog.GetAge())
- }
接口
1 接口
1)定义接口类型
定义接口,接口中可以有未实现的方法。
- type Animal interface {
- GetAge() int
- }
1)实现接口类型
如果某个类型实现了接口的所有方法。则这个类型实现了这个接口。
- type Animal interface {
- GetAge() int
- }
- type Dog struct {
- Name string
- Age int
- }
//实现GetAge()方法则实现了Animal接口
- func (ani Dog) GetAge() int {
- return ani.Age
- }