第一个OC类的实现
                                                                
                17-12-15 15:07
                字数 1438
                阅读 3341
                                已编辑
                                            
            #import <Foundation/Foundation.h>  
// 1.类的声明 @interface 类名 @end结尾  
// 类名的首字母必须大写  
@interface Iphone : NSObject  
{  
    // 属性  
    // 类的属性默认是protect修饰的  
    @public  
    float _model;// _开头  
    int _cpu;  
    double _size;  
    int _color;  
}  
// 方法  
// 没有参数不用写() 直接写; 因为()是用来扩住数据类型的  
// OC 中类方法用 + 表示(只能类调用) 对象方法用 - 表示(只能对象调用)  
- (void)about;  
- (char *)loadMessage;  
// OC中有参数要在数据类型前加 :  
// :是方法名称的一部分  
- (int)callPhone:(int)number;  
- (int)sendMessage:(int)number :(char *) content;  
// 为了提高代码阅读性,OC允许给每个参数添加标签  
- (int)sendMessageWithNumber:(int)number andContent:(char *) content;  
@end  
// 2.类的实现  
@implementation Iphone  
- (void)about {  
    NSLog(@"型号: %f, cpu: %d, 尺寸: %f, 颜色: %i", _model, _cpu, _size, _color);  
}  
-(char *)loadMessage {  
    return "电话已欠费,请及时缴费。";  
}  
- (int)callPhone:(int)number {  
    NSLog(@"打电话给%i", number);  
    return 1;  
}  
- (int)sendMessage:(int)number :(char *) content {  
    NSLog(@"发短信给%i,内容%s", number, content);  
    return 1;  
}  
- (int)sendMessageWithNumber:(int)number andContent:(char *) content {  
    NSLog(@"发短信给%i,内容%s", number, content);  
    return 1;  
}  
@end  
int main(int argc, const char * argv[]) {  
    // 通过类创建对象  
    // 在oc中要通过类创建对象 必须给类发送一个消息(好比c语言中调用方法)  
    // 发送消息 在OC中要发送消息先写[类名/对象名 方法名称];  
    // 调用new方法后会做三件事  
    // 1.给对象分配内存空间  
    // 2.初始化对象属性  
    // 3.返回对象地址  
    // 通过一个Iphone类型的指针接受Iphone对象的地址  
    Iphone *p = [Iphone new];  
    // p 即为Iphone对象  
    // oc中的类本质是结构体  
    p->_size = 4.5;  
    p->_model = 7;  
    p->_cpu = 10;  
    p->_color = 2;  
    // 打印对象属性  
    NSLog(@"color = %d, model = %f,size = %lf, cpu=%d", p->_color, p->_model, p->_size, p->_cpu);  
    // 调用对象方法  
    [p about];  
    char *content = [p loadMessage];  
    NSLog(@"content = %s", content);  
    [p callPhone:159011053];  
    [p sendMessage:122331 :"hello world"];  
    [p sendMessageWithNumber:12132131 andContent:"good luck"];  
    return 0;  
}  这些- + [] 真是反人类啊
0人点赞>
                    
                0 条评论
            
            
                排序方式
                时间
                投票
            
        快来抢占一楼吧
    请登录后发表评论
        
        相关推荐
        
    文章归档
    2025-09
                
                    1 篇
                
            
                    
                2025-08
                
                    7 篇
                
            
                    
                2025-07
                
                    1 篇
                
            
                    
                2025-05
                
                    2 篇
                
            
                    
                2024-11
                
                    1 篇
                
            
                                展开剩余 72 条
                            
                    2024-06
                    
                        1 篇
                    
                
                            
                    2024-05
                    
                        2 篇
                    
                
                            
                    2024-04
                    
                        2 篇
                    
                
                            
                    2024-03
                    
                        2 篇
                    
                
                            
                    2024-01
                    
                        1 篇
                    
                
                            
                    2023-10
                    
                        1 篇
                    
                
                            
                    2023-09
                    
                        1 篇
                    
                
                            
                    2023-08
                    
                        1 篇
                    
                
                            
                    2023-06
                    
                        1 篇
                    
                
                            
                    2023-04
                    
                        1 篇
                    
                
                            
                    2022-12
                    
                        2 篇
                    
                
                            
                    2022-06
                    
                        1 篇
                    
                
                            
                    2022-04
                    
                        4 篇
                    
                
                            
                    2022-03
                    
                        3 篇
                    
                
                            
                    2022-01
                    
                        6 篇
                    
                
                            
                    2021-12
                    
                        2 篇
                    
                
                            
                    2021-11
                    
                        2 篇
                    
                
                            
                    2021-10
                    
                        2 篇
                    
                
                            
                    2021-09
                    
                        1 篇
                    
                
                            
                    2021-08
                    
                        2 篇
                    
                
                            
                    2021-07
                    
                        4 篇
                    
                
                            
                    2021-06
                    
                        1 篇
                    
                
                            
                    2021-05
                    
                        3 篇
                    
                
                            
                    2021-04
                    
                        3 篇
                    
                
                            
                    2021-01
                    
                        2 篇
                    
                
                            
                    2020-11
                    
                        1 篇
                    
                
                            
                    2020-10
                    
                        3 篇
                    
                
                            
                    2020-09
                    
                        2 篇
                    
                
                            
                    2020-08
                    
                        1 篇
                    
                
                            
                    2020-07
                    
                        5 篇
                    
                
                            
                    2020-06
                    
                        5 篇
                    
                
                            
                    2020-05
                    
                        1 篇
                    
                
                            
                    2020-04
                    
                        1 篇
                    
                
                            
                    2020-03
                    
                        2 篇
                    
                
                            
                    2020-02
                    
                        3 篇
                    
                
                            
                    2020-01
                    
                        1 篇
                    
                
                            
                    2019-11
                    
                        5 篇
                    
                
                            
                    2019-10
                    
                        10 篇
                    
                
                            
                    2019-09
                    
                        12 篇
                    
                
                            
                    2019-08
                    
                        17 篇
                    
                
                            
                    2019-07
                    
                        8 篇
                    
                
                            
                    2019-05
                    
                        3 篇
                    
                
                            
                    2019-04
                    
                        8 篇
                    
                
                            
                    2019-03
                    
                        7 篇
                    
                
                            
                    2019-02
                    
                        8 篇
                    
                
                            
                    2019-01
                    
                        5 篇
                    
                
                            
                    2018-12
                    
                        7 篇
                    
                
                            
                    2018-11
                    
                        8 篇
                    
                
                            
                    2018-10
                    
                        4 篇
                    
                
                            
                    2018-09
                    
                        7 篇
                    
                
                            
                    2018-08
                    
                        12 篇
                    
                
                            
                    2018-07
                    
                        9 篇
                    
                
                            
                    2018-06
                    
                        6 篇
                    
                
                            
                    2018-05
                    
                        11 篇
                    
                
                            
                    2018-04
                    
                        18 篇
                    
                
                            
                    2018-03
                    
                        1 篇
                    
                
                            
                    2018-02
                    
                        2 篇
                    
                
                            
                    2018-01
                    
                        10 篇
                    
                
                            
                    2017-12
                    
                        14 篇
                    
                
                            
                    2017-11
                    
                        44 篇
                    
                
                            
                    2017-10
                    
                        13 篇
                    
                
                            
                    2017-09
                    
                        4 篇
                    
                
                            
                    2017-08
                    
                        12 篇
                    
                
                            
                    2017-07
                    
                        5 篇
                    
                
                            
                    2017-06
                    
                        4 篇
                    
                
                            
                    2017-05
                    
                        2 篇
                    
                
                            
                    2017-04
                    
                        3 篇
                    
                
                            
                    2017-03
                    
                        9 篇
                    
                
                            
                    2017-02
                    
                        3 篇
                    
                
                            
                    2017-01
                    
                        2 篇
                    
                
                            
                    2016-12
                    
                        10 篇
                    
                
                            
                    2016-11
                    
                        4 篇
                    
                
                        最新文章
        最受欢迎
    09-04 16:48
                    08-26 17:01
                    08-26 00:10
                    08-16 00:23
                    08-15 16:15
                    13 评论
                    11 评论
                    10 评论
                     
         
                 
    