zoukankan      html  css  js  c++  java
  • 类的声明和实现

    类的定义分为两部分:

      类的声明以及类的实现

     类的声明

    类的声明:
            规定当前类的: 类名 、属性 、行为
    
    格式:
            
            @interface 类名 : 父类名
            {
                  //定义类的属性
            }        
                 //定义类的行为
    
            @end // 最后end结束
    

      

    类的实现

    类的实现:
            实现具体行为
    
       格式:
            @implementation 类名
                
                    //行为的具体实现
            @end
    

      

    //车的类的声明
    
    @interface Car : NSObject
    {
             //类的属性
              int lunzi;   //车的轮子
              NSString *color ;//车的颜色
              int speed;    //车的速度    
    }                    
            //类的行为
            //方法类型标示符(返回类型) 方法签名关键词
            -(void)run; //定义一个无参无返回值的方法
            //方法类型标示符 (返回类型) 方法名1:(参数类型) 参数名; //有一个参数
            //方法类型标示符 (返回类型) 方法名1:(参数类型) 参数名 and (参数类型2) 参数2
            //其中 方法名1 : 中 后面的 ":" 也是方法名的一部分
            //方法前  "+" 标示静态方法 "-"标示动态方法
            -(int) sum :(int) x and :(int) y;
            -(void) runofNum:(int l):andNum2:( int s);
    
    @end
    
     //类的实现
    
    @implementation Car
    //行为的具体实现
    -(void) run{
        NSLog(@"车跑");    
    }
    
    -(int) sum :(int) x and:(int) y
    {
         return x+y;
    }
    
    @end
    
    Car *car =[Car new];
    Car new        1)向计算机申请内存空间
                        2) 给类中的每个成员初始化一个值
                        3) 返回新申请空间的首地址
    
    成员的调用
      car -->
    方法的调用
        [car run];
    
    -(int) sum :(int ) x and :(int ) y;
    有参方法的使用注意:
    //方法的类型  -对象方法
    //方法的返回值    int 类型
    //方法的参数是: x  y
    //参数的类型 :第一个 int 第二个 int
    //方法名: sum : and:(冒号是方法名的一部分)
    int s= [ca sum:34 and: 23];
    

      

  • 相关阅读:
    查看hbase中的中文
    查看hbase中的中文
    scala使用hbase新api
    scala使用hbase新api
    IDEA15使用maven编译scala和java
    IDEA15使用maven编译scala和java
    IDEA非sbt下spark开发
    IDEA非sbt下spark开发
    sed初学者实用说明
    sed初学者实用说明
  • 原文地址:https://www.cnblogs.com/developer-wang/p/4499507.html
Copyright © 2011-2022 走看看