简体   繁体   English

Objective-C私有财产继承

[英]Objective-C Private Property Inheritance

I just wanted to make sure my understanding of property inheritance was correct. 我只是想确保我对属性继承的理解是正确的。 I'm currently trying to make a subclass of a UIViewController. 我目前正在尝试制作UIViewController的子类。 In my UIViewController all my outlets and such are declared in the implementation section like so: 在我的UIViewController中,我的所有插座和此类插座都在实现部分中声明,如下所示:

@interface BaseClass()

@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;

@end

Thus those properties are then private, correct? 因此,那些属性是私有的,对吗? Now when I try and make a subclass and access those properties using my getters and setters, I cannot access them from my subclass. 现在,当我尝试创建一个子类并使用我的getter和setter访问那些属性时,我无法从我的子类中访问它们。 Is the proper form to redeclare those properties again in my subclass' implementation section, like so? 是否像在我的子类的实现部分中一样再次声明这些属性的正确形式?

@interface SubClass()

@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;

@end

I guess I am ok with doing it this way, but then I feel like it ruins the purpose of inheritance. 我想我可以这样做,但是后来我觉得它破坏了继承的目的。 What is the proper way/what am I doing wrong? 正确的方法是什么/我在做什么错?

I would declare the property in the public interface for the BaseClass - I don't see any reason to put them in a class extension. 我会在BaseClass的公共接口中声明该属性-我看不出有任何理由将它们放在类扩展中。

@interface BaseClass : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;

@end

@interface SubClass : BaseClass

// No need to redeclare the property as you're inheriting it.

@end

[EDIT] [编辑]

If you must use the class extension then you could use a private header to achieve the same. 如果必须使用类扩展名,则可以使用私有标头来实现相同的目的。

Public header for BaseClass (BaseClass.h) BaseClass(BaseClass.h)的公共

@interface BaseClass : UIViewController

@end

Private header for BaseClass (BaseClass-Private.h) BaseClass的专用标头(BaseClass-Private.h)

@interface BaseClass ()

@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;

@end

Public header for SubClass (SubClass.h) 子类的公共标头(SubClass.h)

#import "SubClass.h"

@interface SubClass : BaseClass

@end

Implementation of SubClass (SubClass.m) 子类(SubClass.m)的实现

#import "BaseClass-Private.h"

@implementation SubClass

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM