Solidity初步以及小试
ps:文档网站:http://solidity-by-example.org
编译网站:http://remix.ethereum.org
首先在“contracts”文件中建立一个用来储存项目的文件,在项目文件中新建文件,取个名字,系统自动补齐后缀“sol”
然后开头写上
// SPDX-License-Identifier: MIT // compiler version must be greater than or equal to 0.8.24 and less than 0.9.0 pragma solidity ^0.8.26; //版本号
版本号根据实际情况来写,一三行必有!!!
contract HelloWorld { string public greet = "Hello World!"; //定义一个名叫greet的公开的字符串 } //定义当前合约名叫“HelloWorld”
如示:”类型+权限+名“这样的定义方式。
之后
contract Coutner {
//unit默认值为0 uint public count=18; //建立了一个名为“get”的函数 function get() public view returns(uint){ //确定返回类型
return (count * count); //返回当前这个变量的值,读取这个变量数值
}
}
用“function”来定义函数,在部署中显示“count”的值为18,而“get”的值为“324”
如果继续添加
function inc() public { count +=1; //等价于 count = count+1; //在部署中每点击一次“inc”按钮”count“就会加一 //同样“get”函数中的值也会平方增长 }