Object-c 类、对象方法
1 // 2 3 // main.m 4 5 // firestMethod2 6 7 // 8 9 // Created by AleuxQ on 16/3/14. 10 11 // Copyright © 2016年 AleuxQ. All rights reserved. 12 13 // 14 15 #import <Foundation/Foundation.h> 16 17 /* 18 19 事物名称:iphone 20 21 属性:型号、cpu,尺寸,颜色 22 23 行为:打电话 发短信,上网 24 25 */ 26 27 //1、编写类的申明 28 29 30 31 32 33 34 35 36 37 //####################################################### 38 39 //C语言:定义函数分为:声明和实现 40 41 void about1(); 42 43 void about2() //定义 44 45 { 46 47 //实现 48 49 printf("使用C语言打印本机信息\n"); 50 51 } 52 53 //####################################################### 54 55 56 @interface Iphone :NSObject //此处NSObject (super class) 是为了让我们的Iphone 类具备创建对象的能力 57 58 { 59 60 //Instance variable is protected,只有让类中的属性公开后就可以直接通过一个指向结构体的指针来操作对象中的属性 61 62 @public //公共的,属性公开后其它对象可访问 63 64 int _model; 65 66 int _cpu; 67 68 int _size; 69 70 int _color; //黑或白,用 0 或 1 表示 71 72 } 73 74 //OC中定义方法,在类的声明中定义,在 end 之前,(类似C语言中的内部函数,外部函数)- 表示对象方法,只能通过指针调用;+ 表示类方法 75 76 77 78 // 没有返回值没有参数的; 79 80 - (void) about; //无返回值,没有形参不需要用(), 国为OC中()是特殊用法, 用来括住数据类型,故此 about 方法后面不需要() 81 82 83 84 /* 85 86 C语言函数,同样OC的方法也有这四类: 87 88 没有返回值没有参数的; 89 90 有返回值没有参数的; 91 92 有返回值有参数的; 93 94 没有返回值有参数的 95 96 */ 97 98 99 100 //LoadMessage 方法,有返回值没有参数的,读取消息 101 102 - (char *)LoadMessage; 103 104 105 106 //有返回值有参数的,打电话功能 107 108 //OC中如果方法有参数,那个每个参数的数据类型前必须加上一个 冒号: 109 110 - (int)Call:(long int)number; 111 112 113 114 //有多个参数的方法:发短信,给一个手机号发内容为xxxx 的短信 115 116 // 此方法的名称为 :sendMessage:: 117 118 - (int)sendMessage:(int)number :(char *)content; 119 120 121 122 //为了提高写代码过程中的阅读性,OC方法允许我们给每个参数添加一个标签 来说明 当前参数的含义,以致在方法所带参数太多情况下,方法调用时候不知道参数意义是什么。。。在参数多的情况下,方法名称可很长,将方法名翻译过来即可明白 方法实现的什么功能 123 124 //此时标签也是方法名的一部分,方法名为 sendMessageWithNumber:AndContent: 125 126 - (int)sendMessageWithNumber:(long int)number AndContent:(char *)content; 127 128 129 130 - (void)lookWithNumber:(long int)number AndContent:(char *)content; 131 132 133 134 @end 135 136 137 138 //1、编写类的实现,@implementation开头,end 结尾 139 140 @implementation Iphone 141 142 //行为的实现,类的实现 143 144 - (void)about 145 146 { 147 148 NSLog(@"使用Object-C 打印本机信息"); 149 150 //在对象方法中访问该对象的属性,可直接访问,写上 _对象名 即可,比如:_model,_cpu 151 152 NSLog(@"mode = Iphone %d,cpu = %d,size = %d,color = %d",_model,_cpu,_size,_color); 153 154 } 155 156 157 158 - (char *)LoadMessage 159 160 { 161 162 //返回短信内容 163 164 // return "我家我做主"; //NSLog 对C语言支持性不是很好,输出汉字会乱码或空白 165 166 return "Wife is God !"; 167 168 } 169 170 171 172 - (int)Call:(long int)number 173 174 { 175 176 NSLog(@"打电话给%ld",number); 177 178 return 1; 179 180 } 181 182 183 184 - (int) sendMessage:(int)number :(char *)content 185 186 { 187 188 NSLog(@"给%d发短信:%s",number,content); 189 190 return 1; 191 192 } 193 194 195 196 - (int)sendMessageWithNumber:(long int)number AndContent:(char *)content 197 198 { 199 200 NSLog(@"发内容:%s给%ld",content,number); 201 202 return 1; 203 204 } 205 206 207 208 //有参数 无返回值 方法: 209 210 - (void)lookWithNumber:(long)number AndContent:(char *)content 211 212 { 213 214 NSLog(@"look number is %ld,Content is %s",number,content); 215 216 } 217 218 219 220 @end 221 222 223 224 225 226 int main(int argc, const char * argv[]) { 227 228 229 230 //通过类创建 对象 231 232 Iphone *p = [Iphone new]; 233 234 235 236 // OC中的类其实本质就是一个结构休,所以 p 这个指针其实就是指向了一个结构体 237 238 //获取对象的属性 239 240 NSLog(@"model=%d,cpu=%i,size=%d,color=%i",p->_model,p->_cpu,p->_size,p->_color);//初始值全 为 0; 241 242 //个性对象的属 243 244 /* 245 246 (*p)._color = 5; 247 248 NSLog(@"color=%i",(*p)._color); 249 250 p->_model = 6; //(*p)._model = 6; 也可以这样调用 251 252 p->_cpu = 2; 253 254 p->_size = 4.7; 255 256 p->_color = 1; 257 258 NSLog(@"model=%d,cpu=%i,size=%d,color=%i",p->_model,p->_cpu,p->_size,p->_color); 259 260 */ 261 262 //C 函数调用 263 264 about2(); 265 266 267 268 //OC:给对象发消息:方法的调用 269 270 [p about]; 271 272 //[对象 消息(方法名称)],对象方法只能对象调用 273 274 char * content = [p LoadMessage]; 275 276 NSLog(@"短信内空为:%s",content); 277 278 279 280 [p Call:18688888888]; 281 282 [p sendMessage:18688888 :"good good study , day day up !"]; 283 284 [p sendMessage:11111111 :"Fuck You"]; 285 286 287 288 [p sendMessageWithNumber:18888888888 AndContent:"Good,UP UP UP !"]; 289 290 291 292 [p lookWithNumber:18888888888 AndContent:"Addd"]; 293 294 295 296 // insert code here... 297 298 NSLog(@"This is the End !"); 299 300 301 302 303 304 return 0; 305 306 }
输出结果:
2016-03-14 23:50:12.943 firestMethod2[2414:114978] model=0,cpu=0,size=0,color=0
使用C语言打印本机信息
2016-03-14 23:50:12.944 firestMethod2[2414:114978] 使用Object-C 打印本机信息
2016-03-14 23:50:12.944 firestMethod2[2414:114978] mode = Iphone 0,cpu = 0,size = 0,color = 0
2016-03-14 23:50:12.944 firestMethod2[2414:114978] 短信内空为:Wife is God !
2016-03-14 23:50:12.944 firestMethod2[2414:114978] 打电话给18688888888
2016-03-14 23:50:12.944 firestMethod2[2414:114978] 给18688888发短信:good good study , day day up !
2016-03-14 23:50:12.944 firestMethod2[2414:114978] 给11111111发短信:Fuck You
2016-03-14 23:50:12.944 firestMethod2[2414:114978] 发内容:Good,UP UP UP !给18888888888
2016-03-14 23:50:12.944 firestMethod2[2414:114978] look number is 18888888888,Content is Addd
2016-03-14 23:50:12.945 firestMethod2[2414:114978] This is the End !
Program ended with exit code: 0