1.对象的存储细节
#import
@interface Person : NSObject
{
@public
NSString *_name;
int _age;
}
- (void)run;
@end
@implementation Person
- (void)run{
NSLog(@"%@ 像狗一样疯跑", _name);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person *p = [Person new];
p->_name = @"柯南";
p->_age = 12;
[p run];
NSLog(@"姓名:%@, 年龄:%d", p->_name, p->_age);
NSLog(@"内存地址:%p",p);
Person *p2 = [Person new];
p2->_name = @"步美";
p2->_age = 11;
NSLog(@"姓名:%@, 年龄:%d", p2->_name, p2->_age);
NSLog(@"内存地址:%p",p2);
}
2.paragma mark指令的使用
#import
#pragma mark 狗类的声明
@interface Dog : NSObject
{
int _age;
NSString *_name;
}
- (void)eat;
@end
#pragma mark 狗类的实现
@implementation Dog
- (void)eat{
NSLog(@"狗在吃东西");
}
@end
#pragma mark - 人类的声明
@interface Person : NSObject
{
@public
NSString *_name;
int _age;
}
- (void)run;
@end
#pragma mark 人类的实现
@implementation Person
- (void)run{
NSLog(@"%@ 像狗一样疯跑", _name);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSLog(@"Hello, World!");
}
3.函数和对象方法的区别
#import
@interface Person : NSObject
{
@public
NSString *_name;
int _age;
}
- (void)run;
+ (void)test;
@end
@implementation Person
- (void)run{
NSLog(@"%@ 像狗一样疯跑", _name);
}
+ (void)test{
NSLog(@"调用类方法");
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person *p = [Person new];
p->_age = 20;
p->_name = @"路飞";
[p run];
[Person test];
test();
}
return 0;
4.常见错误汇总
#import
#pragma mark 狗类的声明
@interface Dog : NSObject
{
int _age;
NSString *_name;
}
- (void)eat;
@end
#pragma mark - 人类的声明
@interface Person : NSObject
{
@public
NSString *_name;
int _age;
}
- (void)run;
@end
#pragma mark 狗类的实现
@implementation Dog
- (void)eat{
NSLog(@"狗在吃东西");
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person *p = [Person new];
p->_name = @"凤姐";
p->_age = 30;
NSLog(@"aaa");
@try {
[p run];
}
@catch (NSException *exception) {
NSLog(@"出错了, run方法没有实现");
NSLog(@"==%@", exception);
}
@finally {
NSLog(@"巴巴爸爸");
}
NSLog(@"出现吧神龙");
}
return 0;
}
#pragma mark 人类的实现
@implementation Person
@end
5.对象和方法之间的关系
#import
typedef enum{
kSexWomen, kSexMan, kSexYao
}Sex;
@interface Person : NSObject
{
@public
NSString *_name;
int _age;
Sex _sex;
}
- (void)run;
- (void)showPerson:(Person *)person;
- (Person *)changePerson:(Person *)person;
@end
@implementation Person
- (void)run{
NSLog(@"%@ 像狗一样疯跑", _name);
}
- (void)showPerson:(Person *)person{
NSLog(@"传进来人对象的姓名:%@, 年龄:%d, 性别:%d", person->_name, person->_age, person->_sex);
}
- (Person *)changePerson:(Person *)person{
person->_sex = kSexYao;
return person;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person *p = [Person new];
Person *p1 = [Person new];
p1->_name = @"凤姐";
p1->_age = 30;
[p showPerson:p1];
Person *p2 = [Person new];
p2->_name = @"春哥";
p2->_age = 28;
p2->_sex = kSexWomen;
[p showPerson:p2];
[p changePerson:p2];
[p showPerson:p2];
}
return 0;
}
6.练习题1
#import
typedef enum{
kColorBlack, kColorYellow, kColorHua,kColorWhite
}Color;
typedef enum{
kSexFemale, kSexMale, kSexYao
}Sex;
@interface Dog : NSObject
{
@public
Color _color;
int _speed;
Sex _sex;
float _weight;
}
- (void)eat:(NSString *)foodName;
- (void)bark;
- (void)run;
- (BOOL)compareColorWithDog:(Dog *)dog;
- (int)compareSpeedWithDog:(Dog *)dog;
@end
@implementation Dog
- (void)eat:(NSString *)foodName{
_weight += 0.5;
NSLog(@"哮天犬 %@ ,当前体重:%.2f", foodName, _weight);
}
- (void)bark{
NSLog(@"颜色:%d, 速度:%d, 性别:%d, 体重:%.2f", _color, _speed, _sex, _weight);
}
- (void)run{
_weight -= 0.5;
NSLog(@"哮天犬正在以%d速度跑, 跑完后的体重%.2f", _speed ,_weight);
}
- (BOOL)compareColorWithDog:(Dog *)dog{
if (_color == dog->_color) {
return YES;
}else{
return NO;
}
}
- (int)compareSpeedWithDog:(Dog *)dog{
return _speed - (dog->_speed);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Dog *d = [Dog new];
d->_color = kColorBlack;
d->_sex = kSexYao;
d->_speed = 80;
d->_weight = 66.6f;
[d eat:@"带粑粑的榴莲"];
[d eat:@"带粑粑的红薯"];
[d bark];
[d run];
Dog *d2 = [Dog new];
d2->_color = kColorHua;
d2->_sex = kSexFemale;
d2->_speed = 77;
d2->_weight = 55.5f;
BOOL comColor = [d compareColorWithDog:d2];
NSLog(@"比较颜色:%d", comColor);
int comSpeed = [d compareSpeedWithDog:d2];
NSLog(@"比较速度:%d", comSpeed);
}
return 0;
}
7.练习题2
#import
#pragma mark - Dog类的声明
typedef enum{
kColorBlack, kColorYellow, kColorHua,kColorWhite
}Color;
typedef enum{
kSexFemale, kSexMale, kSexYao
}Sex;
@interface Dog : NSObject
{
@public
Color _color;
int _speed;
Sex _sex;
float _weight;
}
- (void)eat:(NSString *)foodName;
- (void)bark;
- (void)run;
- (BOOL)compareColorWithDog:(Dog *)dog;
- (int)compareSpeedWithDog:(Dog *)dog;
@end
#pragma mark - Dog类的实现
@implementation Dog
- (void)eat:(NSString *)foodName{
_weight += 0.5;
NSLog(@"哮天犬 吃%@ ,当前体重:%.2f", foodName, _weight);
}
- (void)bark{
NSLog(@"颜色:%d, 速度:%d, 性别:%d, 体重:%.2f", _color, _speed, _sex, _weight);
}
- (void)run{
_weight -= 0.5;
NSLog(@"哮天犬正在以%d速度跑, 跑完后的体重%.2f", _speed ,_weight);
}
- (BOOL)compareColorWithDog:(Dog *)dog{
if (_color == dog->_color) {
return YES;
}else{
return NO;
}
}
- (int)compareSpeedWithDog:(Dog *)dog{
return _speed - (dog->_speed);
}
@end
#pragma mark - Person类的声明
@interface Person : NSObject
{
@public
NSString *_name;
Dog *_dog;
}
- (void)feedWithDog:(NSString *)foodName;
- (void)WalkWithDog;
@end
#pragma mark - Person类的实现
@implementation Person
- (void)feedWithDog:(NSString *)foodName{
[_dog eat:foodName];
}
- (void)walkWithDog{
[_dog run];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Dog *d = [Dog new];
d->_color = kColorBlack;
d->_sex = kSexYao;
d->_speed = 80;
d->_weight = 66.6f;
Person *p = [Person new];
p->_name = @"二郎神";
p->_dog = d;
[p feedWithDog:@"蟠桃"];
[p feedWithDog:@"人参果"];
[p walkWithDog];
}
return 0;
}
8.对象作为方法的参数连续传递
#import
@interface Gun : NSObject
{
@public
NSString *_size;
int _bulletCount;
}
- (void)shoot;
@end
@implementation Gun
- (void)shoot{
_bulletCount--;
NSLog(@" %@ 正在突突突的射击, 剩余子弹:%d", _size, _bulletCount);
}
@end
@interface Soldier : NSObject
{
@public
NSString *_name;
int _life;
int _level;
}
- (void)fireByGun:(Gun *)gun;
@end
@implementation Soldier
- (void)fireByGun:(Gun *)gun{
[gun shoot];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Gun *g = [Gun new];
g->_size = @"AK47";
g->_bulletCount = 5;
Soldier *s = [Soldier new];
s->_name = @"嘎子";
s->_life = 100;
s->_level = 3;
[s fireByGun:g];
[s fireByGun:g];
[s fireByGun:g];
[s fireByGun:g];
[s fireByGun:g];
[s fireByGun:g];
}
return 0;
}
9.对象作为方法的参数连续传递2
#import
@interface Gun : NSObject
{
@public
NSString *_size;
int _bulletCount;
}
- (void)shoot;
@end
@implementation Gun
- (void)shoot{
_bulletCount--;
NSLog(@" %@ 正在突突突的射击, 剩余子弹:%d", _size, _bulletCount);
}
@end
@interface Soldier : NSObject
{
@public
NSString *_name;
int _life;
int _level;
}
- (void)fireByGun:(Gun *)gun;
@end
@implementation Soldier
- (void)fireByGun:(Gun *)gun{
[gun shoot];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Gun *g = [Gun new];
g->_size = @"AK47";
g->_bulletCount = 5;
Soldier *s = [Soldier new];
s->_name = @"嘎子";
s->_life = 100;
s->_level = 3;
[s fireByGun:g];
[s fireByGun:g];
[s fireByGun:g];
[s fireByGun:g];
[s fireByGun:g];
[s fireByGun:g];
}
return 0;
}
10.对象作为方法的参数连续传递3
#import
@interface Gun : NSObject
{
@public
NSString *_size;
int _bulletCount;
}
- (void)shoot;
@end
@implementation Gun
- (void)shoot{
if(_bulletCount > 0){
_bulletCount--;
NSLog(@" %@ 正在突突突的射击, 剩余子弹:%d", _size, _bulletCount);
}else{
NSLog(@"没子弹了,还突突个毛线,赶紧跑, 剩余子弹数:%d", _bulletCount);
}
}
@end
@interface Soldier : NSObject
{
@public
NSString *_name;
int _life;
int _level;
}
- (void)fireByGun:(Gun *)gun;
@end
@implementation Soldier
- (void)fireByGun:(Gun *)gun{
[gun shoot];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Gun *g = [Gun new];
g->_size = @"AK47";
g->_bulletCount = 5;
Soldier *s = [Soldier new];
s->_name = @"嘎子";
s->_life = 100;
s->_level = 3;
[s fireByGun:g];
[s fireByGun:g];
[s fireByGun:g];
[s fireByGun:g];
[s fireByGun:g];
[s fireByGun:g];
}
return 0;
}
11.练习题讲解(二)
#import
typedef struct{
int year;
int month;
int day;
}MyDate;
@interface Student : NSObject
{
@public
NSString *_name;
MyDate _birthday;
}
@end
@implementation Student
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Student *stu = [Student new];
stu->_name = @"六道杠红领巾";
stu->_birthday = (MyDate){1999, 9, 9};
stu->_birthday.year = 2015;
stu->_birthday.month = 3;
stu->_birthday.day = 26;
NSLog(@"%d年, %d月, %d日", stu->_birthday.year, stu->_birthday.month, stu->_birthday.day);
}
return 0;
}
12.NSString类介绍及用法
#import
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *s1 = @"班长是一个节操满满的好少年";
NSString *s5 = [[NSString alloc] initWithString:s1];
NSLog(@"s5 = %@", s5);
}
return 0;
}
13.NSString字符串长度计算方法
#import
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *s4 = @"学挖掘机哪家强OC";
int len4 = [s4 length];
NSLog(@"len2 = %d", len4);
}
return 0;
}