为什么将现有的实体合同实例作为参数传递给合同构造函数

时间:2020-10-11 14:08:38

标签: ethereum smartcontracts

对于https://ethereumdev.io/interact-with-other-contracts-from-solidity/中的以下代码段

contract CounterFactory {
 
    mapping(address => Counter) _counters;

    function createCounter() public {
        require (_counters[msg.sender] == Counter(0));
        _counters[msg.sender] = new Counter(msg.sender);
    }
    
    function increment() public {
        require (_counters[msg.sender] != Counter(0));
        Counter(_counters[msg.sender]).increment(msg.sender);
        // The following line also works as usual.
        // _counters[msg.sender].increment(msg.sender);
    }
    
    function getCount(address account) public view returns (uint256) {
        require (_counters[account] != Counter(0));
        return (_counters[account].getCount());
    }
    
    function getMyCount() public view returns (uint256) {
        return (getCount(msg.sender));
    }
 
}

为什么这一行(将constact实例传递给构造函数)起作用? Counter(_counters[msg.sender]).increment(msg.sender);

我正在https://remix.ethereum.org/上使用Remix IDE

0 个答案:

没有答案