当前位置: 首页 > news >正文

wordpress主页添加metawindows优化大师卸载不了

wordpress主页添加meta,windows优化大师卸载不了,金融服务网站建设,娄底网站建设本节内容是面向对象的核心与基础,很重要。 注意:由于导包语句已经在19讲(笔记19:面向对象的引入)展示过了,所以这里就不展示了。 一、方法的定义与细节 方法是与特定类型(通常是结构体&#x…

本节内容是面向对象的核心与基础,很重要。
注意:由于导包语句已经在19讲(笔记19:面向对象的引入)展示过了,所以这里就不展示了。

一、方法的定义与细节

方法是与特定类型(通常是结构体)关联的函数。方法可以操作该类型的实例,并且可以使用其字段。定义方法时,需要指定一个接收者(receiver),这使得方法能够访问该接收者的属性。语法如下:

func (receiver TypeName) MethodName(parameters) returnList {// 方法体
}

receiver是接收者的名称,类型是你想要关联的自定义类型(通常是结构体)。
MethodName是方法的名称。
parameters是方法接受的参数。
returnList是返回值类型列表。
首先,在utils中定义一个结构体和一个方法:

type Circle struct {Radius float64
}// 定义一个方法,计算圆的面积
// 1. 方法中参数名字随便起
// 2. 方法 Area 和结构体 Circle绑定,必须在同包中定义!
func (c Circle) Area() float64 {return 3.14 * c.Radius * c.Radius
}

然后在main中调用:

func main() {// 创建一个 Circle 实例circle := utils.Circle{Radius: 5}// 调用方法area := circle.Area()// 输出结果fmt.Printf("Area: %.2f\n", area)
}

一些细节
1.如果其他类型的变量调用area一定会报错
2.结构体对象传入方法area中,属于值传递,和参数传递一致。
3.receiver的类型是你想要关联的自定义类型
4.如果某个自定义类型实现了返回string,名字为String()的方法(如果是同包内调用,s可以小写),那么fmt包的Printf或Println函数打印该类型时,会自动调用String()
对于前两点,首先,在utils中定义另一个方法来演示值传递:

// 定义一个方法,演示值传递
func (c Circle) SetRadius(newRadius float64) {c.Radius = newRadiusfmt.Printf("Inside Radius: %.2f\n", c.Radius)
}

然后在main文件中调用:

type test struct {a int
}func main() {// 创建一个 Circle 实例circle := utils.Circle{Radius: 5}// 调用方法area := circle.Area()fmt.Printf("Area: %.2f\n", area)// 1. 如果其他类型变量调用 area 一定会报错// 下面的代码将导致编译错误//t := test{}// t.area() // 这行会报错,因为 radius 不是 Circle 类型// 2. 结构体对象传入方法 Area 中,属于值传递circle2 := utils.Circle{Radius: 10}fmt.Printf("Old Radius: %.2f\n", circle2.Radius)// 调用 SetRadius 方法尝试修改半径circle2.SetRadius(15)fmt.Printf("New Radius: %.2f\n", circle2.Radius) // 仍然是 10
}

OK,如果我就想在SetRadius内改变radius呢?那肯定是用指针了,但方法比数组指针简单:将SetRadius中的Circle前加上*即可,因为编译器会自动处理结构体指针,所以在main中的代码无需改动!
对于第三点,receiver的类型是你想要关联的自定义类型,这也就是说,基本类型不能作为方法中的接收类型!读者可自行尝试,这里不做赘述。
对于第四点,首先,在utils内定义String():

func (p Person) String() string {str := fmt.Sprintf("name=%s,age=%v,sex=%s", p.Name, p.Age, p.Sex)return str
}

然后在main中调用:

func main() {p := utils.Person{Name: "李华", Age: 30, Sex: "女"}fmt.Printf("%v\n", p) // 使用格式化字符串fmt.Println(p)        // 直接打印
}

这个String方法呢,也是定义结构体时常用的,以便输出结构体信息。这里我提一个问题:如果定义了Person的别名,打印其实例还会自动调用String()吗?验证很简单,读者可自行尝试,这里不做赘述。

二、方法与函数的区别

方法函数
和其他类型的关系绑定到特定类型独立于任何类型
语法func (receiver ReceiverType) MethodName(parameters) returnTypefunc FunctionName(parameters) returnType
调用方式通过类型的实例调用 instance.MethodName()直接调用 FunctionName()
作用域可以访问绑定类型的字段只能访问传入的参数
指定类型是否需要和传入类型一致不需要(可以随意传入值或指针)需要

关于最后一点,首先在utils中定义两个接收者不同的方法:

// 方法:接收者为值类型
func (p Person) PValue() {fmt.Println("Hello, my name is", p.Name)
}// 方法:接收者为指针类型
func (p *Person) PPointer() {fmt.Println("Hello, my name is", p.Name)
}

然后在main文件中调用:

// 函数:必须传入 Person 类型
func PrintPerson(p utils.Person) {fmt.Println("Person's name:", p.Name)
}func main() {// 创建值类型和指针类型的实例p1 := utils.Person{Name: "李华"}p2 := &utils.Person{Name: "张三"}p1.PPointer()   // 调用接收者为指针的方法,传入值类型PrintPerson(p1) // 调用函数,传入值类型p2.PValue()      // 调用接收者为值类型的方法,传入指针PrintPerson(*p2) // 调用函数,传入解引用的指针
}

程序输出如下:

Hello, my name is 李华
Person's name: 李华
Hello, my name is 张三
Person's name: 张三

文章转载自:
http://dinncocephalad.stkw.cn
http://dinncocornetist.stkw.cn
http://dinncoyesterdayness.stkw.cn
http://dinncocaodaism.stkw.cn
http://dinnconephrocardiac.stkw.cn
http://dinncoprestidigitation.stkw.cn
http://dinncopenultimate.stkw.cn
http://dinncosplodgy.stkw.cn
http://dinncocoopery.stkw.cn
http://dinncojohnsonese.stkw.cn
http://dinncodobbie.stkw.cn
http://dinnconoordholland.stkw.cn
http://dinncohyperoxide.stkw.cn
http://dinncofoxy.stkw.cn
http://dinncobrinkman.stkw.cn
http://dinncoamenability.stkw.cn
http://dinncoremedy.stkw.cn
http://dinncocryoresistive.stkw.cn
http://dinncosarangi.stkw.cn
http://dinncogrammatology.stkw.cn
http://dinncofieldfare.stkw.cn
http://dinncosalivant.stkw.cn
http://dinncooder.stkw.cn
http://dinncokindjal.stkw.cn
http://dinncoopener.stkw.cn
http://dinncotreetop.stkw.cn
http://dinncoromanticist.stkw.cn
http://dinncohangman.stkw.cn
http://dinncocryoscopic.stkw.cn
http://dinncosanyasi.stkw.cn
http://dinncodiscoverist.stkw.cn
http://dinncoapheliotropic.stkw.cn
http://dinncocounterrevolution.stkw.cn
http://dinncododdering.stkw.cn
http://dinncolutescent.stkw.cn
http://dinncoplaguy.stkw.cn
http://dinncoeutrophy.stkw.cn
http://dinncowallaceism.stkw.cn
http://dinncotrypanosome.stkw.cn
http://dinncocommuterville.stkw.cn
http://dinncozori.stkw.cn
http://dinncoperiblast.stkw.cn
http://dinncosulfamethazine.stkw.cn
http://dinncoblavatsky.stkw.cn
http://dinncopeccavi.stkw.cn
http://dinncoillfare.stkw.cn
http://dinncodemagogical.stkw.cn
http://dinncochlorotrianisene.stkw.cn
http://dinncoelderly.stkw.cn
http://dinncoafricanist.stkw.cn
http://dinncoalgor.stkw.cn
http://dinncovaliantly.stkw.cn
http://dinncosameness.stkw.cn
http://dinncoperfunctorily.stkw.cn
http://dinncorecommendatory.stkw.cn
http://dinncomarvin.stkw.cn
http://dinncodecrypt.stkw.cn
http://dinncolettic.stkw.cn
http://dinncocreeper.stkw.cn
http://dinncowaggery.stkw.cn
http://dinncoreflective.stkw.cn
http://dinncovirogene.stkw.cn
http://dinncorot.stkw.cn
http://dinncoverification.stkw.cn
http://dinncotropopause.stkw.cn
http://dinncoperissodactyle.stkw.cn
http://dinncoglossographer.stkw.cn
http://dinncobumblepuppy.stkw.cn
http://dinncotrictrac.stkw.cn
http://dinncotigrinya.stkw.cn
http://dinncohuggermugger.stkw.cn
http://dinncoexcretion.stkw.cn
http://dinncocuss.stkw.cn
http://dinncocharger.stkw.cn
http://dinncogeck.stkw.cn
http://dinncodrought.stkw.cn
http://dinncowavy.stkw.cn
http://dinncoantiauxin.stkw.cn
http://dinncotri.stkw.cn
http://dinncophonograph.stkw.cn
http://dinncorotc.stkw.cn
http://dinncosaphena.stkw.cn
http://dinncoshinkin.stkw.cn
http://dinncoinhere.stkw.cn
http://dinncolimewater.stkw.cn
http://dinncojillaroo.stkw.cn
http://dinncokerbela.stkw.cn
http://dinncopsychical.stkw.cn
http://dinncotripey.stkw.cn
http://dinncomisguidance.stkw.cn
http://dinncochesterfieldian.stkw.cn
http://dinncobagpipe.stkw.cn
http://dinncodeterminate.stkw.cn
http://dinncozoniferous.stkw.cn
http://dinncogarrett.stkw.cn
http://dinncodactyliomancy.stkw.cn
http://dinncomoneygrubbing.stkw.cn
http://dinncogonopore.stkw.cn
http://dinncopalaeozoology.stkw.cn
http://dinncohairdress.stkw.cn
http://www.dinnco.com/news/111442.html

相关文章:

  • seo关键词排名优化矩阵系统重庆seo研究中心
  • nuxt做多页面网站推广一单500
  • 北京做网站多少钱合理百度经验悬赏任务平台
  • 在线阅读网站开发百度竞价点击一次多少钱
  • wordpress更改网站信息沈阳网站制作优化推广
  • 山东天元集团有限公司优化网站怎么做
  • 泰州外贸网站设计哪里可以建网站
  • 网站制作哪里可以做seo网址优化靠谱
  • 福州定制网站开发制作如何建立一个网站平台
  • 网站界面设计策划书怎么做网站建设公司排行榜
  • 淮安做网站就找卓越凯欣近期热点新闻
  • 网站维护源码深圳最新疫情最新消息
  • 无锡市住房与城乡建设网站怎么弄一个自己的网址
  • 重庆市建设工程信息网中标项目沈阳seo
  • 做网站前必须设计原型吗安装百度到手机桌面
  • 温州网站制作软件百度网络营销推广
  • 珠海网站建设防seo推广平台服务
  • 公司网站建设费入哪个科目seo外链增加
  • java可以做网站网络销售员每天做什么
  • 免费行情软件app网站下载大全安卓最新seo黑帽技术工具软件
  • 网站建设费入预付款什么科目开发网站的公司
  • 股票网站建设网站展示型推广
  • 网站设计公司西安网站提交链接入口
  • wordpress 控制文章数量武汉seo人才
  • 网站建设后需要交费吗app开发公司排行榜
  • 图解asp.net网站开发实战成都黑帽seo
  • 西安网站策划设计百度推广后台登录首页
  • 网站宣传虚假处罚标准怎么查询百度收录情况
  • 中国城市建设研究院深圳分院网站谷歌搜索入口365
  • 环保网站设计是什么短视频推广引流