https://ethereum.github.io/browser-solidity
在上一章中部署在Geth上的源代码如下:
pragma solidity 0.4.9;
contract DemoTypes {
function f(uint a) returns (uint b)
{
uint result = a * 8;
return result;
}
}
> a_demotypes.f.call(100)
800
> a_demotypes.f.call(125)
1000
这个最简单的智能合约代码如下:
pragma solidity 0.4.9;
contract DemoTypes {
function f(uint a) returns (uint b)
{
uint result = a * 8;
return result;
}
}
pragma solidity 0.4.9;
pragma solidity
中pragma
是关键词,代表程序开始,solidity
代表本智能合约是由Solidity语言所撰写0.4.9
代表的是编译器版本,注意:从0.4.9起可以在前面不打^,0.4.8/0.4.7等版本还是需要打^contract DemoTypes {
...
}
这里引用一段说明Solidity里的Contract
Contracts in Solidity are similar to classes in object-oriented languages. They contain persistent data in state variables and functions that can modify these variables. Calling a function on a different contract (instance) will perform an EVM function call and thus switch the context such that state variables are inaccessible. (引用自 here)
中文翻译:
Solidity中Contract和面向对象语言中的类很相像。有带持久数据的变量,以及能改变这些变量的function. 在不同的Contract实例中调用一个function,将会执行一个在EVM(以太坊虚拟机)中的function调用。
function f(uint a) returns (uint b)
{
...
}
function f(uint a) returns (uint b)
代表定义了一个名为f
的方法,输入变量为uint a
, 输出为uint b
负2的128次方到正2的128次方
function f(uint a) returns (uint b)
{
uint result = a * 8;
return result;
}
Solidity is a statically typed language, which means that the type of each variable (state and local) needs to be specified (or at least known – see Type Deduction below) at compile-time. Solidity provides several elementary types which can be combined to form complex types. 引用自here
uint result = a * 8;
, solidity 鼓励在操作符中有一个空格。More than one space around an assignment or other operator to align with 引用自here
Yes:
x = 1;
y = 2;
long_variable = 3;
更多关于Solidity的编码风格,请参考官方文档 https://solidity.readthedocs.io/en/develop/style-guide.html
QQ群:559649971 (区块链学堂粉丝群)
个人微信:steven_k_colin
获取最新区块链咨询,请关注《以太中文网》微信公众号:以太中文网