iOS注意事项

来源:互联网 发布:杭州淘宝商学院地址 编辑:程序博客网 时间:2024/05/17 08:51

1.封装,继承,多态

Set:方法1.作用提供一个方法给外界设置成员变量的命名规范

 作用:提供一个变量方法给外界设置成员变量值,可以在方法里面对参数进行相应的过

   set后面跟上成员变量的名称,成员变量的首字母必须大写

   一定要接收一个参数,而且参数类型跟成员变量类型一致

  形参的名称不能跟成员变量名一样

Get:方法

      返回对象内部的成员变量

     肯定有返回值,返回值类型肯定和成员变量类型一致,方法名跟成员变量名一致,不需要接    受任何参数

   #import <Fountation/Fountation.h>

@interfceStudent : NSObject

Int age;

}

-(void)setAge:(int)newAge;

@end

@implementationStudent

-(void)setAge:(int )newAge{

   if (newAge<= 0)

  {

   newAge =1; 

     }

   age =newAge;

}

-(void)study

{

  

   NSLog("%d学生在学习"age);

 

 }

@end

Int main()

{

Student *stu =[Student new];

 

 [stu setAge:0];

[stustudy];

 }

2.对于字符一应该用单引号扩起来'Z'

  双引号表示一个字符串

3.类方法智能用类进行调用,当类方法用实例调用时,会出现闪退现象导致程序崩溃

4.对象方法不能用类对象调用,也会出现程序崩溃

5.两者的区别,类方法+开头,只能由类对象来调用

                             对象方法-开头,只能由对象来调用,两者可以重名,对象方法中能调用当前     对象的成员变量

//

//  main.m

//  yun

//

//  Created by qingyun on 15/7/27.

//  Copyright (c) 2015qingyun. All rights reserved.

//

 

#import<Foundation/Foundation.h>

 

@interface Score:NSObject

  {

     int _cscore;

     int _ocscore;

     int _totalscore;

     int _averagescore;

  

   }

-(void)setCscore:(int)cscore;

-(int) cscore;

-(void)setOcscore:(int)ocscore;

-(int)ocscore;

-(int)totalscore;

-(int)averagescore;

@end

@implementation Score

 

-(void)setCscore:(int)cscore

      {

         _cscore =cscore;

          _totalscore = _ocscore+_cscore;

          _averagescore = _totalscore/2;

          NSLog(@"_total value is %d\n",_totalscore);

      }

-(int)cscore

      {

         return _cscore;

      }

-(void)setOcscore:(int)ocscore

   {

      _ocscore=ocscore;

   }

-(int)ocscore

      {

         return _ocscore;

          }

-(int)totalscore

 {

  

     return _totalscore;

 }

-(int)averagescore

 {

     return _averagescore;

  }

 

@end

int main ()

 {

   Score *s =[Scorenew];

    [ssetOcscore:100];

    [ssetCscore:90];

     [saveragescore];

   int a = [s totalscore];

    

    NSLog(@"总分:%d",a);

   return 0;

 }


0 0
原创粉丝点击