Import
Cú pháp import một file trong Solidity tương tự với JavaScript:
import "./someothercontract.sol";
contract newContract is SomeOtherContract {
}
Chúng ta cũng có thể sử dụng alias:
import "filename" as symbolName;
Hoặc chỉ import một phần:
import {symbol1 as alias, symbol2} from "filename";
The is
Keyword
Để kế thừa một contract, ta sử dụng từ khóa is
:
contract Doge {
function catchphrase() public returns (string memory) {
return "So Wow CryptoDoge";
}
}
contract BabyDoge is Doge {
function anotherCatchphrase() public returns (string memory) {
return "Such Moon BabyDoge";
}
}
Solidity có hỗ trợ multiple inheritance:
contract SatoshiNakamoto is NickSzabo, HalFinney {
// Omg, the secrets of the universe revealed!
}
Khi biên dịch, compiler sẽ gom tất cả các parent contract và child contract lại thành một contract duy nhất. Ta gọi contract này là flattened contract.
Inheritance with Constructor Parameters
Có hai cách để truyền đối số vào constructor của contract cha:
- Truyền vào trong câu lệnh kế thừa (khi sử dụng từ khóa
is
). - Truyền vào thông qua constructor.
Minh họa: