当前位置:网站首页>solidty-基础篇-基础语法和定义函数
solidty-基础篇-基础语法和定义函数
2022-07-01 14:46:00 【new life1937】
基础语法
合约
Solidity 的代码都包裹在合约里面. 一份合约就是以太应币应用的基本模块, 所有的变量和函数都属于一份合约, 它是你所有应用的起点.
状态变量和整数
状态变量是被永久地保存在合约中。也就是说它们被写入以太币区块链中. 想象成写入一个数据库。
uint 无符号数据类型, 指其值不能是负数,
int 对于有符号的整数存在的数据类型。
string 字符串用于保存任意长度的 UTF-8 编码数据
数学运算
在 Solidity 中,数学运算很直观明了,与其它程序设计语言相同:
加法: x + y
减法: x - y,
乘法: x * y
除法: x / y
取模 / 求余: x % y (例如, 13 % 5 余 3, 因为13除以5,余3)
Solidity 还支持 乘方操作 (如:x 的 y次方) // 例如: 5 ** 2 = 25
结构体-更复杂的数据类型,有多个属性
struct 结构体允许你生成一个更复杂的数据类型,它有多个属性。
struct Person {
uint age;
string name;
}
数组
建立一个集合,可以用 数组_这样的数据类型. Solidity 支持两种数组: 静态 数组和_动态 数组:
// 固定长度为2的静态数组:
uint[2] fixedArray;
// 固定长度为5的string类型的静态数组:
string[5] stringArray;
// 动态数组,长度不固定,可以动态添加元素:
uint[] dynamicArray;
结构体类型的数组,状态变量被永久保存在区块链中。
Person[] people; // 这是动态数组,我们可以不断添加元素
公共数组
你可以定义 public 数组, Solidity 会自动创建 getter 方法. 语法如下:
Person[] public people;
其它的合约可以从这个数组读取数据(但不能写入数据),所以这在合约中是一个有用的保存公共数据的模式。
定义函数
在 Solidity 中函数定义的句法如下:
function eatHamburgers(string _name, uint _amount) {
}
这是一个名为 eatHamburgers 的函数,它接受两个参数:一个 string类型的 和 一个 uint类型的。现在函数内部还是空的。
我们的函数定义如下:
eatHamburgers("evelyn", 100);
边栏推荐
- [14. Interval sum (discretization)]
- Details of appium key knowledge
- [dynamic programming] p1004 grid access (four-dimensional DP template question)
- Provincial election + noi Part XI others
- Error-tf.function-decorated function tried to create variables on non-first call
- Ensure production safety! Guangzhou requires hazardous chemical enterprises to "not produce in an unsafe way, and keep constant communication"
- Phpcms realizes the direct Alipay payment function of orders
- MIT团队使用图神经网络,加速无定形聚合物电解质筛选,促进下一代锂电池技术开发
- Cannot link redis when redis is enabled
- After twists and turns, I finally found the method of SRC vulnerability mining [recommended collection]
猜你喜欢

Opencv interpolation mode

Salesforce、约翰霍普金斯、哥大 | ProGen2: 探索蛋白语言模型的边界

Don't want to knock the code? Here comes the chance

建立自己的网站(14)

Rearrangement of overloaded operators

数据湖系列之一 | 你一定爱读的极简数据平台史,从数据仓库、数据湖到湖仓一体

Take you to API development by hand

Websocket (simple experience version)
![[14. Interval sum (discretization)]](/img/e5/8b29aca7068a6385e8ce90c2742c37.png)
[14. Interval sum (discretization)]

JVM performance tuning and practical basic theory part II
随机推荐
Leetcode(69)——x 的平方根
JVM第一话 -- JVM入门详解以及运行时数据区分析
[零基础学IoT Pwn] 复现Netgear WNAP320 RCE
Quelle valeur le pdnp peut - il apporter aux gestionnaires de produits? Vous savez tout?
qt捕获界面为图片或label显示
[leetcode 324] swing sorting II thinking + sorting
券商万1免5证券开户是合理安全的吗,怎么讲
JVM performance tuning and practical basic theory part II
Semiconductor foundation of binary realization principle
[14. Interval sum (discretization)]
【15. 区间合并】
网速、宽带、带宽、流量三者之间的关系是什么?
问题随记 —— Oracle 11g 卸载
What are the requirements for NPDP product manager international certification registration?
Research Report on the development trend and competitive strategy of the global high temperature label industry
30 Devops interview questions and answers
Research Report on development trend and competitive strategy of global vibration polishing machine industry
Research Report on the development trend and competitive strategy of the global powder filling machine industry
[zero basic IOT pwn] reproduce Netgear wnap320 rce
Buuctf reinforcement question ezsql