• solidity合约面向对象


    1. 属性【状态变量】的访问权限

    public  internal【合约属性默认的权限】  private

       说明:属性默认访问全向为internalinternalprivate类型的属性,外部是访问不到的。

    当属性为public时,会自动生成一个和属性同名并且返回值为当前属性的get方法。

      例如:

    uint public _age;

    会自动生成一个:

    function _age() constant  returns (uint){
        return _age;
    }

      如果自己定义了_age方法,会覆盖原来的方法:

    internal:

    public:

    2.行为【合约的方法】的访问权限

    访问权限有:public internal private

      方法默认为public类型。

     3.继承

    this指针只能访问public类型的成员,只有是public类型,才能通过合约地址访问【合约内部的this,就是当前合约的地址】,在合约内部如果要访问合约的internal和private,直接访问即可,不要使用this。

      继承使用关键字is:

    pragma solidity ^0.4.4;
    
    contract Animal{
      uint _weight;
      uint public _age;
      uint private _name;
      
      function test() constant {
          
      }
    }
    
    contract Dog is Animal{
      
    }

     

      对于public类型的成员,子类直接继承过来;

      对于internal类型的成员,子类在内部可以访问;

      对于private类型的成员,子类不能直接访问;

    多继承:is Animal, Eat

    pragma solidity ^0.4.4;
    
    contract Animal{
      uint _weight;
      uint public _age;
      uint private _name;
      
      function test() constant returns (uint){
          return 100;
      }
    }
    
    contract Eat{
      function eat() constant returns(string){
        return "eat";
      }
    }
    
    contract Dog is Animal,Eat{
        
        function test() constant returns (uint){
          return 200;
      }
        
        function getWeight() constant returns (uint){
            return _weight;
        }
      
    }
    View Code

     4.值类型,引用类型

    值类型有:

    • Boolean
    • Integer
    • Address
    • fixed byte array【定长字节数组】
    • rational and integer literals ,string linterals【有理数和整形】
    • enum【枚举】
    • function【函数】

     引用类型有:

    • 不定长字节数组
    • 字符串
    • 数组
    • 结构体

       函数参数的默认引用类型为memory,即不会修改实参的值。当修改为storage时,可以修改引用类型的变量的值,如果函数使用了storage,那么只能是internal【只能内部调用】.

      对于字符串,只能修改单个字符。bytes(name)[0] = 'L'   【索引不能出界】

    pragma solidity ^0.4.4;
    
    contract Person{
      string public _name;
      function Person(string name){
          _name=name;
      }
      
      function f(){
          modify(_name);
      }
      
      function modify(string storage name) internal {
          bytes(name)[0] = 'A';
      }
      
      function name() constant returns (string){
          return _name;
      }
    }
    View Code
  • 相关阅读:
    存储过程生成POCO
    Asp.net MVC4与Razor呈现图片的扩展
    Html5中新input标签与Asp.net MVC应用程序
    HTML5上传文件显示进度
    JQuery图表插件之Flot
    用Html5与Asp.net MVC上传多个文件
    TSQL列出最后访问的存储过程
    Asp.net MVC 限制一个方法到指定的Submit按钮
    VisualStudio2012轻松把JSON数据转换到POCO的代码
    Apache Tika源码研究(三)
  • 原文地址:https://www.cnblogs.com/zhuxiang1633/p/9461159.html
Copyright © 2020-2023  润新知