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

来源:互联网 发布:淘宝如何设置限时促销 编辑:程序博客网 时间:2024/05/17 06:11
// Car.h// 类的声明// 类名:Car// 属性:m_nSpeed// 行为:run#import <Foundation/Foundation.h> // NSObject@interface Car : NSObject{// 属性:成员变量(可以是基础类型,枚举,结构体和类对象指针)@publicint 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;}


0 0
原创粉丝点击