OC语言注意事项

来源:互联网 发布:局域网办公软件 编辑:程序博客网 时间:2024/05/01 11:58






/*
 方法
 1.对象方法都是以减号 - 
 2.对象方法的声明必须写在@interface和@end之间
   对象方法的实现必须写在@implementation和@end之间
 3.对象方法只能由对象来调用
 4.对象方法归类\对象所有
 
 函数
 1.函数能写在文件中的任意位置(@interface和@end之间除外),函数归文件所有
 2.函数调用不依赖于对象
 3.函数内部不能直接通过成员变量名访问某个对象的成员变量
 
 */


#import <Foundation/Foundation.h>

// :表示继承,只能是单继承

@interface Person : NSObject

- (void)test:  / /对象方法的声明

    

@end


@implementation Person

- (void)test{  // 对象方法的实现


insert code in here........

}

@end


@interface Car : NSObject
{// 成员变量\实例变量
    //int wheels = 4; 不允许在这里初始化
    //static int wheels; 不能随便将成员变量当做C语言中的变量来使用
    @public
    int wheels;
}


- (void)run;
- (void)fly;
@end


int main()
{
    // wheels = 10;
    /*

    Car *c = [Car new];//创建一个对象

    c->wheels = 4;//修改成员变量的属性值
    //run();
    
    [c run];  //调用对象方法
    */
    

    void test2(); /


    
    test2();
    
    return 0;
}


@implementation Car


- (void) fly
{
    
}


/*
void test2()
{
    NSLog(@"调用了test2函数-%d", wheels);
}*/


void test()
{
    NSLog(@"调用了test函数");
}


- (void)run
{
    test();
    NSLog(@"%d个轮子的车跑起来了", wheels);
}
@end
0 0
原创粉丝点击