solidity 学习

pragma solidity ^0.4.0;
contract Person {

    //属性的默认权限是internal 方法的默认权限是public
    //只有public 权限的属性可以供外部访问。其他需要get set访问
    //通过this(指针)只能访问public的方法。其他权限的方法可以通过方法名()直接调用
    //只有public权限的属性和方法才能通过合约地址去访问。

    //继承
    //只有private 权限的属性不能被继承
    //只有public 权限的方法才能被继承

    // storage 指针传递 memory 值传递

    function age() constant returns (uint) {
        return 26;
    }
    function weight() constant public returns (uint) {
        return 180;
    }
    function height() constant internal returns (uint) {
        return 172;
    }
    function money() constant private  returns (uint) {
        return 32000;
    }
}

 

pragma solidity ^0.4.0;

contract Person {
    uint _age;
    uint _height;
    address _owner;
    //属性==>_age,set方法==setAge get方法==》_age()
    function Person(){
        _age = 18;
        _height = 180;
        _owner = msg.sender;
    }

    function owner() constant returns (address) {
        return _owner;
    }
    function setAge(uint age){
        _age = age;
    }
    //constant 代表只读并且有返回值。 创建实例的时候就会调用带有constant的方法,只要往区块链写入信息都需要付gas
    //同一个钱包地址创建同一个合约不会重新写入区块链,只会返回以前的部署的合约。
    //public inernal(默认权限) private
    function _age() constant returns(uint){
        return _age;
    }

}

 

pragma solidity ^0.4.0;

contract Person{
    uint _age;
    uint private _height;
    uint public _sex;
    //public 属性会自动生成一个get方法 如果手动重写方法可以覆盖掉自动生成的方法。
    //private  internal

    function _sex() constant returns (unit) {
        return _sex;
    }

}

 

pragma solidity ^0.4.0;
contract C {
    bytes9 public g = 0x6254454541545845;
    string public name = "yujunlong";
    bytes public b = new bytes(1);
    bytes1 public bs1;
    bytes32 public bs32;

    function getByteLength() constant public returns (uint){
        return g.length;
    }

    //string==>bytes
    function nameBytes() constant public returns (bytes) {
        return bytes(name);
    }

    function nameLength() constant public returns (uint){
        return bytes(name).length;
    }

    function setNameFBF(bytes1 z) public{

        bytes(name)[0] = z;
    }

    function setbLength(uint len){
        b.length = len;
    }

    //b ==0x64
    function setbData(byte b1, uint index){
        b[index] = b1;
    }

    function pushByte(byte b1){
        b.push(b1);
    }

    //后面补0
    function b1Tb32(){
        bytes9(bs1);
    }
    //截取后面的
    function b32Tb1(){
        bytes1(bs32);
    }

    function bytes32Content(bytes32 x) constant returns (string){
        bytes memory  bytesString = new bytes(32);
        uint charCount = 0;
        for(uint j = 0;j<32;j++){
            byte char = byte(bytes32(uint(x)*2**(8*j)));
             if(char!=0){
                 bytesString[charCount] = char;
                 charCount++;
             }
        }
        bytes memory  bytesString2 = new bytes(charCount);
        for(uint j = 0;j<charCount;j++){
            bytesString2[j] = bytesString[j];
        }
        return string(bytesString2);
    }
}

 

pragma solidity ^0.4.0;

contract Person {
    string _name;

    function Person(string name){
        _name = name;
    }
    function setName(string storage name) internal{
        bytes(name)[0] = "Y";
    }
    function modifyName(){
        setName(_name);
    }
    function name() constant returns (string) {
        return _name;
    }



}

 

pragma solidity ^0.4.0;


contract Person {
    //对象的属性默认是storage 都是要写到区块链中的
    //任何修改状态变量的方法都不能使用constant这个关键字
    //属性的任何赋值都是需要写入到区块链的。
    uint _u;
    bool _b;
    string _s;
    address _a;
    int _i;
    byte b;
    bytes1 b1;
    //长度不可变,内容不可变
    bytes2 b2;
    //固定长度的数组,内容可变,长度不可变
    byte[2] b22 = [byte(0x6c),0x79,0x87];
    bytes32 b32;

    bytes bs;
    //可变
    uint [] T = new uint[](5);
    uint [] TT = [uint(1),3,4];
    //固定不可变
    uint [3] TTT = [uint(1),3,4];



    function Person() {
        _u = 10;
        _b = true;
        _s = "yjl";
        _a = 0x07d62de1141cb58c35e76ab0e4d58903d8812b59;
        _i = -10;

    }


    //
    function f() public {
        uint[3] memory x = [uint(1),3,4];
    }

    //int = int256 有符号 uint = unit256无符号
    //int8-int16-int24...-int256
    //unit8-unit16-unit24...-unit256
    //11111111-01111111   0-01111111
    //_a**_b _a的_b次方
    //-a<<_b==>-a*(2**_b)
    //-a>>_b==>-a/(2**_b)
    //address 40位的4位等于160位==unit160

    //address(unit160) unit160(address)

    //payable 转账关键字 有转账api的时候要在方法后面加上这个关键字。

    //bytes1 = byte  1个字节 8位00000000 a = 65

    //bytes ===string

    //delete 关键字清空bytes 数据
    //固定长度的字节bytes1~bytes32的length的方法返回的是该字节固定的长度,而不是根据内容的变化而变化。

    //0xff==11111111==255
    // 数字默认uint8 需要强转为uint==>[uint(1),3,4]

    //方法中创建的对象默认memory类型。状态属性默认storage

    //memory 是放在内存中,没有写到区块链中的。storage是写到区块链中的。
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值