zoukankan      html  css  js  c++  java
  • Solidity智能合约面向对象编程(一、类的创建)

    Solidity编写智能合约

     1 pragma solidity ^0.4.4;//版本声明 ^代表向上兼容 pragma代表版本声明  solidity 代表开发语言
     2 //定义类
     3 contract Person {
     4     //定义属性,属性名规范前面加_
     5     uint _height;
     6     uint _age;
     7     address _owner;//代表合约的拥有者
     8     //方法名与合约名相同时属于构造函数
     9     function Person(){
    10         _height = 180;
    11         _age = 29;
    12         _owner = msg.sender;
    13     }
    14     //set方法,去掉属性前面下划线,首字母大写,采用驼峰式
    15     function setHeight(uint height){
    16         _height = height;
    17     }
    18     //get方法,方法名不需要写get  constant代表方法只读,returns(uint)代表方法返回值类型
    19     function height() constant returns(uint){
    20         return _height;
    21     }
    22     
    23     function setAge(uint age){
    24         _age = age;
    25     }
    26     function age() constant returns(uint){
    27         return _age;
    28     }
    29     
    30     function owner() constant returns(address){
    31         return _owner;
    32     }
    33     
    34     //析构函数
    35     function kill() {
    36         if(_owner == msg.sender){
    37             selfdestruct(_owner);
    38         }
    39     }
    40     
    41     //0x692a70d2e424a56d2c6c27aa97d1a86395877b3a 当前合约地址
    42 }

     部署合约时,因为要往区块链写入数据,需要矿工进行验证,所以需要花费一些gas奖励给矿工,还有当我们每次调用increment方法时,也属于写入数据,同样需要花费gas,但是调用getCount方法时只是从区块链读取数据,无需验证,读取数据无须花费gas。

  • 相关阅读:
    mysql创建用户后无法访问数据库的问题
    mysql索引
    hadoop安装
    MySQL工作原理
    MySQL数据库优化的八种方式
    FLOAT 和 DOUBLE区别
    在C++中定义常量
    C++变量类型
    FTP主动模式、被动模式(转)
    FtpClient ReplyCode 意义及 FtpClientHelper辅助类
  • 原文地址:https://www.cnblogs.com/cat-fish6/p/10448623.html
Copyright © 2011-2022 走看看