您的位置:首页 > 编程语言

oc第一天课程代码(创建矩形类)

2014-04-02 19:10 267 查看
       一.创建矩形类main代码

#import <Foundation/Foundation.h>

#import "QFRect.h"

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

   //oc关键字,处理代码模块内存管理关键字.

    @autoreleasepool {

        

        // insert code here...

        //创建一个矩形对象,通过alloc函数创建对象并且分配内存
       QFRect* rect1 = [QFRectalloc];

        //通过init函数对对象进行初始化
        rect1 =[rect1init];

        

        //以上两句话可以连在一起

        // rect1 = [[QFRect alloc] init];

        
        [rect1setXPosValue:5];
        [rect1setYPosValue:10];
        [rect1setWidthValue:10];
        [rect1setHeightValue:20];

        
       int xPos = [rect1
getXPosValue];
       int yPos = [rect1
getYPosValue];
       int width = [rect1
getWidthValue];
       int heigth = [rect1
getHeightValue];

        
       NSLog(@"xPos = %d",xPos);
       NSLog(@"yPos = %d",yPos);
       NSLog(@"width = %d",width);
       NSLog(@"height = %d",heigth);

    }
   return 0;
}

      一.创建矩形类.h代码

#import <Foundation/Foundation.h>

//NSObject是所有oc中类的基础类
@interface QFRect :NSObject
{

@public     //定义公有成员变量,这样谁都可以访问

    

@protected  
//受到保护的成员变量

    //左上角坐标x
   int xPos ;

    //右上角坐标y
   int yPos ;

    //矩形宽度
   int width;

    //矩形高度
   int height;

@private    
//私有的成员变量

    
}

//只要申明在头文件中的函数全部为公有函数

//设置一个函数设置xPos的值
-(void)setXPosValue:(int) v
b03c
alue;
-(void)setYPosValue:(int) value;
-(void)setWidthValue:(int) value;
-(void)setHeightValue:(int) value;

//获得xpos的值
-(int)getXPosValue;

//获得YPos的值
-(int)getYPosValue;
-(int)getWidthValue;
-(int)getHeightValue;

@end

一.创建矩形类.m代码

#import "QFRect.h"

@implementation QFRect

//函数实现xpos
-(void)setXPosValue:(int) value
{
   xPos = value;
}
-(void)setYPosValue:(int) value
{
   yPos = value;
}
-(void)setWidthValue:(int) value
{
   width = value;
}
-(void)setHeightValue:(int) value
{
   height = value;
}

//实现getX函数
-(int)getXPosValue
{
   return
xPos;
}

-(int)getYPosValue
{
   return
yPos;
}
-(int)getWidthValue
{
   return
width;
}
-(int)getHeightValue
{
   return
height;
}

@end

                                               http://www.mobiletrain.org/about/news/oc_video2013.html

                                           
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: