首页 > 其他 > 详细

一、初始Object-C

时间:2014-01-14 21:33:47      阅读:442      评论:0      收藏:0      [点我收藏+]

一。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];

一、初始Object-C

原文:http://www.cnblogs.com/hqr9313/p/3513432.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!