开发者

iPhone Super/SubClass Example

I have a superclass Question:

import <Foundation/Foundation.h>
#import "Answer.h"

@interface Question : NSObject {
    NSString* qId;
    NSString* qTitle;
    NSString* qNumber;  
    NSString* sectionId;
    NSString* type;
    Answer* answer;
}

@property (nonatomic, retain) NSString* qId;
@property (nonatomic, retain) NSString* qTitle;
@property (nonatomic, retain) NSString* qNumber;
@property (nonatomic, retain) NSString* sectionId;
@property (nonatomic, retain) NSString* type;
@property (nonatomic, retain) Answer* answer;

@end


#import "Question.h"

@implementation Question
@synthesize qId, qTitle, qNumber, sectionId, type, answer;

-(id)init
{
    if (self = [super init])
    {
        // Initialization code here
        answer = [[Answer alloc]init];
    }
    return self;
}

-(void) dealloc{
    [answer release];
    [super dealloc];
}

@end

I have several types of Question, one example is a Slider Question开发者_JS百科. I want this class to subclass Question:

#import <Foundation/Foundation.h>
#import "Question.h"

@interface SliderQuestion : Question {
    (NSString*) min;
    (NSString*) max;
}

@property (nonatomic, retain) NSString* min;
@property (nonatomic, retain) NSString* max;

@end
}

#import "SliderQuestion.h"

@implementation SliderQuestion

@synthesize min, max;

@end

Is this the correct way to subclass? Will SliderQuestion inherit the properties contained within Question?

SliderQuestion* s = [[SliderQuestion alloc]init];
NSLog(@"%@", s.qId); //is this valid


Do you really want min and max to be instances of NSString? It seems that floats would be more appropriate.

Also, scrap the () in (NSString *) to remove the warning/error message.

Finally, this is the appropriate way to subclass. An instance of SliderQuestion will inherit all properties and methods of the Question class (and NSObject as well)


Notice the ( and ) around the NSStrings, you might wanna remove those.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜