📅  最后修改于: 2020-11-04 04:20:24             🧑  作者: Mango
Solidity支持三种类型的变量。
统一性是一种静态类型的语言,这意味着在声明过程中需要指定状态或局部变量类型。每个声明的变量始终具有基于其类型的默认值。没有“未定义”或“空”的概念。
其值永久存储在合同存储中的变量。
pragma solidity ^0.5.0;
contract SolidityTest {
uint storedData; // State variable
constructor() public {
storedData = 10; // Using State variable
}
}
变量的值仅在定义它的函数中可用。函数参数始终在该函数本地。
pragma solidity ^0.5.0;
contract SolidityTest {
uint storedData; // State variable
constructor() public {
storedData = 10;
}
function getResult() public view returns(uint){
uint a = 1; // local variable
uint b = 2;
uint result = a + b;
return result; //access the local variable
}
}
pragma solidity ^0.5.0;
contract SolidityTest {
uint storedData; // State variable
constructor() public {
storedData = 10;
}
function getResult() public view returns(uint){
uint a = 1; // local variable
uint b = 2;
uint result = a + b;
return storedData; //access the state variable
}
}
使用Solidity First Application一章中提供的步骤运行上述程序。
0: uint256: 10
这些是全局工作空间中存在的特殊变量,可提供有关区块链和交易属性的信息。
Name | Returns |
---|---|
blockhash(uint blockNumber) returns (bytes32) | Hash of the given block – only works for 256 most recent, excluding current, blocks |
block.coinbase (address payable) | Current block miner’s address |
block.difficulty (uint) | Current block difficulty |
block.gaslimit (uint) | Current block gaslimit |
block.number (uint) | Current block number |
block.timestamp (uint) | Current block timestamp as seconds since unix epoch |
gasleft() returns (uint256) | Remaining gas |
msg.data (bytes calldata) | Complete calldata |
msg.sender (address payable) | Sender of the message (current caller) |
msg.sig (bytes4) | First four bytes of the calldata (function identifier) |
msg.value (uint) | Number of wei sent with the message |
now (uint) | Current block timestamp |
tx.gasprice (uint) | Gas price of the transaction |
tx.origin (address payable) | Sender of the transaction |
在Solidity中命名变量时,请牢记以下规则。