码迷,mamicode.com
首页 > 其他好文 > 详细

oc面向对象初步

时间:2014-10-04 21:32:57      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:blog   io   ar   文件   sp   div   c   on   cti   

//建立一个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面向对象初步

标签:blog   io   ar   文件   sp   div   c   on   cti   

原文地址:http://www.cnblogs.com/diyigechengxu/p/4006250.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!