封装对于清除变量或常量的类型有什么作用?

时间:2022-09-02 09:31:02

I'm currently reading through Apple's "Intro to App Development with Swift" student guide. At the end-quiz of chapter 9, "Types", it asks:

我现在正在阅读苹果公司的“应用程序开发简介”学生指南。在第九章“类型”的最后测验中,题目问:

When you're reading code and aren't sure of the type of a variable or constant, what's the quickest way to find out?

当您正在阅读代码并且不确定变量或常量的类型时,最快的方法是什么?

The answer is:

答案是:

Rewrite the section of code using different encapsulation.

使用不同的封装重写代码段。

However, the concept of encapsulation has not been introduced before so I'm rather confused. In the e-book's Glossary, it states:

但是,封装的概念以前没有介绍过,所以我很困惑。在电子书的词汇表中,它指出:

A language mechanism for restricting access to some of the object components and/or a language construct that facilitates the bundling of data with the methods of operating on that data.

一种语言机制,用于限制对某些对象组件的访问,以及/或一种语言结构,该结构方便将数据与操作该数据的方法捆绑在一起。

Can anyone explain how this relates to finding out about the type of a variable or constant?

有人能解释一下这和发现变量或常数的类型有什么关系吗?

Thanks in advance.

提前谢谢。

1 个解决方案

#1


1  

The answer is:

答案是:

Rewrite the section of code using different encapsulation.

使用不同的封装重写代码段。

That's a wrong answer. The right answer is "Use Xcode's [Quick Help] inspector."

这是一个错误的答案。正确的答案是“使用Xcode的[快速帮助]检查器”。

Can anyone explain how this relates to finding out about the type of a variable or constant?

有人能解释一下这和发现变量或常数的类型有什么关系吗?

The suggestion here is that if you rewrite the source to make a private variable public (i.e. change the way it is encapsulated) it would make it easier to find the type of the variable in question.

这里的建议是,如果您重写源代码以使一个私有变量成为公共的(即改变封装的方式),就可以更容易地找到所涉及的变量的类型。

#1


1  

The answer is:

答案是:

Rewrite the section of code using different encapsulation.

使用不同的封装重写代码段。

That's a wrong answer. The right answer is "Use Xcode's [Quick Help] inspector."

这是一个错误的答案。正确的答案是“使用Xcode的[快速帮助]检查器”。

Can anyone explain how this relates to finding out about the type of a variable or constant?

有人能解释一下这和发现变量或常数的类型有什么关系吗?

The suggestion here is that if you rewrite the source to make a private variable public (i.e. change the way it is encapsulated) it would make it easier to find the type of the variable in question.

这里的建议是,如果您重写源代码以使一个私有变量成为公共的(即改变封装的方式),就可以更容易地找到所涉及的变量的类型。