golang 实现面向对象

golang是否支持面向对象
答案是肯定的。golang官方虽然没有明确提出面向对象的概念,但是基于已有的语法设计,我们可以实现面向对象的编程设计。
我们可以借助 structinterface来实现。

表示一个对象
structclass

type Program struct{
    name string
    ver  float32
}
program := Program{"golang",1.13}

此结构体表示了一个编程语言类,用面向对象来看, namever就是成员变量。有了成员变量,那么又该如何表示 成员方法 呢,来看一下代码:

func (program *Program) showProgramInfo(){
   fmt.Printf("this languge is %s , ver is %d", languge.name,languge.ver)
}

func (program *Program) setVer(ver float32){
   languge.ver = ver
}

program := Program{"golang",1.13}
program.showLangugeInfo()
program.setVer(1.14)
fmt.Println(program)

在函数名之前使用小括号,传入声明的结构体指针、变量名,也可以不用指针,写成(languge Languge) ,但是这样就无法覆盖实参来改变结构体内部的值,所以一般都用指针。这样 showLangugeInfo() , setVer() 实际上就是面向对象的 成员方法了

继承
在面向对象的语言中,一般都是用关键字 extends来表示继承的,golang中没有这种明确的继承方法,但实际来表示继承非常简单,不需要额外关键字。

type Application struct{
   Program
   platform string
   port string
}

func (app *Application) showProgramInfo(){
   fmt.Println("this is a app ...")
}

func (app *Application) writeLog(){
   fmt.Println("golang app write log ...")
}

app := Application{
    Program{"golang",1.13},
    "linux",
    "8080"
}
app.showProgramInfo()
app.setVer()
app.writeLog()

在声明 Application struct 时,使用 Program 作为匿名变量,相当于 Application 继承自 Program,Application 继承了 Program 的成员变量和成员方法,可以重写继承的方法,也可以新增成员方法。

多态

package main

import "fmt"

type Languge interface{
   sayHello(program string)
}

type Golang struct{
}

type PHP struct{
}

func(golang Golang) sayHello(program string) {
    fmt.Printf("say hello %s\n",program)
} 

func(php PHP) sayHello(program string) {
    fmt.Printf("say hello %s\n",program)
} 

func main() {
   var golang Languge
   var php Languge
   
   golang = Golang {}
   php = PHP {}
   
   golang.sayHello("golang")
   php.sayHello("php") 
}

作为对比,可以这样写

package main

import (
	"fmt"
)

type Languge interface{
	sayHello()
}

type Golang struct{
}

type PHP struct{
}

func(golang Golang) sayHello() {
	fmt.Println("say hello golang")
}

func(php PHP) sayHello() {
	fmt.Println("say hello php")
}

func langugeSayHello(languge Languge){
	languge.sayHello()
}

func main(){
	var languge Languge

	languge = Golang {}
	langugeSayHello(languge)
	//say hello golang

	languge = PHP {}
	langugeSayHello(languge)
	//say hello php
}

可以看到,不同的结构体实现了同一个接口,调用时会根据具体的结构体对象,调用了不同的具体实现方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值