与Java中的Objective-C类相同的覆盖方法 [英] Overwrite method inside an objective-c class as in Java

查看:71
本文介绍了与Java中的Objective-C类相同的覆盖方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用此语句扩展类,而无需编写整个单独的文件.假设ClassFromFramework是一个类,是包含在库中的框架的一部分.

I often use this statement for extending class without needs of writing a whole separate file. Supposing ClassFromFramework is a class being part of a framework included in library.

public ClassFromFramework {

   public String myMethod() {
      // operations
   }

   //lot of other methods....

}

然后在我的课堂上,我可以做以下事情:

Then in my class I could do the following:

import com.framework.ClassFromFramework;

public MyClass {

   public void method() {

      ClassFromFramework m = new ClassFromFramework() {
        @Override
        public String myMethod() {
          // do operations...
        }
      } 

      m.myMethod();
   }
}

我想知道是否可以使用Objective-c达到相同的效果而无需声明新的.h .m文件组合并导入我的using类.

I wonder if I can achieve the same with Objective-c without declaring a new combination .h .m files and import in my using class.

推荐答案

您可以创建一个新的子类并覆盖方法,但是所有新类都必须位于自己的.h&中. .m文件.这就是Obj-C的运作方式.在这种情况下,拥有其他文件将是有意义的.

You can make a new subclass, and override methods, but all new classes must be in their own .h & .m files. That's how Obj-C operates. In this case, it would make sense to have the additional files.

您还可以使用单词super调用父方法.在继承ViewController的子类时(例如在viewDidLoad中),始终会执行此操作.

You can also call the parent method with the word super. This is done all the time when subclassing a ViewController, such as in viewDidLoad.

这篇关于与Java中的Objective-C类相同的覆盖方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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