在Solidity中的对象组成

问题描述 投票:1回答:1

对象组合如何在Solidity中工作?我还没有找到全面的指南,所有示例似乎都处理了hello world level stuff或ERC20 token实现。

  1. 我可以与其他合同的公共财产签订合同吗?
  2. 这些合同实例可以作为合同之间的函数参数传递吗
oop solidity smartcontracts
1个回答
2
投票

对这两个问题都是肯定的 - 一个简单的例子来说明如下:

    contract HelperContract {
        function foo() public pure returns(uint) {
            return(0);
        }
    }

    contract MainContract {
        HelperContract helperContract;

        function MainContract(address helperAddress) public {
            helperContract = HelperContract(helperAddress);
        }

        function bar() public view returns(uint) {
            return helperContract.foo();
        }
    }

部署HelperContract,然后使用现在部署的HelperContract地址创建MainContract实例,我们可以调用bar,后者将调用foo

您可以将此代码复制粘贴到remix中并快速验证是否存在。

如果你想看到Hello world!类型之外的真实世界的例子,你可以查看CryptoKittieshere,他们使用这种类型的模式。

这是相当多的代码,但你可以寻找KittyAuction合同,其中包含方法setSaleAuctionAddresssetSiringAuctionAddress。这些函数分别设置了对单独部署的SaleClockAuctionSiringClockAuction合约的引用。

© www.soinside.com 2019 - 2024. All rights reserved.