solidity重点总结

本文总结了Solidity编程中的关键概念,包括常量修饰符、函数调用权限(external与internal)、引用类型(如数组、字符串、结构体)以及地址Address的使用。介绍了msg.sender和this的含义,以及address类型的balance、transfer和send方法。还讨论了字符串处理、可变长度数组的声明与操作,以及结构体和mapping在存储中的应用。
摘要由CSDN通过智能技术生成

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';
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值