// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract HelloWorld {
// 字符串类型变量声明
string public myStr = "hellowolrd";
// 布尔值变量定义
bool public b = true;
/*
有符号与无符号整数类型
*/
// uint 默认为 uint256,取值范围从 0 到 2**256 - 1
// 也可使用更小的类型如 uint8、uint16 等
uint public u = 123;
// int 默认为 int256,支持负数
// 示例中使用了 int 类型的变体
int public i = -123;
// 获取 int 类型的最小值和最大值
int public mintInt = type(int).min;
int public maxInt = type(int).max;
// 地址类型变量定义
address public addr = 0xd9145CCE52D386f254917e481eB44e9943F39138;
// bytes32 类型用于存储32字节的十六进制数据
// 可以直接赋值任意32字节的十六进制数
// 若长度不足,右侧自动补零
// 例如:0x1234 实际等价于在后面添加60个零
bytes32 public b32 = 0x0;
// 其他可能的 bytes32 赋值方式(注释示例):
// 零值表示
// bytes32 public b32_zero = 0x0;
// 完整32字节十六进制
// bytes32 public b32_hex = 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef;
// 字符串转换为 bytes32
// bytes32 public b32_str = bytes32("hello");
// 通过哈希函数生成
// bytes32 public b32_hash = keccak256(abi.encode("example"));
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract Function {
// 加法函数定义
// 使用 external 表示该函数可通过外部调用
// pure 表示不读取或修改状态变量,仅基于输入进行计算
function add(uint x, uint y) external pure returns (uint) {
return x + y;
}
// 减法函数实现
// 同样标记为 external 和 pure
// 先将结果存入局部变量,再返回
function sub(uint x, uint y) external pure returns (uint) {
uint ret = x - y;
return ret;
}
}