是否可以让子类看到私有方法和属性? [英] Is it possible to have private methods and properties visible to subclasses?

查看:43
本文介绍了是否可以让子类看到私有方法和属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里要做的基本上是一个对子类也可见的类扩展.我想可以通过在单独的 Category 文件中声明私有方法和属性来实现效果,该文件具有独立于类的自己的 @implementation 块.然后超类和每个子类可以#import 标题.这是我能想到的最好的,但感觉像是不必要的文件污染,并且使用类别实现私有方法的做法在很大程度上已被类扩展所取代.我认为这是不可能的,但我想问问比我更酷的人想到了更好的方法.

What I'm going for here is basically a class Extension that is also visible to subclasses. I suppose the effect could be accomplished by declaring the private methods and properties in a separate Category file with its own @implementation block independent of the class's. The superclass and each subclass could then #import the header. That's the best I can think of, but it feels like unnecessary file pollution, and the practice of realizing private methods with Categories has largely been superseded by class Extensions. I'm thinking it's not possible, but I'm asking on the off-chance that someone cooler than me has thought of a better way.

推荐答案

您可以遵循自己的建议(在单独的标题中声明私有方法),但如果您愿意,可以使用扩展名,而不是类别.

You can follow your own suggestion (declaring the private methods in a separate header), but you can use an extension if you'd prefer, rather than a category.

Apple 通过 UIGestureRecognizer 做到了这一点.看看 UIGestureRecognizerSubclass.h.

Apple does exactly this with UIGestureRecognizer. Take a look in UIGestureRecognizerSubclass.h.

这篇关于是否可以让子类看到私有方法和属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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