我对Objective c完全不熟悉。如何使用Rogue初始化Nissan并调用方法drive()以确保打印出来?
#import <Foundation/Foundation.h>
#import "Car.h"
int main(int argc, const char * argv[])
{
@autoreleasepool
{
Car *drive = [[Car alloc] init];
Car *Nissan =[[Car alloc] init];
[Nissan drive];
Nissan.model=@"Rogue";
}
}
答案 0 :(得分:0)
对于基本实现,您可以遵循此结构。
Car.h
#import <Foundation/Foundation.h>
@interface Car : NSObject
@property (nonatomic) NSString *model;
- (void)drive;
@end
Car.m
#import "Car.h"
@implementation Car
- (void)drive {
NSLog(@"Car model is: %@", self.model);
}
@end
的main.m
#import <Foundation/Foundation.h>
#import "Car.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Car *nissan = [[Car alloc]init];
nissan.model = @"Rogue";
[nissan drive];
}
return 0;
}