zoukankan      html  css  js  c++  java
  • 【转】话说我打算一天学完object c语法,系列1--------来自书Objective-c程序设计

    原文网址:http://blog.csdn.net/zengraoli/article/details/8993466

    类型:

    NSString

    NSInteger

    NSLong控制台输出

    NSObject:对象

    比较两个对象是否相等:

    1. NSObject *object1 = [[NSObject alloc] init];  
    2. NSObject *object2 = obejct1;  
    3. if([object isEqual:object2])  
    4. {  
    5.     NSLong(@"is equal");  
    6. }  
    7. else  
    8. {  
    9.     NSLong(@"is not equal");  
    10. }  

    判断一个string是否为空:

    1. NSString *shortString  = @"HelloWorld";  
    2. if([shortString lengtg] == 0)  
    3. {  
    4.     NSLong(@"is empty string");  
    5. }  
    6. else  
    7. {  
    8.     NSLong(@"is not empty string ");  
    9. }  

    循环结构,比如for:

    1. int increment = 0;  
    2. for (increment = 0; increment < 100; increment++)  
    3. {  
    4.     NSLog(increment++);  
    5. }  

    -(void) method : (int) arguments;

    -为方法类型 +表示类方法

    -(id) initWithAge:(int) _age identify:(int)_identify

    方法名称为initWithAge,第一个参数是(int) _age,第二个参数是(int)_identify

    identify其实是对_identify的一个说明,initWithAge对_age一个说明

    方法的调用:

    1.[类名或对象名 方法名];

    2.对象名.方法名

    1. NSString *s;  
    2. s = [[NSString alloc] initWithString:@Hello iphone4S];  
    3.   
    4.   
    5. Person *person = [Person alloc];  
    6. person = [person init];  
    7.   
    8. -(id) initWithAge:(int)_age identify:(int) _identify  
    9. {  
    10. if(self = [super init])  
    11. {  
    12. age = _age;  
    13. identify = _identify;  
    14. }  
    15. return self;  
    16. }  
    17.   
    18. NSLog(@"self class is : %@", [self class]);  

    @class 和import的区别

    @class只是用到了声明,如果需要用到这个class里面的方法,还需要import,通常在.h文件里面只需要@class,.m文件里面需要import

    oc里面不需要get说明,直接使用:

    多个成员变量可以不写get和set,使用property(list) names

    @implementation Person

    @synthesize myNumber

    @end

    调用的时候:

    NSLog(@"Person number : %d",[person myNumber]);

    还有个@property(nonatomic) int number

    atomic是多线程的一个保护技术

    重载:

    定义一个同名的新方法,新方法必须具有相同的返回类型,并且参数的个数和重载的方法相同

    class里面的权限控制:

    同时也具有public protected private,oc也是单继承

    成员变量的new是这样的:

    1. Person *person = [[Person alloc] init];  

    但是对应的需要在return之前使用:

    1. [pool drain]或者[pool release];  

    drain用于清除pool中对象,release用来释放内存 

    比如可以这样[person release]

    方法调用:

    [实例 方法]

    [类名 方法]

    完整的方法调用格式为:

    [接收方 名字1:参数1 名字2: 参数2 名字3: 参数3 ...]

    oc运行在一个方法调用中嵌套另一个方法调用,比如:

    1. [NSString stringWithFormat:[test format]];  

    另外还有一单,self类似this,可以使用self调用本类中的方法:

    1. -(BOOL) isQualified  
    2. {  
    3. return ([self age] > 21);  
    4. }  

    输入输出,和c差不多,也有scanf,输出用NSLog(),占位符前面需要加上@,如果是oc内置类型,比如NSString需要这样:%@

    id类型和class的简单使用:

    typedef:

    和c是一样的

    typedef int myInt

    myInt age;

    BOOL类型

    YES、NO

    选择器SEL

    P44 用到再说

    创建一个类

    静态成员变量与类方法

    static int intY;

    +(int) staticIntY

    就像上面所写的,需要使用“+”来声明类方法

    变量的存储类型:

    1、auto、自动局部变量,是缺省设置

    2、const

    3、volatile,这个修饰符刚好和const相反,它明确地告诉编译器,该变量的值 会发生改变,他用来修饰被不同线程访问和修改的变量

    定义@property修饰符来设置成员变量的get和set

    修饰符可以是:

    实现一个例子P119

    MyClass.h:

    1. //  
    2. //  MyClass.h  
    3. //  test  
    4. //  
    5. //  Created by Dawn on 13-5-27.  
    6. //  Copyright (c) 2013年 zeng. All rights reserved.  
    7. //  
    8.   
    9. #import <Foundation/Foundation.h>  
    10.   
    11. @interface MyClass : NSObject{  
    12.     int intValue;  
    13.     float floatValue;  
    14. }  
    15.   
    16. @property int _intValue;  
    17. @property (copy) NSString *name;  
    18. @property float floatValue;  
    19. @property (readonly, getter = getANickname) NSString *nickname;  
    20.   
    21. @end   

    MyClass.m:

    1. //  
    2. //  MyClass.m  
    3. //  test  
    4. //  
    5. //  Created by Dawn on 13-5-27.  
    6. //  Copyright (c) 2013年 zeng. All rights reserved.  
    7. //  
    8.   
    9. #import "MyClass.h"  
    10.   
    11. @implementation MyClass  
    12.   
    13. @synthesize _intValue = intValue, name;  
    14.   
    15. // 这条语句不是必须的  
    16. @dynamic floatValue;  
    17.   
    18. -(float) floatValue{  
    19.     return floatValue;  
    20. }  
    21.   
    22. -(void)setFloatValue:(float)aValue{  
    23.     floatValue = aValue;  
    24. }  
    25.   
    26. -(NSString *)getANickname{  
    27.     return @"Lee";  
    28. }  
    29.   
    30. @end  

    main.m:

    1. //  
    2. //  main.m  
    3. //  test  
    4. //  
    5. //  Created by Zeng on 13-5-24.  
    6. //  Copyright (c) 2013年 zeng. All rights reserved.  
    7. //  
    8.   
    9. #import <Foundation/Foundation.h>  
    10. #import "YourClub.h"  
    11. #import "Membership.h"  
    12. #import "MyClass.h"  
    13.   
    14.   
    15. int main(int argc, const char * argv[])  
    16. {  
    17.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
    18.    
    19.     MyClass *class1 = [[MyClass alloc] init];  
    20.     [class1 set_intValue:1];  
    21.     [class1 setName:@"Sam"];  
    22.     [class1 setFloatValue:1.1f];  
    23.       
    24.     NSLog(@"intValue is %i, name is %@, floatValue is %g, nickname is %@", [class1 _intValue], [class1 name], [class1 floatValue], [class1 getANickname]);  
    25.       
    26.     [class1 release];  
    27.       
    28.     [pool release];  
    29.     return 0;  
    30. }   

             在object-c 2.0中,在.h文件中使用@property来标识属性(一般是实例变量);在实现文件中(也就是扩展名为.m的文件),使用@synthesize标识所声明的属性,让系统自动生成设置方法和获取方法。

    声明一个多参数的方法:

    MyClass.h:

    1. //  
    2. //  MyClass.h  
    3. //  test  
    4. //  
    5. //  Created by Dawn on 13-5-27.  
    6. //  Copyright (c) 2013年 zeng. All rights reserved.  
    7. //  
    8.   
    9. #import <Foundation/Foundation.h>  
    10.   
    11. @interface MyClass : NSObject{  
    12.     NSString *name;  
    13.     int age;  
    14. }  
    15.   
    16. @property (nonatomic, copy) NSString *name;  
    17. @property (nonatomic) int age;  
    18.   
    19. -(void) setName:(NSString *)theName andSetTheAge: (int) theAge;  
    20.   
    21. @end  

    MyClass.m:

    1. //  
    2. //  MyClass.m  
    3. //  test  
    4. //  
    5. //  Created by Dawn on 13-5-27.  
    6. //  Copyright (c) 2013年 zeng. All rights reserved.  
    7. //  
    8.   
    9. #import "MyClass.h"  
    10.   
    11. @implementation MyClass  
    12. @synthesize name;  
    13. @synthesize age;  
    14.   
    15.   
    16. -(void) setName:(NSString *)theName andSetTheAge: (int) theAge{  
    17.     [self setName:theName];  
    18.     [self setAge:theAge];  
    19. }  
    20.   
    21. @end   

    main.m:

      1. //  
      2. //  main.m  
      3. //  test  
      4. //  
      5. //  Created by Zeng on 13-5-24.  
      6. //  Copyright (c) 2013年 zeng. All rights reserved.  
      7. //  
      8.   
      9. #import <Foundation/Foundation.h>  
      10. #import "YourClub.h"  
      11. #import "Membership.h"  
      12. #import "MyClass.h"  
      13.   
      14.   
      15. int main(int argc, const char * argv[])  
      16. {  
      17.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
      18.    
      19.     MyClass *class1 = [[MyClass alloc] init];  
      20.     [class1 setName:@"zengraoli" andSetTheAge:36];  
      21.     NSLog(@"name is %@, age is %i", [class1 name], [class1 age]);  
      22.       
      23.     [pool release];  
      24.     return 0;  
      25. }  
  • 相关阅读:
    用OxyPlot在WPF中演示正演磁异常的变化规律
    《暗时间》读书笔记
    [C++]KMP算法实现
    [C++]Infinite House of Pancakes——Google Code Jam 2015 Qualification Round
    [C++]Standing Ovation——Google Code Jam 2015 Qualification Round
    [C++]让CPU使用率曲线呈现为正弦曲线(一)
    [C#]中英文字幕合并的小程序
    [C++]Store Credit——Google Code Jam Qualification Round Africa 2010
    [C++]Saving the Universe——Google Code Jam Qualification Round 2008
    [Java]使用队列求解josephus问题
  • 原文地址:https://www.cnblogs.com/wi100sh/p/4257319.html
Copyright © 2011-2022 走看看