// 使用 get 前缀
function getBalance() public view returns (uint256)
function getOwner() public view returns (address)
参数验证
function getItem(uint256 index) public view returns (uint256) {
require(index < items.length, "Index out of bounds");
return items[index];
}
返回值类型
// 返回单个值
function getPrice() public view returns (uint256)
// 返回多个值
function getDetails() public view returns (uint256, string memory, bool)
// 返回结构体
function getInfo() public view returns (Item memory)
常见使用场景
余额查询
function getBalance(address account) public view returns (uint256) {
return balances[account];
}
状态检查
function isActive() public view returns (bool) {
return status == Status.Active;
}
数据验证
function isValid(address user) public view returns (bool) {
return user != address(0) && !isBlacklisted[user];
}