OC学习01-类和对象

2015-07-20 17:05:52 ? 作者: ? 浏览: 3

基本:

概念:对象 类 封装 多态 主要语言有 C++, Java,OC

面向过程与面向对象的区别

? 面向过程 面向对象
特点 分析步骤,实现函数 分析问题找出参与并得出对象及其作用
侧重 实现功能 对象的设计(具体指功能)
例子 C语言 OC,C++,java

基本代码:

    // OC打印
    NSLog(@Lanou);

    // NSInteger 整型,CGFloat 浮点型,NSString 字符串
    NSInteger i = 100;
    NSLog(@i = %ld,i);
    CGFloat f = 3.14;
    NSLog(@f = %g,f);

    // OC的字符串可以放中文
    NSString *str= @哟哟;
    NSLog(@%@,str);

    // OC的数组
    // 与for循环遍历一样,直接用NSLog进行遍历
    NSArray *arr = @[@1,@2,@3];
    NSLog(@%@,arr);
    for (NSInteger i = 0 ; i < 3; i++) {
        NSLog(@%@,arr[i]);
    }

类和对象

类和对象是面向对象编程的核心 类就是具有相同特征以及行为事物的抽象

对象

对象是类的实例 类是对象的类型

OC中类的定义

先定义类,再创建对象,然后使用对象 定义有两个部分,接口与实现(注意分开写) 文件: .h的为接口文件, .m的为实现文件
接口
@interface Student : NSObject
@end

实现

// 对应的实现文件
@implementation Student

创建对象

任何对象都要占用空间 创建对象分为两步
①分配空间 ②初始化 初始化
// 1.分配空间
Student *stu = [Student alloc];
// 2.初始化
stu = [stu init];
// 可以把上两个步骤合成一个语句
Student *stu = [[Student alloc] init];

初始化中返回的self指的是返回完成的自己

- (id)init
{
    _stuName = @俊宝宝;
    _stuSex = @女;
    _stuHobby = @男;
    _stuAge = 20;
    _stuScore = 100;
    return self;
}

使用对象

    Student *stu = [[Student alloc] init];
    [stu sayHi];

实例变量操作

实例变量初始化的时候只做少量的设置,后期完善 实例变量的可见度分为三种
@public 公有 @private 私有 @protected 受保护的
@interface Student : NSObject
{
    // 成员变量的可见度
    @public
    // 成员变量,或实例变量
    NSString *_stuName; // 名字
    NSInteger _stuAge;  // 年龄
    NSString *_stuSex;  // 性别
    CGFloat _stuScore;  // 分数
    NSString *_stuHobby;// 爱好
}
@end

操作成员

    // 操作成员变量
    // 对象通过->来访问自己的成员变量
    NSLog(@%ld,stu -> _stuAge);
    // 修改年龄
    stu -> _stuAge = 100;
    NSLog(@%ld,stu -> _stuAge);
    // 修改姓名,直接修改字符串直接赋值
    stu -> _stuName = @切克闹;
    NSLog(@%@,stu -> _stuName);

注意:public修饰的实例变量,可以直接使用” ->”访问

部分代码

main.m

int main(int argc, const char * argv[]) {
    // 通过手机的类,创建对象,并且对对象的成员变量进行修改
    MobilePhone *mPhone = [[MobilePhone alloc] init];
    mPhone -> _phoneBrand = @samsung;
    mPhone -> _phoneColor = @白色;
    mPhone -> _phoneModel = @S100;
    mPhone -> _phonePrice = 4800;
    mPhone -> _phoneSize = 1080;
    NSLog(@品牌为%@,颜色:%@,型号:%@,价格:%ld,屏幕:%ld,mPhone->_phoneBrand,mPhone->_phoneColor,mPhone->_phoneModel,mPhone->_phonePrice,mPhone->_phoneSize);
     return 0;
}

MobilePhone.h

@interface MobilePhone : NSObject
{
    @public
    NSString *_phoneBrand;
    NSString *_phoneModel;
    NSInteger _phoneSize;
    NSString *_phoneColor;
    NSInteger _phonePrice;
}
- (void)buyPhone;
- (void)call;
- (void)sendMessage;
- (void)surfInternet;
- (void)watchVideo;

// 写一个用来打印全部信息的功能
- (void)sayHi;

@end

MobilePhone.m

@implementation MobilePhone
- (void)buyPhone
{
    NSLog(@买了个手机);
}
- (void)call
{
    NSLog(@打了个电话);
}
- (void)sendMessage
{
    NSLog(@发了个短信);
}
- (void)surfInternet
{
    NSLog(@上了个网);
}
- (void)watchVideo
{
    NSLog(@看了个视频);
}
// 重写电话的初始化方法
- (id) init
{
    _phoneBrand = @Apple;
    _phoneColor = @red;
    _phoneModel = @S5;
    _phonePrice = 4000;
    _phoneSize = 1080;
    return self;
}
- (void)sayHi
{
    NSLog(@%@ , %@ ,%@  ,%ld ,%ld ,,_phoneBrand,_phoneColor,_phoneModel,_phonePrice,_phoneSize);
}

文章总结了今天学习的基本内容

?

-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: