函数过载
合约内允许定义同名函数,但是输入参数不一致
pragma solidity ^0.4.17;
contract A {
function f(uint _in) public pure returns (uint out) {
out = 1;
}
function f(uint _in, bytes32 _key) public pure returns (uint out) {
out = 2;
}
}
pragma solidity ^0.4.16;
contract A {
function f(B _in) public pure returns (B out) {
out = _in;
}
function f(address _in) public pure returns (address out) {
out = _in;
}
}
contract B {
}
事件
// 事件可方便使用EVM的日志工具,通过dapp的用户接口调用回调函数进行监听。
pragma solidity ^0.4.0;
contract ClientReceipt {
event Deposit(
address indexed _from,
bytes32 indexed _id,
uint _value
);
function deposit(bytes32 _id) public payable {
// 任何调用当前函数的操作都会被检测到
Deposit(msg.sender, _id, msg.value);
}
}
var abi = /* abi as generated by the compiler */;
var ClientReceipt = web3.eth.contract(abi);
var clientReceipt = ClientReceipt.at("0x1234...ab67"); /* address */
var event = clientReceipt.Deposit();
event.watch(
function(error, result) {
if (!error)
console.log(result);
});
/*或者
var event = clientReceipt.Deposit(function(error, result) {
if (!error)
console.log(result);
});
*/
使用log低层接口
log0, log1, log2, log3, log4 ~ logN
pragma solidity ^0.4.10;
contract C {
function f() public payable {
bytes32 _id = 0x420042;
log3(
bytes32(msg.value),
bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20),
bytes32(msg.sender),
_id
);
}
}
用于理解事件的其他资源
接口
当合约继承其他合约,仅一个单独的合约被区位链建立,代码从基础合约复制到建立的合约中。
pragma solidity ^0.4.16;
contract owned {
address owner;
// 构造函数
function owned() {
owner = msg.sender;
}
}
// 继承合约owned的所有属性,但不能使用通过this进行外部访问
contract mortal is owned {
function kill() {
if (msg.sender == owner)
selfdestruct(owner);
}
}
// These abstract contracts are only provided to make the
// interface known to the compiler. Note the function
// without body. If a contract does not implement all
// functions it can only be used as an interface.
contract Config {
function lookup(uint id) public returns (address adr);
}
contract NameReg {
function register(bytes32 name) public;
function unregister() public;
}
contract named is owned, mortal {
function named(bytes32 name) {
Config config = Config(0xD5f9D8D94886E70b06E474c3fB14Fd43E2f23970);
NameReg(config.lookup(1)).register(name);
}
function kill() public {
if (msg.sender == owner) {
Config config = Config(0xD5f9D8D94886E70b06E474c3fB14Fd43E2f23970);
NameReg(config.lookup(1)).unregister();
mortal.kill();
}
}
}
contract PriceFeed is owned, mortal, named("GoldFeed") {
function updateInfo(uint newInfo) public {
if (msg.sender == owner)
info = newInfo;
}
function get() public view returns(uint r) {
return info;
}
uint info;
}
pragma solidity ^0.4.0;
contract owned {
address owner;
function owned() public {
owner = msg.sender;
}
}
contract mortal is owned {
function kill() public {
if (msg.sender == owner)
selfdestruct(owner);
}
}
contract Base1 is mortal {
function kill() public {
/* do cleanup 1 */
mortal.kill();
}
}
contract Base2 is mortal {
function kill() public {
/* do cleanup 2 */
mortal.kill();
}
}
contract Final is Base1, Base2 {
}
pragma solidity ^0.4.0;
contract owned {
function owned() public {
owner = msg.sender;
}
address owner;
}
contract mortal is owned {
function kill() public {
if (msg.sender == owner)
selfdestruct(owner);
}
}
contract Base1 is mortal {
function kill() public {
/* do cleanup 1 */
super.kill();
}
}
contract Base2 is mortal {
function kill() public {
/* do cleanup 2 */
super.kill(); }
}
contract Final is Base1, Base2 {
}