W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
接口類似于抽象合約,但它們不能實現(xiàn)任何功能。還有更多限制:
未來可能會取消其中一些限制。
接口基本上僅限于 Contract ABI 可以表示的內(nèi)容,ABI 和接口之間的轉(zhuǎn)換應(yīng)該是可能的,不會有任何信息丟失。
接口由它們自己的關(guān)鍵字表示:
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.2 <0.9.0; interface Token { enum TokenType { Fungible, NonFungible } struct Coin { string obverse; string reverse; } function transfer(address recipient, uint amount) external; }
合約可以像繼承其他合約一樣繼承接口。
接口中聲明的所有函數(shù)都是隱式聲明的virtual,任何覆蓋它們的函數(shù)都不需要override關(guān)鍵字。這并不自動意味著一個覆蓋函數(shù)可以再次被覆蓋——這只有在覆蓋函數(shù)被標記的情況下才有可能virtual。
接口可以從其他接口繼承。這與普通繼承具有相同的規(guī)則。
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.2 <0.9.0; interface ParentA { function test() external returns (uint256); } interface ParentB { function test() external returns (uint256); } interface SubInterface is ParentA, ParentB { // Must redefine test in order to assert that the parent // meanings are compatible. function test() external override(ParentA, ParentB) returns (uint256); }
在接口和其他類似合約的結(jié)構(gòu)中定義的類型可以從其他合約訪問:Token.TokenType或Token.Coin.
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: