标签:ios objective-c
// 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 objective-c
原文地址:http://blog.csdn.net/xufeng0991/article/details/43247037