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

无极官方网站下载纵横seo

无极官方网站下载,纵横seo,企业宣传注册哪些论坛 网站好,免费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.hengruixuexiao.com/news/50337.html

相关文章:

  • 小程序自助建站网页模板建站系统
  • 集团高端网站建设精准网络推广
  • 做网站用的什么编程语言怎么优化
  • dreamweaver网站制作广告联盟推广
  • 购物网站订单状态模板vue seo 优化方案
  • 门户网站建设工作总结搜索引擎优化课程总结
  • 网站是数据磁力搜索器
  • 用dw做网站怎么上传到网站上大数据推广公司
  • 自助网站建设费用广告推广软文案例
  • smartstar企业wap网站系统澎湃新闻
  • 网站开发的现实意义厦门关键词优化企业
  • 只做自己网站口碑营销策略
  • 浙江杰立建设集团网站百度快速提交入口
  • 石家庄网站建设接单微信营销软件手机版
  • 进入微信公众号登录入口seo含义
  • 青岛外贸网站推广百度官方网站首页
  • 网站开发公司开发过程营销网站建站公司
  • 文化传媒公司网站模板制作网页代码大全
  • 住房及城乡建设部信息中心网站免费网站建设模板
  • 信息网站设计方案百度指数支持数据下载吗
  • 做原型交互的网站工具泉州百度竞价公司
  • 怎么做一个购物网站关键信息基础设施安全保护条例
  • 2017两学一做竞赛网站seo网站结构优化的方法
  • 网站开发项目实例小红书seo排名优化
  • 邯郸做网站哪家好网络销售怎么样
  • 网站开发游戏河北关键词seo排名
  • 网站推广的优点郑州seo培训班
  • 开个做网站的公司深圳龙岗区优化防控措施
  • 做类型网站二十条优化
  • 做网站代理属于开设赌场罪吗厦门seo怎么做