Value使用
来源:互联网 发布:利率敏感性缺口 数据 编辑:程序博客网 时间:2024/06/07 05:36
// 使用integer初始化Value
Value val1(65);
// 使用float初始化Value
//Value val1(3.4f);
// 使用double初始化Value
//Value val1(3.5);
log("The description of the integer value:%s",val1.getDescription().c_str());//整型转换为字符串型
log("val1.asByte() = %c",val1.asByte());//字节转化,结果为A
//----------------------------------------------------
std::string strV = "fkava";
// 使用string初始化Value
Value val2(strV);
log("The description of the string value:%s",val2.getDescription().c_str());
//----------------------------------------------------
auto sp0 = Sprite::create();
Vector<Ref*>* vecV = new Vector<Ref*>();
vecV->pushBack(sp0);
// 使用Vector初始化Value
Value val3(vecV);
log("The description of the Vector value:%s",val3.getDescription().c_str());
delete vecV;
//----------------------------------------------------
Map<std::string, Ref*>* mapV = new Map<std::string, Ref*>();
mapV->insert(strV,sp0);
// 使用Map初始化Value
Value val4(mapV);
log("The description of the Map value:%s",val4.getDescription().c_str());
delete mapV;
//----------------------------------------------------
// 使用Map初始化Value
Value val6(&val4);
log("The description of the Value-type value:%s",val6.getDescription().c_str());
//----------------------------------------------------
// 将val1赋值给val2
val2 = val1;
log("operator-> The description of val2:%s",val2.getDescription().c_str());
// 直接分配值
val2 = 4;
log("operator-> The description of val4:%s",val2.getDescription().c_str());
cocos2d::Value是一个包含了很多原生类型(int,float,double,bool,unsigned char,char*和std::string)外加std::vector<Value>, std::unordered_map<std::string,Value>和
std::unordered_map<int,Value> 的类。
你可以把所有上面的提及的原生类型放入 cocos2d::Value 对象中,然后将它们转化为对应的原生类型。
Value val; // 调用默认构造函数
if (val.isNull()) {
log("val is null");
}else{
std::string str =val.getDescription();
log("The description of val0:%s",str.c_str());
}
//----------------------------------------------------
Value val1(65); // 用一个 int初始化
//Value val1(3.4f); // 用一个 float初始化
//Value val1(3.5); // 用一个 double初始化
log("The description of the integer value:%s",val1.getDescription().c_str());
log("val1.asByte() = %c",val1.asByte());
//----------------------------------------------------
std::string strV = "string";
Value val2(strV); // 用 string初始化
log("The description of the string value:%s",val2.getDescription().c_str());
//----------------------------------------------------
auto sp0 = Sprite::create();
Vector<Object*>* vecV = new Vector<Object*>();
vecV->pushBack(sp0);
Value val3(vecV); // 用 Vector初始化
log("The description of the Vector value:%s",val3.getDescription().c_str());
delete vecV;
//----------------------------------------------------
Map<std::string, Object*>* mapV = new Map<std::string, Object*>();
mapV->insert(strV,sp0);
Value val4(mapV); // 用 Map初始化
log("The description of the Map value:%s",val4.getDescription().c_str());
delete mapV;
//----------------------------------------------------
Value val6(&val4); // 用 Map初始化
log("The description of the Value-type value:%s",val6.getDescription().c_str());
//----------------------------------------------------
val2 = val1; // 在两个不同指类型间赋值
log("operator-> The description of val2:%s",val2.getDescription().c_str());
val2 = 4; // 直接赋值
log("operator-> The description of val4:%s",val2.getDescription().c_str());
输出:
cocos2d: val is null
cocos2d: The description of the integer value:
65
cocos2d: val1.asByte() = A
cocos2d: The description of the string value:
string
cocos2d: The description of the Vector value:
true
cocos2d: The description of the Map value:
true
cocos2d: The description of the Value-type value:
true
cocos2d: operator-> The description of val2:
65
cocos2d: operator-> The description of val4:
Value的作用和用法:在创建Value时,往构造函数里传入一个值,Value就会自动根据这个值来决定自己的类型。在获取Value的值时,就根据它的类型,调用as**函数获取。
整数、浮点型和字符串之间的转换
整型转为字符串: std::string str = "NO"+Value(1).asString();
字符串转为整型:log("%d",Value("1234").asInt())
浮点型转字符串:log("%s",Value(123.5f).asString().c_str())
字符串转浮点型:log("%f",Value("14.45").asFloat())
- Value使用
- AngularJs 使用 .value / .constant
- Spring @Value 使用
- Spring @Value使用示例
- @Value初使用小记
- 使用@Value无法注入
- Spring 3使用@Value
- Cocos3.1Value的使用
- Spring @Value注解的使用
- javascript中使用key-value
- spring boot使用@Value,@PropertySource注解使用
- value
- value
- @Value
- Value
- Value
- Value
- Value
- 归并排序求逆序对
- PAT1003. 我要通过!
- Wayland与Weston简介
- GIT工作流
- Cocos2dx中类库中Vector<T>容器的使用
- Value使用
- 提升网站权重的SEO优化基础,你会吗?
- 免费全功能响应式模板:黑暗元素
- 深度学习简易应用demo
- 【JAVA】google protobuf 3.0 安装使用
- SQL Server 中判断对象是否存在
- Sprite和SpriteFrameCache的使用
- ubuntu 安装分区
- 金蝶K3发票系统与航天金税系统对接批量打印发票功能实现