码迷,mamicode.com
首页 > 编程语言 > 详细

OC -- 带有enum和struct的类的简单设计

时间:2015-07-21 23:55:00      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

#import <Foundation/Foundation.h>

 

@interface Car : NSObject{

    @public

    int _wheels;

    int _speed;

}

 

- (void)run;

 

@end

 

@implementation Car

 

- (void)run{

    NSLog(@"%d个轮子, 速度为%d的车子跑起来了", _wheels, _speed);

}

 

@end

 

void test1(Car *newCar){

    newCar->_wheels = 5;

}

 

void test2(Car *newCar);

 

int main(int argc, const char * argv[]) {

    

    Car *c1 = [[Car alloc] init];

    Car *c2 = [[Car alloc] init];

    test1(c1);

    test2(c2);

    [c1 run];

    [c2 run];

    

    return 0;

}

 

void test2(Car *newCar){

    newCar->_wheels = 4;

    newCar->_speed = 300;

}

*/

/*

#import <Foundation/Foundation.h>

 

@interface Persons : NSObject{

 

    @public

    NSString *_name;

    int _age;

}

 

- (void)print;

 

@end

 

@implementation Persons

 

- (void)print{

    NSLog(@"name = %@, age = %d", _name, _age);

}

 

@end

 

void sett(Persons *p);

 

int main(int argc, const char * argv[]){

 

    

    

    Persons *p = [[Persons alloc] init];

    sett(p);

    [p print];

    

    return 0;

}

 

 

 

void sett(Persons *p){

    p->_name = @"Jerry";

    p->_age = 15;

}

*/

 

#import <Foundation/Foundation.h>

 

typedef enum{

    SexMan,

    SexWoman

} Sex;

 

typedef enum{

    ColorBlue,

    ColorWhite,

    ColorBlack,

    ColorGreen,

    ColorRed,

    ColorOrange

} Color;

 

typedef struct{

    int year;

    int month;

    int day;

} Date;

 

@interface Person : NSObject

 

@property NSString *name;

@property int age;

@property Sex sex;

@property Color favColor;

@property Date birthday;

 

- (void)print;

 

@end

 

@implementation Person

 

- (void)print{

    NSLog(@"name = %@, age = %d, sex = %d, FavColor = %d, birthday = %d-%d-%d", _name, _age, _sex, _favColor, _birthday.year, _birthday.month, _birthday.day);

}

 

@end

 

int main(int argc, const char * argv[]){

 

    Person *p = [[Person alloc] init];

    p.name = @"Jerry";

    p.age = 10;

    p.sex = SexMan;

    p.favColor = ColorWhite;

    Date d = {2015, 7, 21};

    p.birthday = d;

    [p print];

    

    return 0;

}

 

OC -- 带有enum和struct的类的简单设计

标签:

原文地址:http://www.cnblogs.com/lianfu/p/4665932.html

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