IOS_OC_2.2——基础语法2

1,@property 和@synthesize  用于标记get  set方法的声明和实现

      老版本中  property用于方法声明;

                      synthesize用于方法实现;

       现在版本 只用property就直接实现了所有!

  

//
//  Student.h
//  第一个OC项目
//
//  Created by 周刚 on 15/1/7.
//  Copyright (c) 2015年 zg. All rights reserved.
//

#import "Person.h"

@interface Student : Person
{
    int _tel;
    NSString *_class;
    NSString *_teacherName;
}
@property int tel;
@property NSString *class;
@property NSString *teacherName;

@end
//
//  Student.m
//  第一个OC项目
//
//  Created by 周刚 on 15/1/7.
//  Copyright (c) 2015年 zg. All rights reserved.
//

#import "Student.h"

@implementation Student


@end
//
//  main.m
//  第一个OC项目
//
//  Created by 周刚 on 15/1/7.
//  Copyright (c) 2015年 zg. All rights reserved.
//

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

int main(int argc, const char * argv[]) {
    
#pragma mark 第一个标签
    NSLog(@"这是我的第一个OC项目");
    [Person print];
    Person *p = [Person new];
    //方法调用
    [p setAge:23];
    //直接访问成员变量
    //p->_age = 23;
    [p setSex:1];
    [p setName:@"周刚"];
    
    NSLog(@"我的年纪是:%i\n",[p age]);
    NSLog(@"我的性别是:%i\n",[p sex]);
    //对象输出
    NSLog(@"我的姓名是:%@\n",[p name]);
#pragma mark - 我的分割线标签

    
#pragma mark 使用property和synthesize的校验
    Student *stu = [Student new];
    stu.name = @"周刚1111";
    stu.age = 23111;
    stu.sex = 1111;
    stu.class = @"计算机应用111";
    stu.teacherName = @"程菲111";
    
    NSLog(@"我的姓名是:%@\n",stu.name);
    NSLog(@"我的年纪是:%i\n",stu.age);
    NSLog(@"我的性别是:%i\n",stu.sex);
    NSLog(@"我的班级是:%@\n",stu.class);
    NSLog(@"我的老师是:%@\n",stu.teacherName);

    return 0;
}


2,id类型

      id是一个万能指针   相当于   *NSObject

  
    id d = [Student new];
    [d setAge:23];

     相当于java的object;


3,构造方法

     构造方法的定义:  和java相同

    new的实现: 

             1,调用内方法  +(id)alloc分配存储空间  返回一个未初始化类对象

             2,调用成员方法  -init来初始化

       new  方法

+ (instancetype)new
Description    
Allocates a new instance of the receiving class, sends it an init message, and returns the initialized object.
This method is a combination of alloc and init. Like alloc, it initializes the isa instance variable of the new object so it points to the class data structure. It then invokes the init method to complete the initialization process.
Returns    
A new instance of the receiver.
Availability    OS X (10.0 and later)
Declared In    NSObject.h
Reference    NSObject Class Reference

    构造方法就是 -init

   重写init

//
//  Teacher.m
//  第一个OC项目
//
//  Created by 周刚 on 15/1/7.
//  Copyright (c) 2015年 zg. All rights reserved.
//

#import "Teacher.h"

@implementation Teacher
- init
{
    if(self = [super init]){
        _age = 10;
    }
    return self;
}
@end

    注:我们可以再重写构造方法中来实现一个成员变量的固定值初始化

    自定义构造方法:

        1,一定是对象方法

        2,返回值是id类型

        3,方法名以init开头

//
//  Teacher.m
//  第一个OC项目
//
//  Created by 周刚 on 15/1/7.
//  Copyright (c) 2015年 zg. All rights reserved.
//

#import "Teacher.h"

@implementation Teacher
#pragma mark - 重写构造方法
- (id)init
{
    if(self = [super init]){
        _age = 10;
    }
    return self;
}

#pragma mark - 自定义构造方法
- (id)initWithName:(NSString *)name
{
    if(self = [super init]){
        _name = name;
    }
    return  self;
}
@end


你可能感兴趣的:(IOS_OC_2.2——基础语法2)