Code:
pragma solidity ^0.8.21;
contract HelloWeb3{
string public _string = "Hello Web3!";
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;
contract HelloWeb3{
//bool类型
bool public _bool = true;
bool public _bool1 = !_bool; // 取非
bool public _bool2 = _bool && _bool1; // 与
bool public _bool3 = _bool || _bool1; // 或
bool public _bool4 = _bool == _bool1; // 相等
bool public _bool5 = _bool != _bool1; // 不相等
//当||时,前者true直接true,当&&时前者false直接false
bool public _bool6 = _bool || _bool1;
bool public _bool7 = _bool1 && _bool;
//整型
int public _int = -1;
uint public _uint = 1;
uint256 public _number = 20220330;
// 整数运算
uint256 public _number1 = _number + 1; // +,-,*,/
uint256 public _number2 = 2**2; // 指数
uint256 public _number3 = 7 % 2; // 取余数
bool public _numberbool = _number2 > _number3; // 比大小
// 地址
address public _address = 0x7A58c0Be72BE218B41C608b7Fe7C5bB630736C71;
address payable public _address1 = payable(_address); // payable address,可以转账、查余额
// 地址类型的成员
uint256 public balance = _address1.balance; // balance of address
// 固定长度的字节数组
bytes32 public _byte32 = "MiniSolidity";
bytes1 public _byte = _byte32[0];
// 用enum将uint 0, 1, 2表示为Buy, Hold, Sell
enum ActionSet { Buy, Hold, Sell }
// 创建enum变量 action
ActionSet action = ActionSet.Buy;
// enum可以和uint显式的转换
function enumToUint() external view returns(uint){
return uint(action);
}
}
pragma solidity ^0.8.21;
contract FunctionTypes{
uint256 public number = 5;
// 默认function
function add() external{
number = number + 1;
}
// pure: 纯纯牛马
function addPure(uint256 _number) external pure returns(uint256 new_number){
new_number = _number + 1;
}
// view: 看客
function addView() external view returns(uint256 new_number) {
new_number = number + 1;
}
// internal: 内部函数
function minus() internal {
number = number - 1;
}
// 合约内的函数可以调用内部函数
function minusCall() external {
minus();
}
// payable: 递钱,能给合约支付eth的函数
function minusPayable() external payable returns(uint256 balance) {
minus();
balance = address(this).balance;
}
}
view
函数可以读取状态变量,但不能改写;pure
函数既不能读取也不能改写状态变量。