Solidity

契約繼承

  • February 1, 2019

我真的希望這不是一個愚蠢的問題。

我試圖在這樣的結構中呼叫繼承契約的建構子:

contract A{
   uint a;

   constructor (uint _a) public {
       a = _a;
   }
}

contract B is A{
   uint b;

   constructor (uint _a, uint _b) public{
       A(_a);
       b = _b;
   }
}

contract C is B{
   uint c;

   constructor(uint _a, uint _b, uint _c) public {
       B(_a, _b);
       c = _c;
   }
}

如果我嘗試在 Remix 中編譯它,我會收到以下錯誤:

TypeError: Exactly one argument expected for explicit type conversion B(_a, _b);

為什麼我不能用兩個變數呼叫 B 的建構子?

我相信 Solidity 涵蓋了這個確切的場景:繼承 > 基本建構子的參數

這是他們給出的例子:

pragma solidity ^0.4.22;

contract Base {
   uint x;
   constructor(uint _x) public { x = _x; }
}

contract Derived2 is Base {
   constructor(uint _y) Base(_y * _y) public {}
}

所以對你來說,它看起來像這樣:

pragma solidity ^0.4.22;

contract A{
   uint a;

   constructor (uint _a) public {
       a = _a;
   }
}

contract B is A{
   uint b;

   constructor (uint _a, uint _b) A(_a) public{
       b = _b;
   }
}

contract C is B{
   uint c;

   constructor(uint _a, uint _b, uint _c) B(_a, _b) public {
       c = _c;
   }
}

引用自:https://ethereum.stackexchange.com/questions/48985