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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:04:49  来源:igfitidea点击:

How to implement nested classes in objective-c like Java?

javaobjective-c

提问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 Aclass could build the Bclass 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

Objective C 中的私有类

http://lists.apple.com/archives/objc-language/2004/Dec/msg00041.html

http://lists.apple.com/archives/objc-language/2004/Dec/msg00041.html