設計一部iphone手機用面向對象的方法
阿新 • • 發佈:2017-06-04
快捷 [] print 成員 log cpu 變量 public ram
main.m
//編輯字體大小command + < //編譯執行快捷鍵 com + R #import <Foundation/Foundation.h> #import "Iphone.h" int main(int argc, const char * argv[]) { Iphone * phone = [Iphone new]; phone->_ram = 512; phone->_cpu = 1.5; int year = 2015; int month = 7; int day = 15; NSLog(@"Hello, World %d %d %d %@!",year,month,day,@"今天開學了"); printf("hello world %d %d %d\n",year,month,day); return 0; }
iphone.h
#import <Foundation/Foundation.h> @interface Iphone : NSObject { [email protected] 被修飾的成員變量,在類的外部能夠直接訪問 @public float _cpu;//用來存儲CPU的大小 float _ram;//用來存儲內部容量大小 }//專門用來聲明成員變量 - (void)aboutMyPhone; -(void)sendMessag:(char *)content; -(void)sendMessag:(char *)content andPhoneNumber:(char *)phoneNumber; -(void)sendMessagWithMessageContent:(char *)messageContent andPhoneNumber:(char *)phoneNumber; //2.類方法 @end
iphone.m
#import "Iphone.h" @implementation Iphone -(void)aboutMyPhone{ // NSLog(@"aboutMyPhone已經實現了"); NSLog(@"phone cpu %.2f,ram %.2f",_cpu,_ram); } -(void)sendMessag:(char *)content { NSLog(@"%s",content); } -(void)sendMessag:(char *)content andPhoneNumber:(char *)phoneNumber { NSLog(@"給%s發送消息%s",phoneNumber,content); } -(void)sendMessagWithMessageContent:(char *)messageContent andPhoneNumber:(char *)phoneNumber { NSLog(@"給%s發送消息%s",phoneNumber,messageContent); } //重寫description方法 -(NSString *)description { //優化 return [NSString stringWithFormat:@"我的cpu=%.1f我的內部存儲是=%.1fMB",_cpu,_ram]; NSString *result = [NSString stringWithFormat:@"我的cpu=%.1f我的內部存儲是=%.1fMB",_cpu,_ram]; return result; return @"ffffffff"; } @end
設計一部iphone手機用面向對象的方法