如果我們要從 local 的方式 import 檔案,可以假設 Folder Structure 如下:
├── Import.sol
└── Foo.sol
創建一個名為 Foo.sol 的檔案:
pragma solidity ^0.8.11;
struct Point {
uint x;
uint y;
}
error Unauthorized(address caller);
function add(uint x, uint y) pure returns (uint) {
return x + y;
}
contract Foo {
string public name = "Foo";
}
接下來創建一個名為 Import.sol 的檔案,並且在裡面 import Foo.sol:
pragma solidity ^0.8.10;
// import Foo.sol from current directory
import "./Foo.sol";
// import {symbol1 as alias, symbol2} from "filename";
import {Unauthorized, add as func, Point} from "./Foo.sol";
contract Import {
// Initialize Foo.sol
Foo public foo = new Foo();
// Test Foo.sol by getting it's name.
function getFooName() public view returns (string memory) {
return foo.name();
}
}
當然也可以繼承其他地方來的合約:
import "./someothercontract.sol";
contract newContract is SomeOtherContract {
}