java 如何像Java一样在objective-c中实现嵌套类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11690932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement nested classes in objective-c like Java?
提问by graffiti
I need to implement nested classes. How do I write it in objective-c?
For example, I have such code:
我需要实现嵌套类。我如何在objective-c中编写它?
例如,我有这样的代码:
public class A {
private int a;
private B b;
class B {
private int z;
public void f() {
a = 1;
}
}
public void fA() {
b.f();
}
}
How can I implement this code in objective-c??
我如何在objective-c中实现这段代码??
回答by
You can't, as there aren't nested classes in Objective-C. Your A
class could build the B
class dynamically then register it with the runtime, but there doesn't seem much point as this isn't a natural idiom for the Objective-C language. Just create a normal class that isn't used outside your class.
你不能,因为在 Objective-C 中没有嵌套类。您的A
类可以B
动态构建类,然后将其注册到运行时,但似乎没有多大意义,因为这不是 Objective-C 语言的自然习语。只需创建一个不在班级外使用的普通班级。
回答by lreichold
From my understanding, Objective-C doesn't have inner classes.. Try these links where this question has been asked before:
根据我的理解,Objective-C 没有内部类。试试这些链接,之前已经问过这个问题:
How to access a variable from inner class
Private classes in Objective C
http://lists.apple.com/archives/objc-language/2004/Dec/msg00041.html
http://lists.apple.com/archives/objc-language/2004/Dec/msg00041.html