可以重写类别方法吗?的iOS [英] Can category methods be overridden? IOS

查看:102
本文介绍了可以重写类别方法吗?的iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计划如何向某些对象的所有实例添加几个方法.我认为将类别添加到父对象(UIViewController)可以满足我的要求,但是我可以覆盖以这种方式添加的方法吗?大多数时候,子类将使用默认方法,但是我知道我将需要至少重写一次该方法.

I am trying to plan how to add a couple methods to all instances of certain objects. I think adding a category to the parent object(UIViewController) would work for what I want to do, but can I override a method added this way? Most of the time the subclasses will use the default methods but I do know I will need to override the method at least once.

我还想考虑其他什么方法?

Also what other methods should I consider for what I am trying to do?

我想做的事的例子:

我有一组类似于期刊页面的对象.这些页面是UIViewControllers的子类.我想添加用于加载,保存和类似方法.在日记类(NSObject的子类)中,我有一个数组,用于存储每个日记页面的实例.我想要它,因此我可以从数组中选择一个页面并在方法上运行,而不必知道特定的对象类,因为它们都应该具有方法.

I have a set of Objects that act like pages of a journal. These pages are subclasses of UIViewControllers. I want to add methods for loading, saving, and similar. In a journal class(Subclass of NSObject) I have an array storing instances of each Journal page. I want it so I can select a page from the array and run on of the methods without having to know the specific object class as they all should have the methods.

推荐答案

简短的回答:是的,可以.出于继承的目的,将超类上的方法与实际在超类中定义的方法一样对待,并且可以用相同的方式覆盖.

Short answer: yes, you can. For purposes of inheritance category methods on a superclass are treated like methods that are actually defined in the superclass, and can be overridden in the same way.

这篇关于可以重写类别方法吗?的iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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