Solidity

可以呼叫父類修飾符嗎?

  • August 24, 2018

我想做以下事情:

contract Base {
   bool public active;

   modifier canSetMetadata() {
       require(active == false);
       _;
   }
}

contract BaseWithMetadataControl is Base {
   address public metadataControl;

   modifier canSetMetadata() {
       if(msg.sender != metadataControl) {
           super.canSetMetadata();
       }
       _;
   }
}

所以子類應該呼叫父類的修飾符。這似乎不可能,但我不想複製修飾符程式碼,因為它可能會改變……

您可以覆蓋實現檢查的內部函式

pragma solidity 0.4.24;

contract Base {
   bool public active;

   modifier canSetMetadata() {
       require(canSetMetadata_());
       _;
   }

   function canSetMetadata_() internal view returns (bool) {
       return (active == false);
   }
}

contract BaseWithMetadataControl is Base {
   address public metadataControl;

   function canSetMetadata_() internal view returns (bool) {
       if(msg.sender != metadataControl) {
           return super.canSetMetadata_();
       } else {
           return true;
       }
   }
}

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