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

顺徳网站建设公司有哪些搜索优化推广公司

顺徳网站建设公司有哪些,搜索优化推广公司,百度分享wordpress插件下载,电子商务网站开发岗位202. 变量概述与简介 想要用好变量不是一件简单的事情,因为变量需要命名。 我们可以从两个角度去看待一个变量,第一个角度是变量的功能,第二个是变量的可读性。 变量的功能其实非常简单,变量可以存储一个值,这个值是…

202. 变量概述与简介

想要用好变量不是一件简单的事情,因为变量需要命名。

我们可以从两个角度去看待一个变量,第一个角度是变量的功能,第二个是变量的可读性。

变量的功能其实非常简单,变量可以存储一个值,这个值是特定类型的。

比如我们在上一篇写的 var text_to_print = “Hello World”。

text_to_print 这个变量存储了 “Hello World” 这个值。

“Hello World” 这个值的类型是文本类型,在编程语言中,文本类型的专业叫法叫做字符串类型,变量除了有字符串类型还有整数类型、实数类型、逻辑真假类型和自定义类型。

变量的功能非常简单,但是与编程中的其他概念配合后会变得很复杂,数量也会变得非常多,这样就会在一名开发者几个月后重新查看代码或者将代码转交给别人时,变量的可读性就变得尤为重要。

而变量的可读性主要体现在变量的命名上,而主要的难度也在变量的命名上,如何做到变量的名字在几个月后还是一目了然是编程中时时刻刻要思考的问题。

好了,大概介绍了下变量的功能和难点,接下来介绍一些 GDScript 的变量的内容。

首先,是什么让 text_to_print 成为了一个变量?

答案很简单,就是 var 关键字:

var text_to_print = "Hello World"

如果没有 var 关键字,那么 text_to_print 就不是变量了,具体是什么,笔者也不知道,应该是啥也不是了。

var 是 variable 的缩写,variable 就是变量的意思,百度翻译解释如下:

image-20231002164656496

变量就是用来改变的,就像我们的输出十次 Hello World 的例子一样:

extends Node# Called when the node enters the scene tree for the first time.
func _ready():var text_to_print = "Hello World"print(text_to_print)print(text_to_print)print(text_to_print)print(text_to_print)print(text_to_print)print(text_to_print)print(text_to_print)print(text_to_print)print(text_to_print)print(text_to_print)pass # Replace with function body.# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):pass

如果我们想要将输出十次 Hello World 改成输出十次 Hello GDScript,那么我们只需要改动变量的值即可:

extends Node# Called when the node enters the scene tree for the first time.
func _ready():var text_to_print = "Hello GDScript"print(text_to_print)print(text_to_print)print(text_to_print)print(text_to_print)print(text_to_print)print(text_to_print)print(text_to_print)print(text_to_print)print(text_to_print)print(text_to_print)pass # Replace with function body.# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):pass

这非常符合变量是可以改变的定义。

好,回到正题,var 关键字规定了谁是变量。

那么是什么规定了变量的类型呢?

答案就是等号后边的值,即:“Hello GDScript”

var text_to_print = "Hello GDScript"

Godot 的代码编辑器会自动检测等号后边的值是什么类型,在这里是字符串类型,即 string 类型。

我们当然也可以给 text_to_print 指定类型,代码如下:

var text_to_print : String = "Hello GDScript"

如果我们指定别的类型,代码编辑器会报错,比如:

image-20231002165235795

比如笔者这里制定类型为 int,但是接受的值为 String 类型,那么就会报错:

Cannot assign a value of type “String” as “int”,翻译成人话就是不能把一个 String 的值设置给一个 int 类型的变量。

这也是笔者想要介绍的,即一个变量制定好了一个类型,之后就不能存储其他类型的值了,当然大部分编程语言都是这样规定的。

大家到这里会发现,等号不是等于的意思,而是赋值的意思,var text_to_print = “Hello World” 意思是 把 “Hello World” 这个值赋予给 text_to_print 变量,或者可以理解成把 “Hello World” 这个值存储到 text_to_print 变量里。

如图所示:

image-20231002171543254

在我们定义并赋值完变量之后,再次修改或者访问就不用再写一遍 var 关键字了,代码如下:

extends Node# Called when the node enters the scene tree for the first time.
func _ready():var text_to_print = "Hello GDScript"print(text_to_print)print(text_to_print)print(text_to_print)print(text_to_print)print(text_to_print)text_to_print = "Hello Godot"print(text_to_print)print(text_to_print)print(text_to_print)print(text_to_print)print(text_to_print)pass # Replace with function body.# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):pass

运行之后结果如下:

image-20231002171821595

好了,这篇就是关于变量的一个简单介绍,这一篇内容就到这里,我们下一篇再见,拜拜。

知识地图

image-20231002172112342

更多内容

更新期间半价,保持 60% 的内容免费更新到此平台
版权所有 GamePix 独立游戏学院
转载请注明凉鞋的笔记

http://www.khdw.cn/news/63180.html

相关文章:

  • yum wordpress西安seo推广优化
  • 做网站买岩棉今日刚刚发生的国际新闻
  • github做网站速度快吗市场营销案例分析及解答
  • 校园网站建设 德育百度 站长工具
  • 如何维护网站建设百度推广话术全流程
  • 昆明官网seo服务南昌seo建站
  • 中国十大含金量证书厦门关键词优化平台
  • 网站建设公司网站建设专业品牌浙江疫情最新消息
  • 济南哪家公司做网站好网站维护费用
  • 昆山专业做网站独立站seo
  • 公司用dw做网站吗怎么弄属于自己的网站
  • 郑州 做网站seo教学网站
  • 怎么做网站排名会更好免费的外链网站
  • wordpress网页移动自己做seo网站推广
  • 自建博客网站属于b2b的网站有哪些
  • 做网站时候如果添加微信代码建立网站用什么软件
  • 广州企业网络营销全网推广某一网站seo策划方案
  • h5是什么网站上面做的知乎关键词排名优化工具
  • 做素材网站服务器宠物美容师宠物美容培训学校
  • 郑州定制网站网站搭建平台都有哪些
  • 如何做网站编辑seo综合诊断工具
  • 网站 设计风格优化关键词排名seo
  • 星巴克vi设计分析关于进一步优化当前疫情防控措施
  • 做网站需要会写代码6网站服务器软件
  • 深圳办公室租金多少钱一平网站seo优化的目的
  • 洛阳 网站建设百度统计工具
  • 中企动力双语网站网站数据分析案例
  • 建好网站是不是还得维护seo专业培训班
  • 福州疫情最新消息aso优化是什么
  • 做网站前台需要什么技能搜索关键词排行榜