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

    /*
       
       类:  
     
            一类事物共同特征和行为的抽象
     
            大卡车    小轿车    挖掘机
     
            车类: Car
            共同的特征(属性): 颜色   速度   轮子
            共同的行为: 跑     停止
     
       对象:
     
            类的具体的个体
     
     
            东风21D(车类的对象)
     
            解放大卡车(车类的对象)
     
            BYD (车的对象)
     
            时风 (车的对象)
     
     
         2、类的定义
     
            结构体定义
            struct Student{
     
     
            };   ---->定义一个Student 结构体
     
     
     
            类的定义分为两部分:
     
            1)类的声明(规定当前类的:类名、属性、行为)
     
               @interface 类名:父类名
               {
                  //定义类的属性
                }
                //类的行为
               @end
     
     
     
            2)类的实现(实现具体行为)
     
               @implementation 类名
     
                //行为的具体实现
     
               @end
     
     
     */
    
    
    #import <Foundation/Foundation.h>
    
    //车的类的声明
    @interface Car:NSObject
    {
       //类的属性
        int lunzi;  //车的轮子
        NSString *color;//车的颜色
        int speed;  //车的速度
    }
       //类的行为
    @end
    
    
    //类的实现
    @implementation Car
    //行为的具体描述
    @end
    
    
    
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            // insert code here...
            NSLog(@"Hello, World!");
        }
        return 0;
    }
    
    
    /*
     
       在OC中用已经存在的类,如何创建一个对象?
     
           回顾C的结构体,结构体如何创建结构体变量
     
           struct Student stu;  //stu就是结构体变量
     
           struct Student *pstu; //pstu = &stu;
     
           结构体指针访问 结构体变量中的成员变量值
           pstu->age = 20;  //给age 设置 值 是 20
     
     
       OC中对象创建
     
           [Car new];  
           //做了3件事情
           // 1)向计算机申请内存空间
           // 2) 给类中的每一个成员初始化值
           // 3)返回新申请的空间的首地址
     
           //理解方式一:
           //定义了一个Car类型的指针变量
           //指针变量指向新申请的内存空间
     
           //理解方式二:
           //用Car类实例化了一个实例对象,对象的名称是p
             Car *p = [Car new];
     
     
     
     */
    
    
    #import <Foundation/Foundation.h>
    
    //车的类的声明                 车的图纸(类)
    @interface Car:NSObject
    {
        //类的属性
        @public;
        
        int lunzi;  //车的轮子
        NSString *color;//车的颜色
        int speed;  //车的速度
    }
    //类的行为
    @end
    
    
    //类的实现
    @implementation Car
    //行为的具体描述
    @end
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
          
            //创建Car类型的对象 car1
        
            Car *car1=[Car new];
            
            //创建一个对象 实质还是指针 (用指针间接访问实例变量值)
            car1->lunzi = 3;
            car1->speed = 150;
            car1->color = @"蓝色";
            
            //查看车的信息
            NSLog(@"轮子:%d,速度:%d ,颜色:%@",car1->lunzi,car1->speed,car1->color);
            
        }
        return 0;
    }
    
  • 相关阅读:
    HTB-靶机-Charon
    第一篇Active Directory疑难解答概述(1)
    Outlook Web App 客户端超时设置
    【Troubleshooting Case】Exchange Server 组件状态应用排错?
    【Troubleshooting Case】Unable to delete Exchange database?
    Exchange Server 2007的即将生命周期,您的计划是?
    "the hypervisor is not running" 故障
    Exchange 2016 体系结构
    USB PE
    10 months then free? 10个月,然后自由
  • 原文地址:https://www.cnblogs.com/aiti/p/4660612.html
Copyright © 2011-2022 走看看