第一部分 1.10 OC基础语法-点语法、构造方法、结构体

一、点语法

新建一个person类

#import <Foundation/Foundation.h>



@interface Person : NSObject {

    int _age;

}



- (void)setAge:(int)age; // 方法名是setAge:

- (int)age; // 方法名是age



// 方法名是setAge:andNo:

// - (void)setAge:(int)newAge andNo:(int)no;

@end
#import "Person.h"



@implementation Person



- (void)setAge:(int)age {

    NSLog(@"调用了setAge方法:%i", age);

    _age = age;

    

    // 这是错误的写法,会导致死循环,无限调用set方法

    // self.age = newAge;// [self setAge:newAge];

}



- (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;

}

 

二、构造方法

新建一个student类

#import <Foundation/Foundation.h>



@interface Student : NSObject {

    int _age;

    int _no;

}



- (void)setAge:(int)age;

- (int)age;



- (void)setNo:(int)no;

- (int)no;



// 自己写一个构造方法

- (id)initWithAge:(int)age andNo:(int)no;



@end
#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;

        self.no = no;

    }

    

    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就指向谁

- (void)test {

    int age = self.age;

}



+ (void)test2 {

    [Student alloc];

    

    [self alloc];

    

    // 上面两句代码是等效的

}

@end

然后再新建goodstudent继承student

#import "Student.h"



@interface GoodStudent : Student



@end
#import "GoodStudent.h"



@implementation GoodStudent



// 子类访问了父类的成员变量

- (void)test {

    _age = 10;

}

@end

 

main.m中实现

#import <Foundation/Foundation.h>

#import "Student.h"



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

{



    @autoreleasepool {

        // char *s = "neal";  C语言中字符串

        

        NSString *str =  @"itcast"; // OC中的字符串

        

        

        Student *stu = [[Student alloc] initWithAge:15 andNo:2];

        

        // NSLog(@"age is %i and no is %i", stu.age, stu.no);

        // %@代表打印一个OC对象

        NSLog(@"%@", stu);

        

        [stu release];

    }

    return 0;

}

 

三、结构体的定义,代码实现

#include <stdio.h>



void test() {

    // 这个机构只能在函数内部使用

    // 定义一个名为Student的结构体类型

    struct Student {

        int age; // 年龄

        char *name; // 姓名

        float height; // 身高

    };

    

    // 定义一个结构体变量

    struct Student stu = {27, "mj", 1.8f};

    // 下面这行的初始化是错误的

    // stu = {27, "mj", 18.0f};

    

    stu.age = 28;

    

    printf("age=%d\n", stu.age);

    printf("name=%s\n", stu.name);

    printf("height=%.1f\n", stu.height);

}



void test1() {

    struct Student {

        int age; // 年龄

        char *name; // 姓名

        float height; // 身高

    } stu = {27, "mj", 1.8f};

    

    struct Student stu1 = {28, "lmj", 1.9f};

    

    

    struct {

        int age; // 年龄

        char *name; // 姓名

        float height; // 身高

    } stu2 = {27, "mj", 1.8f};

    

    struct {

        int age; // 年龄

        char *name; // 姓名

        float height; // 身高

    } stu3 = {27, "mj", 1.8f};

}



void test2() {

    // 定义一个Date结构体

    struct Date {

        int year;

        int month;

        int day;

    };

    

    // 定义一个学生结构体

    struct Student {

        int age;

        struct Date birthday;

    };

    

    struct Student stu = {27, {2009, 10, 10}};

    

    printf("age=%d\n", stu.age);

    printf("year=%d\n", stu.birthday.year);

    printf("month=%d\n", stu.birthday.month);

    printf("day=%d\n", stu.birthday.day);

}



void test3() {

//    struct Student {

//        int age; // 年龄

//        char *name; // 姓名

//        float height; // 身高

//    };

//    struct Student a[2] = {{27, "mj", 1.8f}, {28, "lmj", 1.9f}};

    

    struct Student {

        int age; // 年龄

        char *name; // 姓名

        float height; // 身高

    } a[2] = {{27, "mj", 1.8f}, {28, "lmj", 1.9f}};

    

    struct Student a2[4];

}



struct Person {

    int age;

};



void change(struct Person p) {

    p.age = 9;

}

// 结构体作为函数参数

void test4() {

    struct Person person = {27};

    change(person);

    

    printf("age=%d", person.age);

}



// 指向结构体的指针

void tets5() {

    // 定义一个结构体变量

    struct Person person = {27};

    // 定义一个指向结构体的指针

    struct Person *p;

    // 让指针p指向结构体person

    p = &person;

    

    printf("age=%d\n", person.age);

    printf("age=%d\n", (*p).age);

    printf("age=%d\n", p->age);

}



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

{

    tets5();

    return 0;

}

 

 

你可能感兴趣的:(构造方法)