solidity语言2

it2022-05-05  219

solidity语言2

变量类型(Value Types)

# 布尔型 关键字 bool 值 true , false 操作符 !, &&, ||, ==, != # 整型 关键字 int(int256), uint(uint256), uint8 ~ uint256(以8单位步进), int8 ~ int256 (以8单位步进) 比较操作符 <=, <, ==, !=, >, >= 算术操作符 +,-,unary -,unary +,*,/,%,**,<<,>> x << y 等价于 x * 2**y x >> y 等价于 x / 2**y 位操作符 &,!,^,~ # 浮点型 关键字 fixed(fixed128x19),ufixed(ufixed128x19),fixedMxN(M整数 8~256,以8步进;N小数 0 ~ 80),ufixedMxN(M整数 8~256,以8步进;N小数 0 ~ 80) 比较操作符 <=, <, ==, !=, >, >= 算术操作符 +,-,unary -,unary +,*,/,%,* # 地址型 关键字 address(20字节,以太地址长度) 操作符 <=, <, ==, !=, >, >= 成员属性 balance(查询地址的余额),transfer(发送以太币wei到一个地址) address x = 0x123; address myAddress = this; if (x.balance < 10 && myAddress.balance >= 10) x.transfer(10); // 我的余额大于10并且x的余额小于10,转给x以太10wei // 假如x是合约地址,它的代码将同transfer的调用一起执行,当没有gas或者执行失败,回退转账操作,当前合约抛出异常停止运行 send是transfer的底层实现,当执行失败,当前合约抛出异常但不停止运行,返回false # 使用send有风险:如果调用堆栈深度到1024,或者gas用尽,会失败。为了安全,转账要一直检查send操作的返回值,使用transfer甚至更好 address nameReg = 0x72ba7d8e73fe8eb666ea66babc8116a41bfb10e2; nameReg.call("register", "MyName"); nameReg.call(bytes4(keccak256("fun(uint256)")), a); // call 返回true,指示调用函数中断,返回false,引起EVM抛出异常 namReg.call.gas(1000000)("register", "MyName"); nameReg.call.value(1 ether)("register", "MyName"); nameReg.call.gas(1000000).value(1 ether)("register", "MyName"); callcode delegatecall # 固定大小的字节数组 关键字 bytes1 ~ bytes32, bytes(bytes1) 比较操作符 <=, <, ==, !=, >, >= 位操作符 &,!,^,~,<<,>> 索引访问 x[k], 0 <= k < I 成员属性 length # 动态大小的字节数组 关键字 bytes,string(UTF-8-encoded) # 地址常量(Address Literals) 0xdCad3a6d3569DF655070DEd06cb7A1b2Ccd1D3AF # 整数常量 2e10, -2e10, 2e-10, 2.5e1 (2**800 + 1) - 2**800 # 字符串常量 "foo", 'bar' # 十六进制常量 hex"001122FF" # 枚举(Enums) pragma solidity ^0.4.16; contract test { enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill } ActionChoices choice; ActionChoices constant defaultChoice = ActionChoices.GoStraight; function setGoStraight() public { choice = ActionChoices.GoStraight; } function getChoice() public view returns (ActionChoices) { return choice; } function getDefaultChoice() public pure returns (uint) { return uint(defaultChoice); } } posted on 2018-02-27 15:29 北京涛子 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/liujitao79/p/8479125.html


最新回复(0)