代码之家  ›  专栏  ›  技术社区  ›  LK.

Objective-C中的保护方法

  •  109
  • LK.  · 技术社区  · 14 年前

    在Objective-C中,什么等效于受保护的方法? 我想定义只有派生类可以调用/实现的方法。

    8 回复  |  直到 11 年前
        1
  •  47
  •   Pang Ajmal PraveeN    7 年前

    既不能声明受保护的方法 私人的。Objective-C的动态特性使得无法实现方法的访问控制。(你可以用沉重的代价 修改编译器或运行时,速度会受到严重的影响,但由于明显的原因,没有这样做。)

    取自 Source .

        2
  •  156
  •   Enrique Brian Westphal    7 年前

    你可以 模拟

    • 在类扩展中声明私有方法(即在类“.m”文件顶部附近声明的未命名类别)

    正如Sachin所指出的,这些保护并不是在运行时强制执行的(例如在Java中)。

        3
  •  14
  •   Michael Kernahan    12 年前

    下面是我为使受保护的方法对我的子类可见而做的,而不要求它们自己实现这些方法。这意味着我在我的子类中没有收到编译器关于实现不完整的警告。

    @protocol SuperClassProtectedMethods <NSObject>
    - (void) protectMethod:(NSObject *)foo;
    @end
    
    @interface SuperClass (ProtectedMethods) < SuperClassProtectedMethods >
    @end
    

    m:(编译器现在将强制您添加受保护的方法)

    #import "SuperClassProtectedMethods.h"
    @implementation SuperClass
    - (void) protectedMethod:(NSObject *)foo {}
    @end
    

    #import "SuperClassProtectedMethods.h"
    // Subclass can now call the protected methods, but no external classes importing .h files will be able to see the protected methods.
    
        4
  •  9
  •   redwud    11 年前

    我刚刚发现了这个,它对我有用我。给你改进Adam的答案,在你的超类中,在.m文件中实现受保护的方法,但不要在.h文件中声明它。在您的子类中,在.m文件中创建一个新的类别,声明超类的受保护方法,您可以在子类中使用超类的受保护方法。如果在运行时强制执行,这最终不会阻止假定受保护方法的调用方。

    /////// SuperClass.h
    @interface SuperClass
    
    @end
    
    /////// SuperClass.m
    @implementation SuperClass
    - (void) protectedMethod
    {}
    @end
    
    /////// SubClass.h
    @interface SubClass : SuperClass
    @end
    
    /////// SubClass.m
    @interface SubClass (Protected)
    - (void) protectedMethod ;
    @end
    
    @implementation SubClass
    - (void) callerOfProtectedMethod
    {
      [self protectedMethod] ; // this will not generate warning
    } 
    @end
    
        5
  •  2
  •   marius bardan    10 年前

    另一种使用@protected变量的方法。

    @interface SuperClass:NSObject{
      @protected
        SEL protectedMehodSelector;
    }
    
    - (void) hackIt;
    @end
    
    @implementation SuperClass
    
    -(id)init{
    
    self = [super init];
    if(self) {
     protectedMethodSelector = @selector(baseHandling);
     }
    
    return self;
    }
    
    - (void) baseHandling {
    
      // execute your code here
    }
    
    -(void) hackIt {
    
      [self performSelector: protectedMethodSelector];
    }
    
    @end
    
    @interface SubClass:SuperClass
    @end
    
    @implementation SubClass
    
    -(id)init{
    
    self = [super init];
    if(self) {
     protectedMethodSelector = @selector(customHandling);
     }
    
    return self;
    }
    
    - (void) customHandling {
    
      // execute your custom code here
    }
    
    @end
    
        6
  •  1
  •   chinthakad    12 年前

    [super performSelector:@selector(privateMethod)];

        7
  •  0
  •   Adam Waite    11 年前

    你可以 某种程度上 使用类别执行此操作。

    @interface SomeClass (Protected)
    -(void)doMadProtectedThings;
    @end
    
    @implementation SomeClass (Protected)
    
    - (void)doMadProtectedThings{
        NSLog(@"As long as the .h isn't imported into a class of completely different family, these methods will never be seen. You have to import this header into the subclasses of the super instance though.");
    }
    
    @end
    

    最好的方法可能是@Brian Westphal回答的类延续类别,但是您必须为每个子类实例重新定义这个类别中的方法。

        8
  •  0
  •   Jadar ohho    10 年前

    一种选择是使用 类扩展

    .h :

    @interface SomeAppDelegate : UIResponder <UIApplicationDelegate>
    
    @property (strong, nonatomic) UIWindow *window;
    
    @end
    

    .m :

    @interface SomeAppDelegate()
    - (void)localMethod;
    @end
    
    @implementation SomeAppDelegate
    
    - (void)localMethod
    {
    }
    
    @end
    
        9
  •  0
  •   andrewchan2022    5 年前

    -(void) internalMethod;