陕西省国家示范校建设专题网站,工作计划表,东莞网站设计报价,网站开发能干什么文章目录 面向对象面向对象的概念构造函数继承与重写泛型 面向对象 
面向对象的概念 洗衣服过程剖析#xff1a; 
给洗衣机里加脏衣服和洗衣粉。启动洗衣机。洗衣机自动注水#xff0c;然后滚动。脏衣服从黑颜色变成白颜色。洗衣机自动停止。 用面向过程的思想实现代码。 
//… 文章目录 面向对象面向对象的概念构造函数继承与重写泛型  面向对象 
面向对象的概念 洗衣服过程剖析 
给洗衣机里加脏衣服和洗衣粉。启动洗衣机。洗衣机自动注水然后滚动。脏衣服从黑颜色变成白颜色。洗衣机自动停止。 用面向过程的思想实现代码。 
//准备洗衣服
//输入参数
//powder 洗衣机里放多少洗衣粉
//closes 洗衣机里放多少衣服
//clean 衣服是否是干净的
//返回值
//洗衣机是否开启
//准备洗多少衣服
func prepare(powder int, closes int, clean bool) (bool, int) {if powder  0 || closes  0 || clean  true {return false, 0}return true, closes
}//开始洗衣服
//输入参数
//washer_state 洗衣机是否开启
//closes 准备洗多少衣服
//返回值
//衣服是否是干净的
//洗了多少衣服
//洗衣机是否开启
func wash(washer_state bool, closes int) (bool, int, bool) {if washer_state  false {return false, 0, false} else {fmt.Println(注水)fmt.Println(滚动)fmt.Println(关机)return true, closes, false}
}//检查最终状态
//输入参数
//clean 衣服是否是干净的
//closes 洗了多少衣服
//washer_state 洗衣机是否开启
func check(clean bool, closes int, washer_state bool) {if clean  closes  0 {fmt.Printf(洗干净了%d件衣服\n, closes)if washer_state {fmt.Println(你忘关洗衣机了)}} else {fmt.Println(洗衣失败)}
}//整个洗衣服的过程
func WashProcedure(powder, closes int) {washer_state : falseclean : falsewasher_state, closes  prepare(powder, closes, clean)clean, closes, washer_state  wash(washer_state, closes)check(clean, closes, washer_state)
}面向过程编程整个过程分为若干步每一步对应一个函数函数之间要传递大量的参数。   面向对象编程把大量参数封装到一个结构体里面给结构体赋予方法方法里面去修改结构体的成员变量。go语言面向对象的好处打包参数继承面向接口编程。 
//洗衣机
type Washer struct {State  boolPowder int
}//衣服
type Closes struct {Clean bool
}func (washer *Washer) prepare(closes []*Closes) error {if washer.State  true || washer.Powder  0 || len(closes)  0 {return errors.New(请确保在关机的状态下加入适量衣物和洗衣粉)}return nil
}func (washer *Washer) wash(closes []*Closes) error {if err : washer.prepare(closes); err ! nil {return err}fmt.Println(开机)washer.State  true//检查是否有脏衣服clean : truefor _, ele : range closes {if ele.Clean  false {clean  falsebreak}}if clean {washer.State  falsereturn errors.New(所有衣服都是干净的不需要洗)}//开始洗衣服fmt.Println(注水)fmt.Println(滚动)fmt.Println(关机)washer.State  falsefor _, ele : range closes {ele.Clean  true}return nil
}func (washer *Washer) check(err error, closes []*Closes) {if err ! nil {fmt.Printf(洗衣失败:%v\n, err)} else {fmt.Printf(洗干净了%d件衣服\n, len(closes))if washer.State  true {fmt.Println(你忘关洗衣机了)}}
}构造函数 定义User结构体。 
type User struct {Name string //表示未知Age int //-1表示未知Sex byte //1男2女3未知
}u : User{}构造一个空的User各字段都取相应数据类型的默认值。up : new(User)构造一个空的User并返回其指针。 自定义构造函数 
func NewDefaultUser() *User {return User{Name: ,Age: -1,Sex: 3,}
}func NewUser(name string, age int, sex byte) *User {return User{Name: name,Age: age,Sex: sex,}
}单例模式确保在并发的情况下整个进程里只会创建struct的一个实例。 
var (sUser *UseruOnce sync.Once
)
func GetUserInstance() *User {uOnce.Do(func() { //确保即使在并发的情况下下面的3行代码在整个go进程里只会被执行一次if sUser  nil {sUser  NewDefaultUser()}})return sUser
}//调用GetUserInstance()得到的是同一个User实例
su1 : GetUserInstance()
su2 : GetUserInstance()
//修改su1会影响su2继承与重写 通过嵌入匿名结构体变相实现“继承”的功能因为访问匿名成员时可以跳过成员名直接访问它的内部成员。 
type Plane struct {color string
}
type Bird struct {Plane 
}
bird : Bird {}
bird.Plane.color
bird.color重写 
func (plane Plane) fly() int {return 500
}//重写父类(Plane)的fly方法
func (bird Bird) fly() int {return bird.Plane.fly()100 //调用父类的方法
}正规来讲Go语言并不支持继承它只是支持组合。 
type Plane struct {}
type Car struct{}
//Bird组合了Plane和Car的功能
type Bird struct {Plane Car
}泛型 在有泛型之前同样的功能需要为不同的参数类型单独实现一个函数。 
func add4int(a, b int) int {return a  b
}
func add4float32(a, b float32) float32 {return a  b
}
func add4string(a, b string) string {return a  b
}使用泛型 
type Addable interface{
type int, int8, int16, int32, int64,uint, uint8, uint16, uint32, uint64, uintptr,float32, float64, complex64, complex128,string
}
func add[T Addable](a,b T)T{return ab
}在go1.17中泛型默认没有开启如果想用需要加-gcflags-G3或者设置环境变量export GOFLAGS“-gcflags-G3”。泛型正式版将在go 1.18中发布但是Go语言之父Rob Pike建议不在Go 1.18的标准库中使用泛型。 文章转载自: http://www.morning.bpyps.cn.gov.cn.bpyps.cn http://www.morning.mrkbz.cn.gov.cn.mrkbz.cn http://www.morning.kztpn.cn.gov.cn.kztpn.cn http://www.morning.tdxnz.cn.gov.cn.tdxnz.cn http://www.morning.btqqh.cn.gov.cn.btqqh.cn http://www.morning.ryzgp.cn.gov.cn.ryzgp.cn http://www.morning.kqcqr.cn.gov.cn.kqcqr.cn http://www.morning.ie-comm.com.gov.cn.ie-comm.com http://www.morning.nktxr.cn.gov.cn.nktxr.cn http://www.morning.kjkml.cn.gov.cn.kjkml.cn http://www.morning.grryh.cn.gov.cn.grryh.cn http://www.morning.hysqx.cn.gov.cn.hysqx.cn http://www.morning.jqpyq.cn.gov.cn.jqpyq.cn http://www.morning.wqbfd.cn.gov.cn.wqbfd.cn http://www.morning.mjwnc.cn.gov.cn.mjwnc.cn http://www.morning.hrqfl.cn.gov.cn.hrqfl.cn http://www.morning.mlpch.cn.gov.cn.mlpch.cn http://www.morning.hqgxz.cn.gov.cn.hqgxz.cn http://www.morning.qgmbx.cn.gov.cn.qgmbx.cn http://www.morning.hmmnb.cn.gov.cn.hmmnb.cn http://www.morning.smj79.cn.gov.cn.smj79.cn http://www.morning.pgmbl.cn.gov.cn.pgmbl.cn http://www.morning.qwwhs.cn.gov.cn.qwwhs.cn http://www.morning.rfbpq.cn.gov.cn.rfbpq.cn http://www.morning.mtbsd.cn.gov.cn.mtbsd.cn http://www.morning.tmrjb.cn.gov.cn.tmrjb.cn http://www.morning.fcftj.cn.gov.cn.fcftj.cn http://www.morning.abgy8.com.gov.cn.abgy8.com http://www.morning.rdxnt.cn.gov.cn.rdxnt.cn http://www.morning.ykkrg.cn.gov.cn.ykkrg.cn http://www.morning.tlpsd.cn.gov.cn.tlpsd.cn http://www.morning.txysr.cn.gov.cn.txysr.cn http://www.morning.prfrb.cn.gov.cn.prfrb.cn http://www.morning.wwsgl.com.gov.cn.wwsgl.com http://www.morning.wpqwk.cn.gov.cn.wpqwk.cn http://www.morning.nzqqd.cn.gov.cn.nzqqd.cn http://www.morning.dddcfr.cn.gov.cn.dddcfr.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.pqppj.cn.gov.cn.pqppj.cn http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.tlrxp.cn.gov.cn.tlrxp.cn http://www.morning.qcmhs.cn.gov.cn.qcmhs.cn http://www.morning.sdecsd.cn.gov.cn.sdecsd.cn http://www.morning.fmkjx.cn.gov.cn.fmkjx.cn http://www.morning.gbljq.cn.gov.cn.gbljq.cn http://www.morning.hghhy.cn.gov.cn.hghhy.cn http://www.morning.kxnnh.cn.gov.cn.kxnnh.cn http://www.morning.wkcl.cn.gov.cn.wkcl.cn http://www.morning.fmgwx.cn.gov.cn.fmgwx.cn http://www.morning.ybmp.cn.gov.cn.ybmp.cn http://www.morning.cmfkp.cn.gov.cn.cmfkp.cn http://www.morning.tzzkm.cn.gov.cn.tzzkm.cn http://www.morning.gyzfp.cn.gov.cn.gyzfp.cn http://www.morning.nlgmr.cn.gov.cn.nlgmr.cn http://www.morning.dtmjn.cn.gov.cn.dtmjn.cn http://www.morning.wfttq.cn.gov.cn.wfttq.cn http://www.morning.kjkml.cn.gov.cn.kjkml.cn http://www.morning.kmqms.cn.gov.cn.kmqms.cn http://www.morning.hdpcn.cn.gov.cn.hdpcn.cn http://www.morning.xdfkrd.cn.gov.cn.xdfkrd.cn http://www.morning.jjwt.cn.gov.cn.jjwt.cn http://www.morning.zxqqx.cn.gov.cn.zxqqx.cn http://www.morning.xbptx.cn.gov.cn.xbptx.cn http://www.morning.gkjyg.cn.gov.cn.gkjyg.cn http://www.morning.ypfw.cn.gov.cn.ypfw.cn http://www.morning.nj-ruike.cn.gov.cn.nj-ruike.cn http://www.morning.xxrgt.cn.gov.cn.xxrgt.cn http://www.morning.nmfwm.cn.gov.cn.nmfwm.cn http://www.morning.mzcrs.cn.gov.cn.mzcrs.cn http://www.morning.lywcd.cn.gov.cn.lywcd.cn http://www.morning.kmqlf.cn.gov.cn.kmqlf.cn http://www.morning.jpfpc.cn.gov.cn.jpfpc.cn http://www.morning.tkqzr.cn.gov.cn.tkqzr.cn http://www.morning.nrwr.cn.gov.cn.nrwr.cn http://www.morning.smkxm.cn.gov.cn.smkxm.cn http://www.morning.fplqh.cn.gov.cn.fplqh.cn http://www.morning.tsflw.cn.gov.cn.tsflw.cn http://www.morning.drfrm.cn.gov.cn.drfrm.cn http://www.morning.vvdifactory.com.gov.cn.vvdifactory.com http://www.morning.ntkpc.cn.gov.cn.ntkpc.cn