为什么我们需要单独的“.swift”每个班级的文件? [英] Why do we need separate ".swift" files for each class?

查看:88
本文介绍了为什么我们需要单独的“.swift”每个班级的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道你是否能够为我回答一个非常基本的初学者问题。我正在研究Lynda上的Cocoa + Swift教程,我对类/对象有点困惑。

Wondering if you might be able to answer a very basic beginner question for me. I’m working through the Cocoa + Swift tutorial on Lynda and I’m a little confused about classes/objects.

基本上,我想知道为什么我们必须为我们创建的每个新类创建一个新的swift文件。

Basically, I want to know why we have to create a new swift file for each new class we create.

据我所知,您可以在项目的任何 .swift 文件中创建一个新类。我的问题是,为什么我们必须不断为每个新类创建 .swift 文件。

As far as I know, you can create a new class within any .swift file in the project. My question is, why do we have to continually keep creating .swift files for each new class.

我想知道为什么你不能只有一个名为 AllClasses.swift .swift 文件创建所有类,例如:

I’m wondering why there isn’t just one .swift file called AllClasses.swift that you can create all the classes in, for instance:

AllClasses.swift 中是以下代码:

Within AllClasses.swift is the following code:

Class FirstClass : NSObject
Class SecondClass : NSObject
Class ThirdClass : NSObject
Class FourthClass : NSObject

反对:

FirstClass.swift 中是以下代码:

Within FirstClass.swift is the following code:

Class FirstClass : NSObject

SecondClass.swift 中的代码如下:

Within SecondClass.swift is the following code:

Class SecondClass : NSObject

ThirdClass.swift 中包含以下代码:

Within ThirdClass.swift is the following code:

Class ThirdClass : NSObject

FourthCl中ass.swift 是以下代码:

Within FourthClass.swift is the following code:

Class FourthClass : NSObject

我只想知道为什么我们需要将不同的代码分成文件,如果可以从项目的任何区域内调用它们。对于Mac应用程序,似乎几乎所有内容都可以在 AppDelegate.swift 文件中完成。

I just want to know why we need to separate different code into files if it can be called from within any area of the project. In the case of a Mac application, it seems like almost everything could be done from within the AppDelegate.swift file.

这是一个蠢货问题,但另一个可能使面向对象的障碍成为我完全掌握的难题。

This is a moronic question, but another hurdle that may be making object orientation a hard concept for me to fully grasp.

推荐答案

也许我可以解释一下以一种有趣的方式:

Maybe I can explain it in a somewhat amusing way:

一开始,没有文件概念,所有代码都在一个实体中。这些实体中的代码由行号引用。因为一切都在一个地方很容易找到你想要的东西,即使程序很小。它比打孔磁带更好,因此有很多欢乐。我们必须做一些关于从盒式磁带加载的事情。

In the beginning there was no concept of files and all code was in a single entity. Code within such entities was referenced by line numbers. Because everything was in one place it was easy to find what you wanted, even though programs were small. It was better than punch tape and so there was much rejoicing. We gotta do something about loading from cassette though.

然后有人发现你可以把代码分解成名为 modules 的单独部分以及软件更大。男人我的10MB硬盘是巨大的。 每个模块都是专家,可以致电其他专家。 它使您的代码更易于导航。非常高兴。

But then someone discovered you could break up the code into separate parts called modules which was just as well as software was getting bigger. Man my 10MB hard drive is huge. Each module was a specialist and could call other specialists. It made your code easier to navigate. There was much rejoicing.

然后有人发现面向对象(OO)并且文件很便宜。程序是如此之大,现在人们很难找到那个模拟非洲燕子的空速的课程,该课程包含10000多行的多课程文件,可能是时候开始上课了在自己的文件中。毋庸置疑,有很多欢欣鼓舞。

But then someone discovered object-orientation (OO) and files were cheap. Programs were so large now people were having a hard time finding that class that modelled the airspeed of an African Swallow in that multiple-class-containing file of 10000+ lines that maybe its time to start putting each class in its own file. Needless to say there was much rejoicing.

然后软件已经变得如此之大,以至于有人发现了源代码控制,这对于一个编码团队来说是最重要的所有抄写员都在一块软件上冥想。疯狂确保了兄弟情谊,他们不小心努力在一个30,000多行的文件中编写一个程序(非洲燕子研究已经发展到包括欧洲燕子),即使有OO,也只会导致冲突后的线冲突在他们尝试检查源控制系统的更改时。赌注有很多燃烧。后来的启示导致将代码分解成许多文本或文件是避免私刑的方法。

Then software had become so large that someone discovered source control which was most important when a team of coding scribes all meditated on a piece of software. Madness ensured for the brotherhood whose careless endeavour to write a program in one file of 30,000+ lines (research on African Swallows had grown to include European Swallows) even with OO, only lead to line conflict after line conflict during their attempts to check in changes into the source control system. There was much burning at the stake. Later revelations lead to breaking up the code into many texts or files was the way to avoid a lynching.


  • 总之,没有规则要说每个类必须有一个文件,但这样做是一个很好的做法,主要是因为你的程序增长到任何合理的大小或复杂程度,如果不这样做,代码的导航和维护将成为一个问题。

  • 与团队合作变得更加重要,因为在任何给定文件上并发工作的作者数量,源代码提交冲突的可能性就会增加。

我相信僧侣现在正在研究他们最喜欢的颜色和首都城市。

I believe the monks are studying their favourite colours and capital cities of countries now.

这篇关于为什么我们需要单独的“.swift”每个班级的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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