开发者

Is it possible to share code betwee classes that does not have the same parent?

开发者 https://www.devze.com 2022-12-11 14:57 出处:网络
I have two classes that are already subclasses of a different parent (due to an external library I cannot change this). However I want to share som开发者_StackOverflow社区e common code between them, s

I have two classes that are already subclasses of a different parent (due to an external library I cannot change this). However I want to share som开发者_StackOverflow社区e common code between them, say for example, code that handles an action after a popup dialog etc. How do I do this?


Refactor the shared code into its own class, and include an instance of that class as a field/property within your other two classes.


You can re-factor the appropriate code into a utilities class, and then have the two classes call it. As for the iPhoneSDK, you can probably have the utility method be the delegate method itself.


You could write a category on a common ancestor class. Then both classes could import that Category and call the common code.

0

精彩评论

暂无评论...
验证码 换一张
取 消