Objective-C 中使用类别的私有方法:从子类调用 super

发布于 2024-08-08 06:21:34 字数 1786 浏览 8 评论 0原文

我正在阅读如何在 Objective-C 中实现私有方法(在 Objective-C 中为类定义私有方法的最佳方式),我的脑海中突然出现一个问题:

如何实现受保护的方法,即可见的私有方法假设

我有一个 MySuperClass,其类别包含其所有私有方法,并且我想实现一个 MySubclass 重写或调用 super 到 MySuperClass 私有方法之一。这可能吗(使用类别方法来实现私有方法)?

看一下这段代码,底部有重写方法。

// ===========================
// = File: MySuperClass.h
// = Interface for MySuperClass
// ===========================

@interface MySuperClass : Object
...
@end

// ===========================
// = File: MySuperClass.m
// ===========================
#import "MySuperClass.h"

// =================================
// = Interface for Private methods
// =================================
@interface MySuperClass (Private)

-(void) privateInstanceMethod; 

@end

// =====================================
// = Implementation of Private methods
// =====================================
@implementation MySuperClass (Private)

-(void) privateInstanceMethod
{
  //Do something
}

@end

// ================================
// = Implementation for MySuperClass
// ================================
@implementation MySuperClass
... 
@end




// ===========================
// = File: MySubClass.h
// = Interface for MySubClass
// ===========================

@interface MySubClass : MySuperClass
...
@end


// ================================
// = Implementation for MySubClass
// ================================

#import MySubClass.h

@implementation MySubClass
//OVERRIDING a Super Private method.
-(void) privateInstanceMethod
{
  [super privateInstanceMethod]; //Compiler error, privateInstanceMethod not visible!
  //Do something else
}
@end

希望有人已经弄清楚了这一点。

干杯!

I was reading how to implement private methods in Objective-C (Best way to define private methods for a class in Objective-C) and a question popped up in my mind:

How do you manage to implement protected methods, i.e. private methods that are visible to subclasses?

Suppose I have a MySuperClass with a Category containing all its private methods, and I want to implement a MySubclass overriding or calling super to one of the MySuperClass private methods. Is that possible (using the Categories approach towards implementing private methods)?

Take a look at some of this code, at the bottom there is the overriden method.

// ===========================
// = File: MySuperClass.h
// = Interface for MySuperClass
// ===========================

@interface MySuperClass : Object
...
@end

// ===========================
// = File: MySuperClass.m
// ===========================
#import "MySuperClass.h"

// =================================
// = Interface for Private methods
// =================================
@interface MySuperClass (Private)

-(void) privateInstanceMethod; 

@end

// =====================================
// = Implementation of Private methods
// =====================================
@implementation MySuperClass (Private)

-(void) privateInstanceMethod
{
  //Do something
}

@end

// ================================
// = Implementation for MySuperClass
// ================================
@implementation MySuperClass
... 
@end




// ===========================
// = File: MySubClass.h
// = Interface for MySubClass
// ===========================

@interface MySubClass : MySuperClass
...
@end


// ================================
// = Implementation for MySubClass
// ================================

#import MySubClass.h

@implementation MySubClass
//OVERRIDING a Super Private method.
-(void) privateInstanceMethod
{
  [super privateInstanceMethod]; //Compiler error, privateInstanceMethod not visible!
  //Do something else
}
@end

Hopefully somebody already figured this out.

Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拧巴小姐 2024-08-15 06:21:34

此 GNUStep 页面 描述了第 4.5 节中的一种方法:

...这就是好的一面
允许您模拟受保护的
方法也是如此。为此,笔者
子类必须在某些方面被告知
关于受保护方法的方式,以及
他们将需要忍受
编译器警告。或者,你
可以声明受保护类别
在单独的接口文件中(例如,
“PointProtected.h”),并提供此
接口文件的理解
它只能被进口并且
由子类的接口文件使用。

This GNUStep page describes one approach Section 4.5:

...The bright side of this is it
allows you to simulate protected
methods as well. For this, the writer
of a subclass must be informed in some
way about the protected methods, and
they will need to put up with the
compiler warnings. Alternatively, you
could declare the Protected category
in a separate interface file (e.g.,
"PointProtected.h"), and provide this
interface file with the understanding
that it should only be imported and
used by a subclass's interface file.

〗斷ホ乔殘χμё〖 2024-08-15 06:21:34

在 Apple,当他们构建框架时,典型的模式是拥有一个公共标头 (MyClass.h) 和一个私有标头 (MyClass_private.h),并且仅将公共标头复制到构建产品中。当然,.m 文件将 #import 两者。

At Apple, when they build the frameworks the typical pattern is to have a public header (MyClass.h) and a private header (MyClass_private.h), and only copy the public headers into the build product. The .m file will #import both of them, of course.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文