首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Solidity零基础学习心得

Solidity零基础学习心得

作者头像
XPcode7
发布2025-10-23 14:14:41
发布2025-10-23 14:14:41
1140
举报
Solidity零基础开始学习教学

最上面刚开始都要写一段小绿色的文字,那其实就是一个授权文件 1、这里是在选择编译器的版本

在这里插入图片描述
在这里插入图片描述

2、下面这个是选择部署当前合约

在这里插入图片描述
在这里插入图片描述

3、如何查看当前编译器的版本呢?

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.右侧自己输入的那一段意思是需要编译器版本在0.8.13版本之上,所以左侧的版本需要手动选择一个符合要求的(不然就是红色报错) 2.Contract HelloWorld就是定义合约的内容String是一个字符串的意思Public意思是公开的意思,说明这个字符串是公开的谁都可以访问greet就是变量名称这个就是自己定义的

Uint也是一个字符串意思是非负整数 这个基本的语法逻辑跟Java是相同的count+=1 意思是count=count+1

在这里插入图片描述
在这里插入图片描述

点击蓝色按钮只调用区块链的状态 但橙色按钮每点击一次就消耗一点ether(依ser)也叫消耗了一点(盖斯)费用,也叫消耗了一点以太坊

代码语言:javascript
复制
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Counter {
    uint public count;                      unit这个也就是字符串非负整数

    // Function to get the current count
    function get() public view returns (uint) {	   当前这一段的意思是:通过当前这段话读取前一句的count
        return count;
    }

    // Function to increment count by 1
    function inc() public {
        count += 1;
    }

    // Function to decrement count by 1
    function dec() public {
        // This function will fail if count = 0
        count -= 1;
    }
}
3 Primitive Data Types(原生数据类型)
代码语言:javascript
复制
Boolean    布尔
Uint			非负整数
Int			整数
Address		地址
1、新建一个合约
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Primitives {————————————Primitives合约的名称
    bool public boo = true;———————— bool(布尔)是solidity中的逻辑字符号(true/false)初始默认值是False
    /*
    uint stands for unsigned integer, meaning non negative integers
    different sizes are available
        uint8   ranges from 0 to 2 ** 8 - 1
        uint16  ranges from 0 to 2 ** 16 - 1
        ...
        uint256 ranges from 0 to 2 ** 256 - 1
    */
														Uint8是非负整数类型,最大值是2的8方-1(255)范围就是零到255
														Unit16 是2**16-1(65535),范围就是0~65535
Unit默认值也就是最大值最大范围2**256-1
    uint8 public u8 = 1;
    uint public u256 = 456;
    uint public u = 123; // uint is an alias for uint256

    /*
    Negative numbers are allowed for int types.
    Like uint, different ranges are available from int8 to int256
    
    int256 ranges from -2 ** 255 to 2 ** 255 - 1
    int128 ranges from -2 ** 127 to 2 ** 127 - 1
    */
    int8 public i8 = -1;
    int public i256 = 456;
    int public i = -123; // int is same as int256
												Int8是整数类型,最小值是-2**7最大值2**7-1共256个数字
    // minimum and maximum of int
    int public minInt = type(int).min;
    int public maxInt = type(int).max;

    address public addr =    0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;
		addr=就是地址的意思,交互对象账号地址

    /*
    In Solidity, the data type byte represent a sequence of bytes. 
    Solidity presents two type of bytes types :

     - fixed-sized byte arrays
     - dynamically-sized byte arrays.
     
     The term bytes in Solidity represents a dynamic array of bytes. 
     It’s a shorthand for byte[] .
    */
    bytes1 a = 0xb5; //  [10110101]
    bytes1 b = 0x56; //  [01010110]

    // Default values
    // Unassigned variables have a default value
    bool public defaultBoo; // false
    uint public defaultUint; // 0
    int public defaultInt; // 0
    address public defaultAddr; // 0x0000000000000000000000000000000000000000
				Bytes代表字节 (没整明白)
在这里插入图片描述
在这里插入图片描述

上面这段段话就是用的type方法,意思是变量a的unit16最大值是多少——(65535) 通过内置的type函数获取当前的类型变量的最大值最小值

4、变量的作用域

Local 本地的,是只存在函数内存当中,调用的时候才有 Blockchain存在区块链上的,需要消耗GAS(盖斯) state 这个存在区块链上消耗GAS global默认的全局变量,是整个以太坊自带的变量,关于区块链的一些信息

代码语言:javascript
复制
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Variables {
    // State variables are stored on the blockchain.
    string public text = "Hello";
    uint public num = 123;

    function doSomething() public {
        // Local variables are not saved to the blockchain.
        uint i = 456;

        // Here are some global variables
        uint timestamp = block.timestamp; // Current block timestamp   这个叫时间戳,上面这句话意思是1970年距离今天的秒数
        address(地址的意思开头) sender = msg.sender; // address of the caller      msg.sender调用当前的地址
    }
}
555、constant(常量)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
//constant就相当于使后面的变量不能被修改的,这样储存的时候可以减少GAS消耗
contract Constants {
    // coding convention to uppercase constant variables
    address public constant MY_ADDRESS = 0x777788889999AaAAbBbbCcccddDdeeeEfFFfCcCc;
    	// constant后面紧跟的值必须是大
    uint public constant MY_UINT = 123;
}

Immutable(不可篡改) 变量前加constructor这个修饰符意思就是刚开始设了的值就不可更改了而变量前加 Immtable修饰符说明他只能被constructor初始化函数修改,然后就不可再更改了

代码语言:javascript
复制
Reading and Writing to a State Variable
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract SimpleStorage {           定义一个变量,SimpleStorage(简单储存)
    // State variable to store a number
    uint public num;  //在区块链上部署了一个名为num的变量,占用一个位置

    // You need to send a transaction to write to a state variable.
    function set(uint _num) public {       这一段里面的_name是自己随便设置一个变量名字只是为了下面提取跟name一样的值
        num = _num;
    }

    // You can read from a state variable without sending a transaction.
    function get() public view returns (uint) {
        return num;
    }
}

什么情况下不会消耗GAS呢 只读取区块链的状态,变量仅仅是读取所以不会消耗GAS Ether and Wei 1ether等于10的18次方wei(wei是以太坊上的GAS最小的单位) // SPDX-License-Identifier: MIT pragma solidity ^0.8.20;

contract EtherUnits { uint public oneWei = 1 wei; // 1 wei is equal to 1 bool public isOneWei = 1 wei == 1; ——(==是判断两者是否相等,>=判断左侧是否大于右侧)

代码语言:javascript
复制
uint public oneEther = 1 ether;
// 1 ether is equal to 10^18 wei
bool public isOneEther = 1 ether == 1e18; 

}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-10-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Solidity零基础开始学习教学
  • 3 Primitive Data Types(原生数据类型)
  • 4、变量的作用域
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档