//
// main.m
// 第一个OC类-类方法
#import <Foundation/Foundation.h>
// 1.编写类的声明
@interface Iphone : NSObject
{
@public
float _model; // 型号 0
int _cpu; // cup 0
double _size; // 尺寸 0
int _color; // 颜色 0
}
- (void)about;
- (char *)loadMessage;
- (int)signal:(int)number;
- (int)sendMessageWithNumber:(int)number andContent:(char *)content;
// 计算器
// 并且如果该方法中没有使用到属性(成员变量), 那么你可以把这个方法定义为类方法
//- (int)sumWithValue1:(int)value1 andValue2:(int)value2;
// 如果定义类方法, 类方法的写法和对象方法一模一样, 只需要将对象方法的-号换成+, 那么就定义了一个类方法
+ (int)sumWithValue1:(int)value1 andValue2:(int)value2;
+ (void)demo;
@end
// 2.编写类的实现
@implementation Iphone
// 行为的实现
- (void)about
{
NSLog(@"%s", [self loadMessage]);
}
- (char *)loadMessage
{
return "wife is god";
}
- (int)signal:(int)number
{
NSLog(@"打电话给%i", number);
return ;
}
- (int)sendMessageWithNumber:(int)number andContent:(char *)content
{
NSLog(@"发短信给%i, 内容是%s", number, content);
return ;
}
// 注意: 如果声明的是对象方法那么就必须实现对象方法
// 如果声明的是类方法那么就必须实现类方法 /*
类方法和对象方法的区别
0. 对象方法以-开头
类方法以+开头
1. 对象方法必须用对象调用
类方法必须用类来调用
2. 对象方法中可以直接访问属性(成员变量)
类方法中不可以直接访问属性(成员变量)
3. 类方法和对象方法可以进行相互调用
4.1对象方法中可以直接调用类方法
4.2类方法中间接调用对象方法 (注意: 不建议这样使用)
4.3类方法中可以直接调用其它类方法
4.4对象方法中可以直接调用对象方法 如果方法中没有使用到属性(成员变量), 那么能用类方法就用类方法. 类方法的执行效率比对象方法高。 类方法一般用于定义工具方法:字符串查找,文件操作,数据库操作。
*/
+ (int)sumWithValue1:(int)value1 andValue2:(int)value2
{
NSLog(@"型号 = %f, cpu = %i, 尺寸= %f, 颜色 = %i", _model, _cpu, _size, _color);
Iphone *p = [Iphone new];
[p signal:]; // 注意: 在企业开发中, 不建议这样使用
return value1 + value2;
}
+ (void)demo
{
NSLog(@"%i", [Iphone sumWithValue1: andValue2:]);
NSLog(@"demo");
}
@end int main(int argc, const char * argv[]) {
// 1.通过类创建对象
Iphone *p = [Iphone new];
// 2.修改对象的属性
p->_size = 3.5;
p->_color = ;
p->_model = ;
p->_cpu = ;
// 3.如果给对象发消息(如果调用对象的方法)
[p about];
char *content = [p loadMessage];
NSLog(@"content = %s", content);
[p signal:];
[p sendMessageWithNumber: andContent:"hi"];
// int res = [p sumWithValue1:10 andValue2:20];
int res = [Iphone sumWithValue1: andValue2:];
NSLog(@"res = %i", res);
[Iphone signal:];
Iphone *p = [Iphone new];
// int res = [p sumWithValue1:10 andValue2:20];
// [p about];
// [Iphone sumWithValue1:10 andValue2:20];
[p demo];
[p about];
return ;
} void test()
{
// 1.创建一个对象
/*
开辟一块存储空间
*/
// Iphone *p1 = [Iphone new];
// 2.利用对象调用加法运算方法
// int res = [p1 sumWithValue1:50 andValue2:50];
int res = [Iphone sumWithValue1: andValue2:];
NSLog(@"res = %i", res); }
//
// main.m
// 第一个OC类-类方法2
#import <Foundation/Foundation.h> typedef enum
{
kIColorBlack, //0, 黑色
kIColorWhite, //1, 白色
kIColorTuHaoJin //2, 土豪金
} IColor; //写枚举一般写别名 // 1.编写类的声明
@interface Iphone : NSObject
{
@public
float _model; // 型号 0
int _cpu; // cup 0
double _size; // 尺寸 0
// int _color; // 颜色 0代表黑色 1代表白色 2土豪金
// enum IColor _color;
IColor _color;
}
- (void)about;
- (char *)loadMessage;
- (int)signal:(int)number;
- (int)sendMessageWithNumber:(int)number andContent:(char *)content;
+ (int)sumWithValue1:(int)value1 andValue2:(int)value2;
//- (NSString *)colorWithNumber:(IColor)number;
+ (NSString *)colorWithNumber:(IColor)number;
@end // 2.编写类的实现
@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;
}
// NSLog(@"型号 = %f, cpu = %i, 尺寸= %f, 颜色 = %i", _model, _cpu, _size, _color);
*/
// Iphone *p = [Iphone new];
// NSString *name = [p colorWithNumber:_color];
NSString *name = [Iphone colorWithNumber:_color];
NSLog(@"型号 = %f, cpu = %i, 尺寸= %f, 颜色 = %@", _model, _cpu, _size, name);
} // 工具方法, 根据传入的整数返回对应的字符串
//- (NSString *)colorWithNumber:(IColor)number
+ (NSString *)colorWithNumber:(IColor)number;
{
NSString *name;
switch (number) {
case : //枚举是0,1,2
name = @"黑色";
break;
case :
name = @"白色";
break;
case :
name = @"土豪金";
break;
default:
name = @"华强北";
break;
}
return name;
} - (char *)loadMessage
{
return "wife is god";
} - (int)signal:(int)number
{
NSLog(@"打电话给%i", number);
return ;
} - (int)sendMessageWithNumber:(int)number andContent:(char *)content
{
NSLog(@"发短信给%i, 内容是%s", number, content);
return ;
} + (int)sumWithValue1:(int)value1 andValue2:(int)value2
{
return value1 + value2;
} @end int main(int argc, const char * argv[]) {
// 1.创建对象
// 每次给Iphone类发送一个new消息都会开辟一块新的存储空间
// 也就意味着每次发送new消息创建出来的对象都是一个新的对象
Iphone *p = [Iphone new];
p->_color = kIColorWhite; //给枚举赋值是直接写枚举值
p->_cpu = ;
p->_model = ;
p->_size = 3.5;
// 2.给对象发送消息
[p about];
return ;
}

最新文章

  1. static cross compile gtk-2.16.6+gtk-directfb+arm-linux (arm-linux-gcc-3.4.4+glib-2.3.5)
  2. 理解RxJava线程模型
  3. python中的最最最基本语法(1)
  4. Linux下用C读取配置文件。类似ini这样。
  5. (hdu)2444 The Accomodation of Students 判断二分图+最大匹配数
  6. FSG1.33解压缩算法分析
  7. windows CMD.exe下写路径太长的解决方案
  8. 基于visual Studio2013解决面试题之0303数组求和
  9. coco2d-x 基于视口的地图设计
  10. Xcode 5.1.1 与 Xcode 6.0.1 共存
  11. 用GeneratedKeyHolder获得新建数据主键值
  12. 关于rem的使用和less编译工具考拉
  13. layedit第三次改造
  14. MUI的一些笔记
  15. 【译】第9节---EF Code First中数据注解
  16. 利用pt-table-checksum校验数据一致性
  17. .NET 4并行编程入门之Task的取消[转]
  18. C++ 0x std::async 的应用
  19. jump game(贪心算法)
  20. oracle之 关闭透明大页

热门文章

  1. java selenium启动火狐浏览器报错:Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: VISTA Build info: version: &#39;3.8.1&#39;, revision: &#39;6e95a6684b&#39;, time: &#39;2017-12-01T19:05:14.666Z
  2. collectionView必须点击两次才跳转
  3. 手动触发dom节点事件代码
  4. java ---书写自己的名字
  5. 国内DNS服务器地址
  6. 阿里巴巴矢量库IconFont__使用小录
  7. C# tostring(&quot;0000000&quot;)
  8. spring学习地址
  9. List集合的特有功能概述和测试
  10. Django MVC与MTV概念 Ajax、分页实现