Objective-C:如何从子类访问父属性? [英] Objective-C: How do you access parent properties from subclasses?

查看:143
本文介绍了Objective-C:如何从子类访问父属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我定义了这个类,如何在子类中访问 someObject 属性而不出现编译器错误?

If I have this class defined, how do I access the someObject property in subclasses without compiler errors?

@interface MyBaseClass
  // someObject property not declared here because I want it to be scoped 
  // protected. Only this class instance and subclass instances should be
  // able to see the someObject property.
@end

// This is a private interface extension...properties declared here
// won't be visible to subclasses. However, I don't see any way to 
// declare protected properties...
@interface MyBaseClass (private)
   @property (nonatomic, readwrite, retain) NSObject *someObject;
@end



@interface MySubclass : MyBaseClass 
@end

@implementation MySubclass

- (id) init {
    // Try to do something with the super classes' someObject property. 
    // Always throws compile errors.

    // Semantic Issue: Property 'someObject' not found 
    // object of type 'MySubclass *'
    self.someObject = nil; 

}
@end




我显然不明白继承如何在objective-c中工作。



I'm obviously not understanding how inheritance works in objective-c. Could someone enlighten me?

推荐答案

解决方案是在类扩展中声明MyBaseClass私有属性: p>

The solution you're after is to declare the MyBaseClass private property in a class extension:

@interface MyBaseClass ()
@property (nonatomic, readwrite, retain) NSObject *someObject;
@end

您随后可以自由声明 >在MySubclass中的MyBaseClass 。这让MySubclass知道这些属性,以便它的代码可以谈论他们。

You are then free to make that declaration both in MyBaseClass and in MySubclass. This lets MySubclass know about these properties so that its code can talk about them.

如果重复打扰你,将类扩展名放在自己的.h文件中,将它导入到两个.m文件中。

If the repetition bothers you, put the class extension in a .h file of its own and import it into both .m files.

我将从自己的代码中给出一个例子。这是 MyDownloaderPrivateProperties.h

I will give an example from my own code. Here is MyDownloaderPrivateProperties.h:

@interface MyDownloader ()
@property (nonatomic, strong, readwrite) NSURLConnection* connection;
@property (nonatomic, copy, readwrite) NSURLRequest* request;
@property (nonatomic, strong, readwrite) NSMutableData* mutableReceivedData;
@end

没有相应的 .m 文件这就是这个文件中的所有;它是,因为它是纯粹的声明性。现在这是 MyDownloader.m 的开始:

There is no corresponding .m file and that's all that's in this file; it is, as it were, purely declarative. Now here's the start of MyDownloader.m:

#import "MyDownloader.h"
#import "MyDownloaderPrivateProperties.h"
@implementation MyDownloader
@synthesize connection=_connection;
@synthesize request=_request;
@synthesize mutableReceivedData=_mutableReceivedData;
// ...

下面是它的子类MyImageDownloader的开始。

And here's the start of its subclass MyImageDownloader.m:

#import "MyImageDownloader.h"
#import "MyDownloaderPrivateProperties.h"

问题解决。隐私保留,因为这些是导入 MyDownloaderPrivateProperties.h 的唯一类,因此它们是关于编译器所知的关于这些属性的唯一的类(这是所有的隐私是在Objective- C)。子类可以访问私有属性,其访问器由超类合成。我相信这是你想要完成的第一个地方。

Problem solved. Privacy is preserved, as these are the only classes that import MyDownloaderPrivateProperties.h so they are the only classes that know about these properties as far as the compiler is concerned (and that's all that privacy is in Objective-C). The subclass can access the private properties whose accessors are synthesized by the superclass. I believe that's what you wanted to accomplish in the first place.

这篇关于Objective-C:如何从子类访问父属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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