Objective-C 编码实践:自写类和枚举方式输出新建类的各个成员
来源:互联网 发布:sql server分离mdf 编辑:程序博客网 时间:2024/05/16 20:31
StockHolding.h
main.m
#import <Foundation/Foundation.h>@interface StockHolding : NSObject{ float purchaseSharePrice; float currentSharePrice; int numberOfShares;}@property float purchaseSharePrice;@property float currentSharePrice;@property int numberOfShares;-(float)costInDollars;-(float)valueInDollars;@end
StockHolding.m
这里有个很神奇的事情就是自动生成get、set方法的变量引用时,需要"_"开头
#import "StockHolding.h"@implementation StockHolding-(float)costInDollars{ float cost = _purchaseSharePrice * _numberOfShares; return cost;}-(float)valueInDollars{ return _currentSharePrice * _numberOfShares;}@end
main.m
一定不要误用关键字
#import <Foundation/Foundation.h>#import "StockHolding.h"int main(int argc, const char * argv[]){ @autoreleasepool { StockHolding *StockHonding1 = [[StockHolding alloc] init]; [StockHonding1 setNumberOfShares:40]; [StockHonding1 setPurchaseSharePrice:2.30]; [StockHonding1 setCurrentSharePrice:4.50]; StockHolding *StockHonding2 = [[StockHolding alloc] init]; [StockHonding2 setNumberOfShares:40]; [StockHonding2 setPurchaseSharePrice:2.30]; [StockHonding2 setCurrentSharePrice:4.50]; StockHolding *StockHonding3 = [[StockHolding alloc] init]; [StockHonding3 setNumberOfShares:40]; [StockHonding3 setPurchaseSharePrice:2.30]; [StockHonding3 setCurrentSharePrice:4.50]; NSMutableArray *dataList = [NSMutableArray array]; [dataList addObject:StockHonding1]; [dataList addObject:StockHonding2]; [dataList addObject:StockHonding3]; for(StockHolding *s in dataList){ NSLog(@"the StockHolding number is %d",[s numberOfShares]); NSLog(@"the StockHolding purchase shares price is %.2f",[s purchaseSharePrice]); NSLog(@"the StockHolding current share Price is %.2f",[s currentSharePrice]); NSLog(@"the StockHolding cost in dollars is %.2f",[s costInDollars]); NSLog(@"the StockHolding value in dollars is %.2f",[s valueInDollars]); } NSLog(@"Hello, World!");// NSArry *dataList = [Stock] } return 0;}
0 0
- Objective-C 编码实践:自写类和枚举方式输出新建类的各个成员
- 对于Objective-C新建类的理解
- Objective-C类的静态数据成员
- 各个编码方式的由来和区别
- Objective-C定义枚举类型的几种方式
- Objective-C枚举的几种定义方式与使用
- objective-c中类似ruby枚举类的实例方法
- objective-c 读取文本文件编码方式(stringWithContentsOfFile和initWithContentsOfFile)
- iOS --- Objective-C中类的成员变量与属性
- Objective-C的单例模式如何新建和测试?
- objective-c没有类静态成员变量
- Objective-C类成员变量深度剖析
- Objective-C类成员变量深度剖析
- Objective-C类成员变量深度剖析
- Objective-C成员变量声明方式探究
- objective-c 的枚举用法
- Objective-C 枚举的使用
- Objective-C 类,数据成员,函数成员,对象,
- 学生成绩排序
- mysql行列转换方法总结
- MyEclipse 9.0的 破解 图解
- 逻辑回归概述
- #include
- Objective-C 编码实践:自写类和枚举方式输出新建类的各个成员
- 自动旋转和调整大小
- QQ音乐上的音频格式
- cocos2d-x C++的do...while(0)解释
- 面试礼仪
- hdu 2209 翻纸牌游戏
- NYOJ737石子合并(一)
- 括号匹配(二)
- Digit Problem