--- title: 'Solidity 註解' lang: zh-tw --- Solidity 註解 === ## 簡介 在Solidity中,除了普通註解外,還有一些特殊註解來提供一些信息給開發者或工具使用。這些註解通常會在三斜線(`///`)或是多行註解(`/** ... */`)中使用。 ## 註解分類 有以下幾種: - `@dev`: 專門用來寫開發者的註解,用於描述合約或函數的邏輯與規則。 ```javascript! /// @dev This function adds two numbers together. function add(uint256 a, uint256 b) public pure returns (uint256) { return a + b; } ``` - `@param`: 用來描述函數的參數。可以使開發者理解參數用途。 ```javascript! /// @dev Adds two numbers. /// @param a The first number. /// @param b The second number. function add(uint256 a, uint256 b) public pure returns (uint256) { return a + b; } ``` - `@return`: 描述函數返回值。 ```javascript! /// @dev Adds two numbers. /// @param a The first number. /// @param b The second number. /// @return The sum of a and b. function add(uint256 a, uint256 b) public pure returns (uint256) { return a + b; } ``` - `@notice`: 提供使用者或終端用戶看的高層次簡介,通常是給合約或函數的簡單描述,適合合約的公開接口文件。 ```javascript! /// @notice Adds two numbers together and returns the result. function add(uint256 a, uint256 b) public pure returns (uint256) { return a + b; } ``` - `@inheritdoc`: 用來從父類繼承註解或描述,這在繼承合約或函數時很有用,避免重複描述。 ```javascript! /// @inheritdoc ParentContract function someFunction() public override { // function implementation } ``` - `@custom`: 自定義標籤,用來提供額外的信息。你可以隨意命名這些標籤來描述特定的用途或元數據。 ```javascript= /// @custom:info This is some custom information for this function. function myFunction() public { // function implementation } ``` :::warning 這些標籤大多數是為了增強程式碼的可讀性,或者在生成文件(如 NatSpec 文檔)時使用。 :::