首页 > 代码库 > oc面向对象初步

oc面向对象初步

//建立一个Car类.h文件#import <Foundation/Foundation.h>interface Car : NSObject{     @public     int wheels;}- (instancetype)init;- (void)runWithSpeed :(int) speed AndDirection : (int) direction;+ (NSString *) fetchCarBrand;@end//建立一个Car类.m文件#import <Foundation/Foundation.h>#import <Car.h>@implementation Car+ (void)initialize{    [super initialize];    [self fetchCarBrand];    NSLog(@"%s", __PRETTY_FUNCTION__);}- (instancetype)init{   if ([super init]!=nil) {        wheels = 90;        brand = nil;    }    return self;}#import <Foundation/Foundation.h>#import "Car.h"@interface QQCar : Car{    int cheapPrice;}- (instancetype)initWithPrice: (int) price;@end//建立一个QQCar类.h文件#import "QQCar.h"@implementation QQCar- (instancetype)init{    if ([super init]) {        cheapPrice = 30000;    }    return self;}- (instancetype)initWithPrice: (int) price{    if ([super init]) {        cheapPrice = price;    }        return self;}@end//建立一个QQCar类.m文件#import <UIKit/UIKit.h>@interface ViewController : UIViewController@end//建立一个ViewController类.h文件#import "ViewController.h"#import "Car.h"#import "QQCar.h"@interface ViewController ()@end//建立一个ViewController类.m文件@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];    Car *car = [[Car alloc]init];    car->wheels = 4;    [car runWithSpeed:100 andDirection:1];    [Car fetchCarBrand];    QQCar *qqcar = [[QQCar alloc] initWithPrice:40000];    [qqcar runWithSpeed:30 andDirection:2];}@end

  

oc面向对象初步