IOS第二天(点语法、构造方法与discription方法)

一、点语法

  点语法的格式跟java中的对象方法与变量的访问差不多,但作用在等号的前后各代表不同的意思:

  1.放在等号的左边代表调用了set方法

  2.放在等号的右边代表调用了get方法

  student.h

#import <Foundation/Foundation.h>

@interface Person : NSObject {
    int _age;                      //变量默认下划线开头
}

- (void)setAge:(int)age;    // 方法名是setAge:
- (int)age;                       // 方法名是age,代表getAge方法

// 方法名是setAge:andNo:
// - (void)setAge:(int)newAge andNo:(int)no;
@end

 

 

 

  student.m

#import "Person.h"

@implementation Person

- (void)setAge:(int)age {
    NSLog(@"调用了setAge方法:%i", age);
    _age = age;
    
    // 这是错误的写法,会导致死循环,无限调用set方法
    // self.age = newAge;   // 相当于是[self setAge:newAge];
    // 点语法用在被赋值的一边,代表调用set方法
}

- (int)age {
    NSLog(@"调用了age方法:%i", _age);
    
    return _age;
}
@end

 

 

  main.m

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        Person *person = [[Person alloc] init];
        
        person.age = 10;         // 等效于[person setAge:10];
        
        int age = person.age;   // 等效于int age = [person age];
        
        NSLog(@"age is %i", age);
        
        [person release];
    }
    return 0;
}

 

 

二、构造方法与description方法

  1.构造方法的作用就是对对象进行初始化

  2.重写description方法,有点像重写java中的toString方法,默认打印对象的时候是打出地址值(java中是hashcode值),重写,改成自己需要的

  student.h

#import <Foundation/Foundation.h>

@interface Student : NSObject {
//变量有三种修饰符:public、protect、private
//变量如果不修饰,默认是protect类型
    int _age;
    int _no;
}

- (void)setAge:(int)age;
- (int)age;

- (void)setNo:(int)no;
- (int)no;

// 自己写一个构造方法
- (id)initWithAge:(int)age andNo:(int)no;

@end

 

 

 

  student.m

#import "Student.h"

@implementation Student

- (void)setAge:(int)age {
    _age = age;
}

- (void)setNo:(int)no {
    _no = no;
}

- (int)age {
    return _age;
}

- (int)no {
    return _no;
}
// 实现构造方法
- (id)initWithAge:(int)age andNo:(int)no {
    // 首先要调用super的构造方法
    // self = [super init];
    
    // 如果self不为nil
    if (self = [super init]) {
        // _age = age;
        // _no = no;
        self.age = age;         //这儿调用了setAge方法
        self.no = no;           //这儿调用了setNo方法
    }
    
    return self;
}

// 重写父类的description方法
// 当使用%@带打印一个对象的时候,会调用这个方法
- (NSString *)description {
    NSString *str = [NSString stringWithFormat:@"age is %i and no is %i", self.age, self.no];
    return str;
}

// 如果直接把方法写在.m文件中,没有在.h文件中进行声明,那么这就是私有方法

// 谁调用方法,self就指向谁

//动态方法中self代表的是当前对象
- (void)test {
    int age = self.age;
}

+ (void)test2 {
    [Student alloc];
    
    [self alloc];
    
    // 上面两句代码是等效的,在静态方法中使用self,self代表的就是当前类
}
@end

 

 

  GoodStudent.h

#import "Student.h"

@interface GoodStudent : Student  //这里是定义了一个GoodStudent类,继承自Student

@end

 

 

  GoodStudent.m

#import "GoodStudent.h"

@implementation GoodStudent

// 子类访问了父类的成员变量
// 这里是私有方法,因为此test方法没有在GoodStudent.h中声明
- (void)test {
    _age = 10;
}
@end

 

  main.m

#import <Foundation/Foundation.h>
#import "Student.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        // char *s = "itcast";  C语言中字符串
        
        NSString *str =  @"itcast"; // OC中的字符串
        
        //用这种方法创建对象,可以调用自己写的构造方法
        Student *stu = [[Student alloc] initWithAge:15 andNo:2];
        
        //还可以用Student *stu = [Student new]来创建stu对象
        //等价于Student *stu = [[Student alloc] init];
        //但此方法就不能调用构造函数

        // NSLog(@"age is %i and no is %i", stu.age, stu.no);
        // %@代表打印一个OC对象
        NSLog(@"%@", stu);
        
        [stu release];
    }
    return 0;
}

 

posted @ 2015-10-02 22:00  ZashioM  阅读(325)  评论(1编辑  收藏  举报