solidity以太坊合约之继承【父类构造方法都会强制执行】
无参构造方式:使用is继承即可,无需重新指定
有参构造方式:使用is继承之后,还需要指定参数【两种参数初始化方法】
pragma solidity >=0.5.0 <0.7.0;
contract A {
event LogA(string test);
constructor() public {
emit LogA("执行了A合约");
}
}
contract B is A {
event LogB(string test);
constructor() public {
emit LogB("执行了B合约");
}
}
contract X {
event LogD(string test);
constructor() public {
emit LogD("执行了X合约");
}
}
contract C is B,X {
event LogC(string test);
constructor() public {
emit LogC("执行了C合约");
}
}
合约部署后执行结果【父类构造函数全部执行】:
[
{
"from": "0xef55bfac4228981e850936aaf042951f7b146e41",
"topic": "0x2c97ef1bf565d0aa27910658d6868f60344c016f7d84ccec59549e8780043d34",
"event": "LogA",
"args": {
"0": "执行了A合约",
"test": "执行了A合约",
"length": 1
}
},
{
"from": "0xef55bfac4228981e850936aaf042951f7b146e41",
"topic": "0xbc227f4cc6bf66a4720ae5c43efba09560823aa859c6f6afb05549a257b2db85",
"event": "LogB",
"args": {
"0": "执行了B合约",
"test": "执行了B合约",
"length": 1
}
},
{
"from": "0xef55bfac4228981e850936aaf042951f7b146e41",
"topic": "0xdf68300bec9878a850aecba9659afb363f9a69b0a67b57d69c7eddd2b66fb855",
"event": "LogD",
"args": {
"0": "执行了X合约",
"test": "执行了X合约",
"length": 1
}
},
{
"from": "0xef55bfac4228981e850936aaf042951f7b146e41",
"topic": "0x07093231bee6b246869b33e85368e0a59341a93fcf169eb12eeed1705aedb1fb",
"event": "LogC",
"args": {
"0": "执行了C合约",
"test": "执行了C合约",
"length": 1
}
}
]