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
#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
相关文章推荐
- 一个简单的asp数据库操作类
- 在线管理数据库 类
- c# 类和成员的修饰详细介绍
- 深入c# 类和结构的区别总结详解
- c#结构和类的相关介绍
- 基于JavaScript 类的使用详解
- 数组与类使用PHP的可变变量名需要的注意的问题
- PHP中ADODB类详解
- 一个改进的UBB类
- php全局变量和类配合使用深刻理解
- 二级域名转向类
- javascript之大字符串的连接的StringBuffer 类
- 给多个地址发邮件的类
- PHP 实例化类的一点摘记
- PHP 类相关函数的使用详解
- Java Calendar类常用示例_动力节点Java学院整理
- 查找页面中所有类为test的结点的方法
- 我常用的几个类
- C++类中的常量介绍
- php实现MySQL数据库备份与还原类实例