zoukankan      html  css  js  c++  java
  • 类和对象

    面向对象编程

    基本:

    • 概念:对象 类 封装 多态
    • 主要语言有 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);
    }

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

  • 相关阅读:
    大数据学习之大数据简介03
    大数据学习之Linux进阶02
    大数据学习之Linux基础01
    连接数据库出现java.sql.SQLException: Unknown system variable 'tx_isolation'
    Linux中伪分布的搭建
    【TCP/IP】入门学习笔记 三
    【TCP/IP】入门学习笔记 二
    【TCP/IP】入门学习笔记 一
    【CentOS】CentOS7 自动同步时间:服务ntp,命令ntpdate
    【Mysql】- pt-online-schema-change在线更新大表字段、加索引
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/8858616.html
Copyright © 2011-2022 走看看