iT邦幫忙

0

iOS APP 開發 OC 第六天, 練習實作一個類

tags: OC 30 day

題目

請依照題目實作一個類:
類名:手機(Phone)
屬性:顏色(_color),大小(_size),_cpu
行為:
查看本機信息(aboutMyPhone),
打電話(call),應該有參數,傳入電話號碼才能打電話。
發短信(sendMessage) 參數:電話號碼和短信內容。

創建對象並測試。

  1. 定義一個類
@interface Phone : NSObject
@end
@implementation Phone
@end
  1. 屬性
@interface Phone : NSObject
{
    NSString *_color;
    float _size;
    NSString *_cpu;
}
    
@end
@implementation Phone
@end
  1. 無參數的方法聲明和實現
@interface Phone : NSObject
{
    NSString *_color;
    float _size;
    NSString *_cpu;
}
 -(void)aboutPhone; 
@end
@implementation Phone
 -(void)aboutPhone
 {
     NSLog(@"顏色:%@ 大小%f CPU %@",_color,_size,_cpu);
 }
@end
@interface Phone : NSObject
{
    @public
    NSString *_color;
    float _size;
    NSString *_cpu;
}
 -(void)aboutPhone;
@end
@implementation Phone
 -(void)aboutPhone
 {
     NSLog(@"顏色:%@ 大小%f CPU %@",_color,_size,_cpu);
 }
@end




int main(int argc, char * argv[]) {
    Phone *iPhone = [Phone new];
    iPhone->_color = @"玫瑰金";
    iPhone->_cpu = @"A9";
    iPhone->_size = 4.7f;
    
    return  0;
}
  1. 帶一個參數的方法聲明和實現
@interface Phone : NSObject
{
    @public
    NSString *_color;
    float _size;
    NSString *_cpu;
}
 -(void)aboutPhone;
-(void)callWithNumber:(NSString *)number;
@end
@implementation Phone
 -(void)aboutPhone
 {
     NSLog(@"顏色:%@ 大小%f CPU %@",_color,_size,_cpu);
 }
-(void)callWithNumber:(NSString *)number;
{
    NSLog(@"正在呼叫 %@",number);
    NSLog(@"對不起您撥的電話已停機。");
}
@end




int main(int argc, char * argv[]) {
    Phone *iPhone = [Phone new];
    iPhone->_color = @"玫瑰金";
    iPhone->_cpu = @"A9";
    iPhone->_size = 4.7f;
    [iPhone aboutPhone];
    [iPhone callWithNumber:@"110"];
    return  0;
}
  1. 帶多個參數的方法聲明和實現
@interface Phone : NSObject
{
    @public
    NSString *_color;
    float _size;
    NSString *_cpu;
}
 -(void)aboutPhone;
-(void)callWithNumber:(NSString *)number;
-(void)sendWithMessage:(NSString *)msg toNumber:(NSString *)number;
@end
@implementation Phone
 -(void)aboutPhone
 {
     NSLog(@"顏色:%@ 大小%f CPU %@",_color,_size,_cpu);
 }
-(void)callWithNumber:(NSString *)number;
{
    NSLog(@"正在呼叫 %@",number);
    NSLog(@"對不起您撥的電話已停機。");
}
-(void)sendWithMessage:(NSString *)msg toNumber:(NSString *)number
{
    NSLog(@"正在向%@發送訊息:%@",number,msg);
    NSLog(@"發送成功!");
}
@end




int main(int argc, char * argv[]) {
    Phone *iPhone = [Phone new];
    iPhone->_color = @"玫瑰金";
    iPhone->_cpu = @"A9";
    iPhone->_size = 4.7f;
    [iPhone aboutPhone];
    [iPhone callWithNumber:@"110"];
    [iPhone sendWithMessage:@"今晚老地方見" toNumber:@"110"];
    return  0;
}


尚未有邦友留言

立即登入留言