一、初始Object-C

一。OC概述

特点:

1没有包得概念

2关键字以@开头

3.拓展名 .m

二。第一个OC

1,分为2个文件。.m和.h文件

2. .m文件用来实现类  .h用来定义声明类

.h文件中得写法

//@interface 代表声明一个类

//:表示继承

#import <Foundation/Foundation.h>

@interface Student : NSObject{//成员变量定义在大括号中

    int age;

    int no;

}

//age get 方法

//-代表动态方法,+代表静态方法

- (int)age;

-(int)no;

- (void)setAge:(int)newAge;

-(void)setAge:(int)newAge andNo:(int)newNo;

@end

  .m文件中得写法

#import "Student.h"

//@implementation代表实现类

@implementation Student

- (int)age{

    NSLog(@"调用了get");

    return age;

}

-(int)no{

    return no;

}

- (void)setAge:(int)newAge andNo:(int)newNo{

    NSLog(@"调用了set");

    age = newAge;

    no = newNo;

}

@end

 3.调用类

/创建一个Student对象;

        //1.调用一个静态方法来分配内存

        Student *stu = [[Student alloc] init];

       

        //调用一个动态方法init初始化

        //stu = [stu init];

       // [stu setAge:100];

        //int age =  [stu age];

        //NSLog(@"%i",age);    

        [stu setAge:17 andNo:23];

        NSLog(@"%i  %i",[stu age],[stu no]);

//释放对象

        [stu release];

原文地址:https://www.cnblogs.com/hqr9313/p/3513432.html