之后在代码中更改变量值的原因是什么? [英] What is the reason for changing the value of a variable later in the code?

查看:167
本文介绍了之后在代码中更改变量值的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我一直在观看有关代码的教程,我对改变变量有疑问。



我通过以下示例工作:



class Person {



var Name:String =Initial Name

var年龄:Int = 24

var其他:String =其他变量

var十进制:Double = 4.1399



init(){

self.sayCheese()

}



func sayCheese(){

print(Cheese)

}

} < br $>




var firstPerson = Person()



firstPerson.Name =琼斯

firstPerson.Age

firstPerson.Other

firstPerson.Decimal

firstPerson.Name






在代码末尾,我将.Name字符串更改为等于Jones,而不是Initial Nam E。当我可以返回并更改初始值时,在代码中稍后更改Name的字符串有什么意义?为什么有必要编写额外的代码行来更改名称,这不是一个令人困惑的问题吗?



对于这个问题的无知感到抱歉,但我不熟悉编码,这让我陷入困境!我确定这是有充分理由的!



非常感谢!



我尝试了什么:



我尝试调整变量并添加其他值但我仍然无法理解原因。

Hi All,

I've been watching tutorials on code and I have a question about changing variables.

I worked through the following example:

class Person {

var Name:String = "Initial Name"
var Age:Int = 24
var Other:String = "Other Variable"
var Decimal:Double = 4.1399

init () {
self.sayCheese()
}

func sayCheese() {
print("Cheese")
}
}


var firstPerson = Person()

firstPerson.Name = "Jones"
firstPerson.Age
firstPerson.Other
firstPerson.Decimal
firstPerson.Name



Towards the end of the code, I change the .Name string to equal "Jones", as opposed to "Initial Name". What is the point in changing the string of Name later in the code when I could go back and change the initial value? Why is it necessary to write an additional line of code to change a name and isn't this confusing?

Sorry for the ignorance of the question, but I'm new to coding and this has got me stuck! I'm sure there's a very good reason for it!

Thanks a lot!

What I have tried:

I've tried adjusting variables and adding additional values but I still can't understand the reason.

推荐答案

因为初始值适用于Person的每个实例,而不仅仅适用于您正在谈论的那个。

如果您将初始值更改为琼斯然后它会工作正常 - 直到你试图添加第二个人史密斯。

初始值是默认占位符,并作为设置的一部分创建人而不是个人的一部分。它不会创建任何与任何个人相关的内容,它只是为要记录的个人设置框架。

执行此操作时:

Because the initial values apply to every instance of a Person, not just to the one you are talking about.
If you change the initial value to "Jones" then it would work fine - until you tried to add a second person "Smith".
The initial value is a "default placeholder" and is created as part of the "setup" of a Person rather than as part of an individual human being. It doesn't create anything that relates to any individual, it just sets up the framework for an individual to be recorded in.
When you do this:
var firstPerson = Person()

您正在创建一个单独的,然后填写他的详细信息:

You are creating an indiviidual, and you then fill in his details:

var firstPerson = Person()
firstPerson.Name = "Jones"

您还可以创建一个不同的人:

You can also create a different person:

var firstPerson = Person()
firstPerson.Name = "Jones"
var secondPerson = Person()
firstPerson.Name = "Smith"

这两个名字彼此独立,正如你在现实世界中所期望的那样。 br />


如果通过创建新变量(firstPerson,secondPerson等)来指定名称,为什么还要放置.Name?为什么你不能写:



firstPerson =琼斯

secondPerson =史密斯







你可以......但是这个名字不会有任何相关的信息保存在人class:年龄,地址,JobTitle等等。

让我们暂时考虑一下汽车:这是一辆汽车,它是一辆Red Mercedes。这是我的车。在那里有一辆不同的汽车:蓝色福特。那是你的车。

他们两个不是同一个对象 - 如果你把你的手机放在你的汽车的手套箱里,你不希望在我的汽车的手套箱里找到它 - 但他们有很多与它们相关的信息:发动机尺寸,燃料类型,门数,型号,年龄,注册号,VIN号等......列表继续。要在计算机中表示这一点,你可以为每个属性分别设置变量,但是你需要为我的汽车提供几十个变量,而为你的汽车需要几十个变量 - 如果我们介绍那辆汽车,那么汽车,他的汽车,乔斯汽车,如果使代码非常笨拙。

所以我们创建一个汽车类,其中包含有关单个车辆的所有相关信息,并填写每辆车的信息,以保持所有一起(这被称为Car类的实例,将它与类的定义分开)

现在我们谈论我的汽车我们可以查看类实例,我们可以得到颜色,引擎尺寸,左手或右手驱动器,车轮尺寸,或者那个特定车辆在那一点我们需要的东西。

另一件事我们能做的就是玩它:我们不需要知道它是用哪种车来做它的事情。我的意思是变量的名称(在你的情况下 firstPerson )与它包含的类的实例无关 - 这是你的导师的东西稍后会提出,但因为变量不是实例而只是对它的引用,你可以将引用传递给一个函数,该函数不需要知道实例的来源,它只是处理任何实例。

目前可能还不清楚 - 而且我不想在这里踩太多你的导师脚趾 - 但如果我们回到汽车片刻,MyCar是一个变量,指的是一辆特定的车辆。但MyWifesCar是另一个变量,它指的是同一辆车,如ThisCar和OurCar - 个别名称不是对象本身,因为如果实际车辆发生某些变化(我可能将其驱动到树中)那么损坏就会反映出来在车辆中,无论我使用哪个变量来访问它:MyCar,ThisCar,OurCar,MyWifesCar都是指同一个车辆的实例。

你不能用单独的变量做到这一点! :笑:



这有意义吗?

Andthe two names are independant of each other, as you would expect in the real world.

If you assign names by creating a new variable (firstPerson, secondPerson etc.) why do you need to put .Name at all? Why can't you just write:

firstPerson = "Jones"
secondPerson = "Smith"



You could...but then the name wouldn't have any related info which is held in the Person class: Age, Address, JobTitle, or whatever.
Let's just think about cars for a moment: Here is a car, it's a Red Mercedes. It's My Car. Over there is a different car: a Blue Ford. That is Your Car.
They two are not the same object - if you put your mobile in the glove box of Your Car you don't expect to find it in the glove box of My Car - but they have a lot of info associated with them: engine size, fuel type, number of doors, model, age, registration number, VIN number, ... the list goes on. To represent that in a computer, you could just have separate variables for each attribute, but then you'd need a couple of dozen variables for My Car, and a separate couple of dozen for Your Car - and if we introduce That Car, This Car, His Car, Joes Car if makes the code very unwieldy.
So we create a Car class which holds all the related information about a single vehicle and fill in the information for each car to keep it all together (this is called an instance of the Car class to separate it from the definition of the class)
Now when we talk about My Car we can look at the class instance and we can get the Colour, engine size, left or right hand drive, wheel size, or whatever we need at that point for that one specific vehicle.
The other thing we can do is play around with it: we don't need to know whose car it is in order to do things with it. What I mean by that is that the name of the variable (in your case firstPerson) has nothing to do with the instance of the class that it contains - this is stuff your tutor will bring up later, but because the variable isn't the instance but just a reference to it you can pass the reference to a function which doesn't need to know where the instance came from, it just processes any instance.
That's probably not clear at the moment - and I don't want to tread on your tutors toes too much here - but if we go back to cars for a moment, MyCar is a variable which refers to a specific vehicle. But MyWifesCar is another variable which refers to the same vehicle, as is ThisCar, and OurCar - the individual names aren't the object itself because if something changes in the actual vehicle (I drive it into a tree perhaps) then the damage is reflected in the vehicle, regardless of which variable I use to access it: MyCar, ThisCar, OurCar, MyWifesCar all refer to the same instance of a vehicle.
You can't do that with separate variables! :laugh:

Does that make any sense?


这篇关于之后在代码中更改变量值的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆