【发布时间】:2019-01-05 09:13:39
【问题描述】:
我正在尝试继承和覆盖父母的应付函数(并添加一些修饰符和逻辑),但到目前为止似乎无法这样做。
我的第一次直观尝试是这样的:
contract foo {
function test() public payable returns (uint256) {
return msg.value;
}
}
contract bar is foo {
function test() public payable returns (uint256) {
return super.test.value(msg.value / 2)();
}
}
并收到编译错误:
TypeError: Member "value" not found or not visible after argument-dependent lookup in function () - did you forget the "payable" modifier?
super.test.value(msg.value)();
给函数一个不同的名字也不起作用,所以这不是一个压倒一切的问题。
contract foo {
function test() public payable returns (uint256) {
return msg.value;
}
}
contract bar is foo {
function anotherTest() public payable returns (uint256) {
return super.test.value(msg.value / 2)();
}
}
我通过使用强制转换来欺骗编译器稍微走得更远,但这显然不好,因为会在运行时导致无限循环:
contract foo {
function test() public payable returns (uint256) {
return msg.value;
}
}
contract bar is foo {
function test() public payable returns (uint256) {
return foo(address(this)).test.value(msg.value / 2)();
}
}
有效的方法是将两者结合起来,即给它一个不同的名称和铸造:
contract foo {
function test() public payable returns (uint256) {
return msg.value;
}
}
contract bar is foo {
function anotherTest() public payable returns (uint256) {
return foo(address(this)).test.value(msg.value / 2)();
}
}
现在用 6 wei 调用 bar.test() 返回 6,调用 bar.anotherTest() 返回 3。但这并不是我想要的,因为它仍然通过 bar 公开 test()。
那么我做错了什么,或者这是设计使然?真的很想了解为什么 Solidity 会以这种方式运行,以及是否有解决方法来实现我想要的。谢谢!
【问题讨论】:
标签: inheritance parent ethereum solidity