Car.h
#import <Foundation/Foundation.h>
@interface Car : NSObject{
@private
NSString *name_;
}
@property(strong) NSString *name;
@property(strong) NSString *type;
@end
void Exam_1_1();
void Exam_1_2();
void Exam_1_3_Car();
void Exam_1_4_Car2();
void Exam_1_5_Car_Property();
Car.m
#import "Car.h"
//车类实现
@implementation Car
- (void) setName:(NSString *)name{
name_ = name;
}
- (NSString*) name{
return name_;
}
@synthesize type;
@end
//简单ObjectCwb例子
void Exam_1_1()
{
@autoreleasepool {
NSString *yyName = @"湖南省交通科学研究院科学家";
NSLog(@"你好,%@!", yyName);
}
}
//简单类型
void Exam_1_2()
{
@autoreleasepool {
int i = 1;
NSLog(@"我的整型数=%i",i);
float f = 2;
NSLog(@"我的浮点型数=%f", f);
NSLog(@"我的浮点数(科学型)= %e", f);
char a = 'A';
NSLog(@"我的字符=%c", a);
NSLog(@"百分号%%");
NSString *s = @"我是ObjectC字符串";
NSLog(@"我的字符串=%@", s);
NSLog(@"我的字符串指针=%p", s);
NSLog(@"我的字符=%c,整数=%i,字符串=%@", a, i, s);
}
}
//简单对象
void Exam_1_3_Car()
{
@autoreleasepool{
Car *car = [[Car alloc] init];
NSLog(@"Car is %@", car);
}
}
//对象属性(间接式,setter,getter)
void Exam_1_4_Car2()
{
@autoreleasepool {
Car *car = [[Car alloc] init];
car.name = @"运动型汽车";
NSLog(@"车.名称=%@", car.name);
[car setName:@"新的车名"];
NSLog(@"新的车.名称=%@", [car name]);
}
}
//对象属性(直接式@synthesize)
void Exam_1_5_Car_Property()
{
@autoreleasepool {
Car *car = [[Car alloc] init];
car.type = @"运动";
NSLog(@"车.类型=%@", [car type]);
[car setType:@"商务"];
NSLog(@"车.类型=%@", [car type]);
}
}