二、代码
#import <Foundation/Foundation.h>
typedef enum
{
kIColorBlack, // 黑色
kIColorWhite, // 白色
kIColorTuHaoJin, // 土豪金
}IColor;
#pragma mark 类
@interface IPhone : NSObject
{
@public
float _model; // 型号
int _cpu; // cpu
double _size; // 尺寸
IColor _color; // 颜色 0代表黑色 1代表白色 2土豪金
}
-(void)about;
-(char *)loadMessage;
-(int) signal:(int)number;
- (int)sendMessageWithNumber:(int)number andContent:(char *)content;
+(int)sumWithValue1:(int)v1 andValue2:(int)v2;
+(void)demo;
//-(NSString *)colorWithNumber:(IColor)number;
+(NSString *)colorWithNumber:(IColor)number;
@end
#pragma mark 实现
@implementation IPhone
-(void)about
{
// char *name;
/*
NSString *name;
switch (_color) {
case 0:
name = @"黑色";
break;
case 1:
name = @"白色";
break;
case 2:
name = @"土豪金色";
break;
default:
name = @"五颜六色";
break;
}
*/
// IPhone *p = [IPhone new];
// NSString *name = [p colorWithNumber:_color];
NSString *name = [IPhone colorWithNumber:_color];
// NSLog(@"型号 = %f cpu = %i,尺寸 = %f, 颜色 =%i",_model,_cpu,_size,_color);
NSLog(@"型号 = %f cpu = %i,尺寸 = %f, 颜色 =%@",_model,_cpu,_size,name);
}
// 工具方法, 根据传入的整数 返回对应的字符串
//-(NSString *)colorWithNumber:(IColor)number
+(NSString *)colorWithNumber:(IColor)number
{
NSString *name;
switch (number) {
case 0:
name = @"黑色";
break;
case 1:
name = @"白色";
break;
case 2:
name = @"土豪金色";
break;
default:
name = @"五颜六色";
break;
}
return name;
}
-(char *)loadMessage
{
char *content = "老婆我们家我做主";
return "lyh is cool";
}
-(int) signal:(int)number
{
NSLog(@"打电话 %i",number);
return 1;
}
- (int)sendMessageWithNumber:(int)number andContent:(char *)content
{
NSLog(@"发短息 %i, 内容: %s",number,content);
return 1;
}
+(int)sumWithValue1:(int)v1 andValue2:(int)v2;
{
IPhone *p = [IPhone new];
[p signal:123];
return v1 + v2;
}
+(void)demo
{
[IPhone sumWithValue1:30 andValue2:40];
}
@end
#pragma mark main函数
void text();
int main(int argc, const char * argv[])
{
// 1.创建对象
// 每次给iPhone类 发送一个new消息 都会开辟一块新的存储空间
// 也就 意味着每次发送new 消息 创建出来的对象 都是一个新的对象
IPhone *p = [IPhone new];
p->_color = kIColorWhite;
p->_cpu = 1;
p->_size = 3.5;
p->_model = 4;
[p about];
return 0;
}
void text()
{
IPhone *p1 = [IPhone new];
int res = [IPhone sumWithValue1:10 andValue2:20];
NSLog(@"res = %i",res);
}