为什么要使用嵌套类? [英] Why would one use nested classes?

查看:104
本文介绍了为什么要使用嵌套类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

做Objective-c已有5年了,所以请忍受我.

Been doing objective-c for 5 years, so please bear with me.

我一直在努力寻找文档,这些文档清楚地说明了为什么要在另一个定义内嵌套一个类定义.

I'm struggling to find documentation that clearly explains why you would want to nest a class definition, inside of another.

如果我有两个像下面这样的类,对我来说有意义的是,它们被声明为彼此上下.您甚至可能希望拥有一个引用另一个属性的嵌套属性.

If I had two classes like the following, it makes sense to me that they are declared above and below each other. You may even want to have a nested property that references the other.

class DataImporter {
     }

class DataGenerator {
     }

但是我不明白为什么像下面这样的嵌套安排会有用.

But I don't understand why a nested arrangement like the following would be useful.

class DataImporter {
     class DataGenerator {
     } }

推荐答案

您可以使用此功能进行命名空间设置.这样,您就可以使用具有相同名称的类(例如DataGenerator).但是出于不同的目的.这将是DataImporter.DataGenerator类,但是您可以拥有另一个OtherClass.DataGenerator类,这将是一个完全不同的类.

You would do that for Namespacing. This way you can have the classes with the same name (like DataGenerator). But for different purposes. This one would be DataImporter.DataGenerator class, but you could have another OtherClass.DataGenerator class which would be a totally different one.

类似于Objective-C,当您在类名之前有2或3个字母时(例如:UIView).但是当您创建自己的文件时,它就像SSView

Like in Objective-C when you had 2 or 3 letters before the name of the class (ex: UIView). But when you created your own, it would be like SSView

您还可以将内部类声明为私有,并仅在该文件中使用它.

You can also declare the inner class private and use it just in that file.

这篇关于为什么要使用嵌套类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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