solidity的constant修饰符代表不能用函数修改任何数据.
external只能外部调用,internal只能内部调用,如果函数中写了this....则表明这个函数必须从外部调用,但是这种写法不常用,按照大佬的回答做就行了
As for best practices, you should use external
if you expect that the function will only ever be called externally, and use public
if you need to call the function internally. It almost never makes sense to use the this.f()
pattern, as this requires a real CALL
to be executed, which is expensive. Also, passing arrays via this method would be far more expensive than passing them internally.
引用类型的变量类型
引用类型为:数组,字符串,结构体等
当引用类型作为函数参数的时候,其类型默认为memory,memory类型的变量会临时拷贝一份值存储到内存中。函数参数用memory类型的时候相当于值传递,而storage类型则是指针传递。storage会改变区块的内容,而storage并不会
一个编程实例:
memory------无法改变合约中的值
pragma solidity ^0.4.4;
contract Person {
string public _name;
function Person() {
_name = "liyuechun";
}
function f() {
modifyName(_name);
}
function modifyName(string storage name) {
var name1 = name;
bytes(name1)[0] = 'L';
}
}
storage-------可以改变合约中的值
pragma solidity ^0.4.4;
contract Person {
string public _name;
function Person() {
_name = "liyuechun";
}
function f() {
modifyName(_name);
}
function modifyName(string storage name) internal {
var name1 = name;
bytes(name1)[0] = 'L';
}
}