OC基础第二天
2019獨角獸企業重金招聘Python工程師標準>>>
FJMobilePhone.h#import <Foundation/Foundation.h>
@interface FJMobilePhone : NSObject{
?? ?
? ? NSString *_brand;//手機牌子
? ? double _price;//手機價格
? ? double _screenSize;//手機屏幕大小
? ? NSString *_color;//手機顏色
? ? NSString *_owner;//手機主人
? ? NSString *_number;//手機號碼
? ? NSDictionary *_contacts;//這是一個‘字典’后面會有介紹
? ? //簡單來說字典就是存儲鍵值對的集合。這里保存的是一個號碼對應一個手機主人
}
/**初始化方法*/
+ (instancetype)phoneWithBrand:(NSString *) brand owner:(NSString *)owner
? ? ? ? ? ? ? ? ? ? ? ? number:(NSString *)number;
- (instancetype) initWithBrand:(NSString *)brand owner:(NSString *)owner
? ? ? ? ? ? ? ? ? ? ? ? number:(NSString *)number;
/**定義一個手機打電話的方法*/
- (BOOL) call : (NSString *) otherNumber;
/**定義一個手機發短信的功能*/
- (void) sendMessage:(NSString *) message to:(NSString *) otherNumber;
@end
FJMobilePhone.m
#import?"FJMobilePhone.h"
@implementation?FJMobilePhone
+ (instancetype) phoneWithBrand:(NSString?*)brand owner:(NSString?*)owner number:(NSString?*)number{
?? ?
? ??return?[[self?alloc]initWithBrand:(NSString?*)brand?owner:(NSString?*)owner?number:(NSString?*)number];
}
- (instancetype)initWithBrand:(NSString?*)brand owner:(NSString?*)owner number:(NSString?*)number{
?? ?
? ??if(self?= [super?init]){
? ? ? ??_brand?= brand;
? ? ? ??_owner?=owner;
? ? ? ??_number?= number;
? ? ? ??_contacts?=?@{@"13799322422":@"鹿晗",
? ? ? ? ? ? ? ? ? ? ??@"28774198274":@"大腿",
? ? ? ? ? ? ? ? ? ? ??@"37597293729":@"微微",
? ? ? ? ? ? ? ? ? ? ??};//這就是字典的用法?前面是鍵key后面是值value通過key可以找到鍵
? ? }
? ??return?self;
}
- (BOOL)call:(NSString?*)otherNumber{
? ??int?rate =?arc4random() %10?+1;//產生一個1到10的隨意數
? ??if(rate <?7){
? ? ? ??NSString?*otherName =?_contacts[otherNumber];//這里的意思是通過otherNumber找到一個otherName
? ? ? ??//字典的索引器語法:通過key找到value,如果給定的key沒有對應的value返回nil
?? ? ? ?
? ? ? ??printf("%s正在和%s煲電話粥.......\n",[_owner?UTF8String],otherName?
?? ? ? ? ? ? ? [otherName?UTF8String]:[otherNumber?UTF8String]);
? ? ? ??//[otherNumber UTF8String]由于printf是C語言的語法這里需要把C的字符串轉化成OC的字符
? ? ? ??// UTF8String就是把C的字符串轉化成OC的字符
? ? ? ??return?YES;
?? ? ? ?
? ? }
? ??return??NO;
?? ?
}
- (void)sendMessage:(NSString?*)message to:(NSString?*)otherNumber{
?? ?
? ??NSString?*otherName =?_contacts?[otherNumber];
? ??printf("%s給%s發送短信,?內容是: %s\n", [_owner?UTF8String], otherName? [otherName?UTF8String]: [otherNumber?UTF8String], [message?UTF8String]);
?? ?
}
@end
main.m
#import <Foundation/Foundation.h>
#import "FJMobilePhone.h"
int main(int argc, const char * argv[]) {
? ? @autoreleasepool {
? ? ? ? FJMobilePhone *phone = [FJMobilePhone phoneWithBrand:@"oppo" owner:@"luhan" number:@"7777777"];
?? ?
? ? ? ? [phone call:@"37597293729"];
? ? ? ? [phone sendMessage:@"I LOVE YOU" to:@"37597293729"];
?? ?
? ? }
? ? return 0;
}
程序運行結果截圖:
轉載于:https://my.oschina.net/luhoney/blog/643787
總結
- 上一篇: Activiti 简易教程一 versi
- 下一篇: Bzoj 2749: [HAOI2012