ethereum(以太坊)(一)

Posted on 2018-11-13 09:45  eilinge  阅读(236)  评论(0编辑  收藏  举报

  从这周开始,开始学习以太坊开发--solidity,开始决定往区块链方向发展,毕竟区块链技术应用广泛。一开始接触solidity开发语言不太习惯,毕竟一直在学习python语法,有很多都不能接受。有难度,我喜欢!!

 

成功者往往会选择走人少走的,更艰难的路,只有做别人都很难实现的事,再加上个人加倍付出的努力,成功才更有意义!!!

 

pragma solidity ^0.4.0;

contract Test{
    string str1;
    uint8 _age;
    uint8 _height;
    uint8 _sex;
    address _owner;
    
    //函数名与与合约名一样则表示构造函数,内部赋值变量需要先声明状态变量   
  //Variables inside the construct cannot be called directly outside
function Test(){ //==constructor _age = 10; _height = 140; _sex = 1; _owner = msg.sender;
     //uint public _mouth;ParserError }
function setvalue(uint8 age){ _age = age; } function getvalue() constant returns(uint8){ return _age; } function setStrvalue(string pam){ str1 = pam; } //向字符串内传参,使用双引号("");若使用单引号('),SyntaxError: Unexpected token ' in JSON at position 1 function getStrvalue() constant returns(string){ return str1; } //未执行kill(),返回当前合约地址;执行kill(),error: Failed to decode output: TypeError: Cannot read property 'length' of undefined function getOwner() constant returns(address){ return _owner; } // 析构函数:当msg.sender为owner,删除合约地址 function kill() public{ if (_owner == msg.sender){ selfdestruct(_owner); } } }
强制的数据位置(Forced data location)
    状态变量(State variables)强制为: storage
默认数据位置(Default data location)
    函数参数及返回参数:memory
    复杂类型的局部变量:storage

深入分析
storage 存储结构是在合约创建的时候就确定好了的,它取决于合约所声明状态变量。但是内容可以被(交易)调用改变。
    Solidity 称这个为状态改变,这也是合约级变量称为状态变量的原因。也可以更好的理解为什么状态变量都是storage存储。

memory 只能用于函数内部,memory 声明用来告知EVM在运行时创建一块(固定大小)内存区域给变量使用。
    storage 在区块链中是用key/value的形式存储,而memory则表现为字节数组