You are viewing a single comment's thread from:

RE: Solidity开发指南

in #starnote3 days ago

1.合约对象,2.继承,3.接口, 4.低级调用,call delegatecall

1. 合约对象
contract FuncAdd {
   uint x = 263;
   function add(uint x, uint y) external returns(uint) { 
      return x;
   };
}

contract TestAdd {
    //用合约地址来实例化接口
    FuncAdd t = FuncAdd(0x3dA5048CE9384a35fF4F3AAF0B4804114e584039);
    function test(uint x, uint y) public returns(uint){
        return t.add(x, y);
    }
}

2.继承
继承A合约中的方法,可以直接调用。
contract A {
    uint public x;
    function setValue(uint _x) public {
        x = _x;
    }
}

contract B is A {
    function modify(uint _y) public {
        setValue(_y); //可以直接调用A合约中的方法
    }
}

3. 接口
接口有点类似抽象合约的功能,可用于两个合约间的调用。是非侵入式接口,也就是不用显式的调用接口。在ERC20合约中比较常见。
注意:B中引用A的接口,A的函数仍会在A的环境中执行。
interface IFuncAdd {
    function add(uint x, uint y) external returns(uint);
}
contract TestAdd {
    //用合约地址来实例化接口
    IFuncAdd t = IFuncAdd(0x3dA5048CE9384a35fF4F3AAF0B4804114e584039);
    function test(uint x, uint y) public returns(uint){
        return t.add(x, y);
    }
}