首页 > 移动平台 > 详细

iOS复习笔记3:类的基本定义

时间:2015-01-29 00:06:33      阅读:306      评论:0      收藏:0      [点我收藏+]
// Car.h
// 类的声明
// 类名:Car
// 属性:m_nSpeed
// 行为:run


#import <Foundation/Foundation.h> // NSObject


@interface Car : NSObject
{
// 属性:成员变量(可以是基础类型,枚举,结构体和类对象指针)
@public
	int m_nSpeed;// 默认初始化为0
}


// 行为:方法(方法名,返回值,参数)
- (void)stop;
- (void)run:(int)speed; 				// 类型需要加小括号
- (bool)turnWithSpeed:(int)speed andDirection:(int)direction;
// 可以只有冒号- (bool)turn:(int)speed:(int)direction;
// 方法名包括冒号:turnWithSpeed:andDirection://turn:
@end

// Car.m
// 类的实现


@implementatiom Car 		// Car类名


- (void)stop
{
	NSLog(@"stop");
}


- (void)run:(int)speed
{
	self.m_nSpeed = speed;
	NSLog(@"run speed = %d", self.m_nSpeed);
}


- (bool)turnWithSpeed:(int)speed andDirection:(int)direction //- (bool)turn:(int)speed:(int)direction
{
	self.m_nSpeed = speed;
	NSLog(@"run speed = %d, direction = %d", self.m_nSpeed, direction);


	return YES
}


@end


// main.m
// 类的使用


#import "Car.h"


int main()
{
	Car* car = [Car new];	// 创建类的实例
	car->m_nSpeed = 60;		// 类属性访问
	[car stop]
	[car run:60]
	[car turnWithSpeed:60 andDirection:1]


	return 0;
}


iOS复习笔记3:类的基本定义

原文:http://blog.csdn.net/xufeng0991/article/details/43247037

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