【Solidity】Interface的使用方法

使用标准的接口可以极大的拓展程序的功能。在solidity语言中,接口可以用来接受相同规则的合约,实现可更新的智能合约。

  • interface可以被继承
  • interface的function必须是external修饰符
  • interface 无法定义constructor
  • interface不能有变量

代码范例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
文件结构
├── Employee.sol
└── Company.sol
文件结构 ├── Employee.sol └── Company.sol
文件结构
├── Employee.sol
└── Company.sol

文件1:Employee.sol

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
interface IEmployee {
function setName(string memory _name) external;
function getName()external view returns(string memory);
}
contract Employee is IEmployee{
string private name;
function setName(string memory _name) public override{
name = _name;
}
function getName()public override view returns(string memory){
return name;
}
}
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 <0.9.0; interface IEmployee { function setName(string memory _name) external; function getName()external view returns(string memory); } contract Employee is IEmployee{ string private name; function setName(string memory _name) public override{ name = _name; } function getName()public override view returns(string memory){ return name; } }
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

interface IEmployee {
    function setName(string memory _name) external;
    function getName()external view returns(string memory);
}

contract Employee is IEmployee{
    string private name;
    function setName(string memory _name) public override{
        name = _name;
    }

    function getName()public override view returns(string memory){
        return name;
    }
}

文件2:Company.sol,
通过company 能够链接employee获取和更改employee的数据,必须部署employee合约先,然后通过employee的合约地址,继续部署Company合约

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import { IEmployee } from "./Employee.sol";
contract Company {
IEmployee employee;
constructor(address _employeeAddress){
employee = IEmployee(_employeeAddress);
}
function setName(string memory _name) public {
employee.setName(_name);
}
function getName() public view returns (string memory){
return employee.getName();
}
}
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 <0.9.0; import { IEmployee } from "./Employee.sol"; contract Company { IEmployee employee; constructor(address _employeeAddress){ employee = IEmployee(_employeeAddress); } function setName(string memory _name) public { employee.setName(_name); } function getName() public view returns (string memory){ return employee.getName(); } }
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

import { IEmployee } from "./Employee.sol";

contract Company {
    IEmployee employee;

    constructor(address _employeeAddress){
        employee = IEmployee(_employeeAddress);
    }

    function setName(string memory _name) public {
        employee.setName(_name);
    }

    function getName() public view returns (string memory){
        return employee.getName();
    }
}

Loading

Facebook评论