Objective_C_01

15/12/4
Objective_C_01
    
1.命名规则
        1.工程名首字母大写
        2.驼峰命名法
        3.实例变量:以“_”开头,首字母小写,驼峰命名法
        4.方法名:首字母小写
        5.变量名首字母都应该小写
        6.写注释!!!!!!!!!!!
    
创建一个Student对象
       Student *stu1 = [[Student alloc] init];
       使用箭头的方式访问实例变量,赋值
        stu1->_age = 18;

#import <Foundation/Foundation.h>
    
    在OC中,接口部分 的关键字是:@interface ...@end
    格式:关键字  类名: 父类名
    @interface MobilePhone : NSObject//声明手机类
    {
        //    类的特征
        //    类的实例变量
    @public
        NSString *_brand;//手机品牌
        CGFloat _price;//手机价格
        NSString *_color;//手机颜色
        NSString *_system;//手机系统
        //   NSString属于OC中的类  NSInteger CGFloat是C语言中的基本数据类型
        //   只要是OC中的类,声明变量的时候都需要添加*号
    }
    //声明当前类的行为
    //OC中的方法(和C语言中的函数一个意思,只是语法改变了)
    -(void)sayHello;
    @end

#import "MobilePhone.h"
    @implementation MobilePhone
    关键字:@implementation...@end
    @implementation 类型
        实现打招呼
    -(void)sayHello{
        NSLog(@"你好:我是%@牌的手机",_brand);
    }
    @end

#import <Foundation/Foundation.h>
    
    @interface Student : NSObject
    {
        //    CGFloat _score;不能放在@public前,无可见度
    @public
        NSString *_name;//姓名
        NSInteger _num;//学号
        NSInteger _age;//年龄
        CGFloat _score;//分数
    }
    -(void)sayHello;
    -(void)introduceAelf;
    @end

#import "Student.h"
    @implementation Student
    -(void)sayHello{
        NSLog(@"大家好,我叫%@",_name);
    }
    -(id)init{
        _name = @"李铁蛋";
        _age = 18;
        _num = 01;
        _score = 75.5;
        
        return self;
    }
    -(void)introduceAelf{
        NSLog(@"大家好,我叫%@学号是%ld 分数为%0.2f 年龄为%ld",_name,_num,_score,_age);
    }
    @end

posted @ 2015-12-19 14:21  DH_Fantasy  阅读(77)  评论(0)    收藏  举报