日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Object-c 类、对象方法

發(fā)布時(shí)間:2024/1/17 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Object-c 类、对象方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Object-c 類、對(duì)象方法

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 屬性:型號(hào)、cpu,尺寸,顏色 22 23 行為:打電話 發(fā)短信,上網(wǎng) 24 25 */ 26 27 //1、編寫類的申明 28 29 30 31 32 33 34 35 36 37 //####################################################### 38 39 //C語言:定義函數(shù)分為:聲明和實(shí)現(xiàn) 40 41 void about1(); 42 43 void about2() //定義 44 45 { 46 47 //實(shí)現(xiàn) 48 49 printf("使用C語言打印本機(jī)信息\n"); 50 51 } 52 53 //####################################################### 54 55 56 @interface Iphone :NSObject //此處NSObject (super class) 是為了讓我們的Iphone 類具備創(chuàng)建對(duì)象的能力 57 58 { 59 60 //Instance variable is protected,只有讓類中的屬性公開后就可以直接通過一個(gè)指向結(jié)構(gòu)體的指針來操作對(duì)象中的屬性 61 62 @public //公共的,屬性公開后其它對(duì)象可訪問 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語言中的內(nèi)部函數(shù),外部函數(shù))- 表示對(duì)象方法,只能通過指針調(diào)用;+ 表示類方法 75 76 77 78 // 沒有返回值沒有參數(shù)的; 79 80 - (void) about; //無返回值,沒有形參不需要用(), 國為OC中()是特殊用法, 用來括住數(shù)據(jù)類型,故此 about 方法后面不需要() 81 82 83 84 /* 85 86 C語言函數(shù),同樣OC的方法也有這四類: 87 88 沒有返回值沒有參數(shù)的; 89 90 有返回值沒有參數(shù)的; 91 92 有返回值有參數(shù)的; 93 94 沒有返回值有參數(shù)的 95 96 */ 97 98 99 100 //LoadMessage 方法,有返回值沒有參數(shù)的,讀取消息 101 102 - (char *)LoadMessage; 103 104 105 106 //有返回值有參數(shù)的,打電話功能 107 108 //OC中如果方法有參數(shù),那個(gè)每個(gè)參數(shù)的數(shù)據(jù)類型前必須加上一個(gè) 冒號(hào): 109 110 - (int)Call:(long int)number; 111 112 113 114 //有多個(gè)參數(shù)的方法:發(fā)短信,給一個(gè)手機(jī)號(hào)發(fā)內(nèi)容為xxxx 的短信 115 116 // 此方法的名稱為 :sendMessage:: 117 118 - (int)sendMessage:(int)number :(char *)content; 119 120 121 122 //為了提高寫代碼過程中的閱讀性,OC方法允許我們給每個(gè)參數(shù)添加一個(gè)標(biāo)簽 來說明 當(dāng)前參數(shù)的含義,以致在方法所帶參數(shù)太多情況下,方法調(diào)用時(shí)候不知道參數(shù)意義是什么。。。在參數(shù)多的情況下,方法名稱可很長,將方法名翻譯過來即可明白 方法實(shí)現(xiàn)的什么功能 123 124 //此時(shí)標(biāo)簽也是方法名的一部分,方法名為 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、編寫類的實(shí)現(xiàn),@implementation開頭,end 結(jié)尾 139 140 @implementation Iphone 141 142 //行為的實(shí)現(xiàn),類的實(shí)現(xiàn) 143 144 - (void)about 145 146 { 147 148 NSLog(@"使用Object-C 打印本機(jī)信息"); 149 150 //在對(duì)象方法中訪問該對(duì)象的屬性,可直接訪問,寫上 _對(duì)象名 即可,比如:_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 //返回短信內(nèi)容 163 164 // return "我家我做主"; //NSLog 對(duì)C語言支持性不是很好,輸出漢字會(huì)亂碼或空白 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發(fā)短信:%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(@"發(fā)內(nèi)容:%s給%ld",content,number); 201 202 return 1; 203 204 } 205 206 207 208 //有參數(shù) 無返回值 方法: 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 //通過類創(chuàng)建 對(duì)象 231 232 Iphone *p = [Iphone new]; 233 234 235 236 // OC中的類其實(shí)本質(zhì)就是一個(gè)結(jié)構(gòu)休,所以 p 這個(gè)指針其實(shí)就是指向了一個(gè)結(jié)構(gòu)體 237 238 //獲取對(duì)象的屬性 239 240 NSLog(@"model=%d,cpu=%i,size=%d,color=%i",p->_model,p->_cpu,p->_size,p->_color);//初始值全 為 0; 241 242 //個(gè)性對(duì)象的屬 243 244 /* 245 246 (*p)._color = 5; 247 248 NSLog(@"color=%i",(*p)._color); 249 250 p->_model = 6; //(*p)._model = 6; 也可以這樣調(diào)用 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 函數(shù)調(diào)用 263 264 about2(); 265 266 267 268 //OC:給對(duì)象發(fā)消息:方法的調(diào)用 269 270 [p about]; 271 272 //[對(duì)象 消息(方法名稱)],對(duì)象方法只能對(duì)象調(diào)用 273 274 char * content = [p LoadMessage]; 275 276 NSLog(@"短信內(nèi)空為:%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 } View Code

?

?

輸出結(jié)果:

2016-03-14 23:50:12.943 firestMethod2[2414:114978] model=0,cpu=0,size=0,color=0

使用C語言打印本機(jī)信息

2016-03-14 23:50:12.944 firestMethod2[2414:114978] 使用Object-C 打印本機(jī)信息

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] 短信內(nèi)空為: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發(fā)短信:good good study , day day up !

2016-03-14 23:50:12.944 firestMethod2[2414:114978] 11111111發(fā)短信:Fuck You

2016-03-14 23:50:12.944 firestMethod2[2414:114978] 發(fā)內(nèi)容: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

?

?

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/aleuxqin/p/5277759.html

總結(jié)

以上是生活随笔為你收集整理的Object-c 类、对象方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。