前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >从零构建以太坊(Ethereum)智能合约到项目实战——学习笔记4

从零构建以太坊(Ethereum)智能合约到项目实战——学习笔记4

作者头像
墨文
发布2020-02-28 14:31:21
4750
发布2020-02-28 14:31:21
举报
文章被收录于专栏:m0w3nm0w3n

P10 、1-Solidity面向对象编程 - 类和对象

面向过程编程

我想听音乐为例子:

1、打开电脑

2、打开音乐播放器

3、搜索一首歌曲

4、播放歌曲

5、暂停

6、下一首

7、音量调节

8、关闭音乐播放器

9、关闭电脑

按照预设的顺序一步一步的去执行,这个过程就是面向过程编程。

面向对象编程

对象:电脑、音乐播放器

电脑-》属性:颜色、尺寸、价格、酷我音乐

电脑-》方法/行为:开机、关机,安装酷我音乐,卸载酷我音乐

酷我音乐-》属性:皮肤,11M,¥41,1000歌

酷我音乐-》方法/行为:启动酷我音乐、关闭酷我音乐、搜素歌曲、下一首歌、上一首歌、暂停、调节音量、播放

面向对象实现上面的程序:

1、【电脑-》开机】

2、【酷我音乐-》启动】

3、【酷我音乐-》搜索】

4、【酷我音乐-》播放】

5、【酷我音乐-》调节音量】

6、【酷我音乐-》暂停】

7、【酷我音乐-》播放】

8、【酷我音乐-》关闭】

9、【电脑-》关机】

类和对象

苹果电脑(类):小明的电脑、谢霆锋的电脑、张柏芝的电脑、汪峰的电脑(对象)

明星(类):章子怡、撒贝宁、刘德华、成龙(对象)

武功秘籍(类):降龙十八掌、九阴真经、辟邪剑法、打狗棒法(对象)

总结:

类:类它是一个抽象的概念,它将一堆相似的东西共同的属性和行为抽象出来,所有抽象出来的属性和行为组成一个类,可以通过类去**实例化**N多个对象。

P11 、2-如何通过Solidity实现一个类(合约)

solidity在线编辑器:http://remix.ethereum.org/#optimize=false&version=soljson-v0.5.1+commit.c8a2cb62.js

编写智能合约可以通过atom来进行编写,也可以通过在线编辑器编写

 1、编写智能合约SmartContract

代码语言:javascript
复制
pragma solidity ^0.4.4;
/*
pragma:版本声明
solidity:开发语言
0.4.4:当前合约的版本,0.4代表主版本,.4代表修复bug的升级版
^:代表向上兼容,0.4.4~0.4.9可以对我们当前的代码进行编译
*/

//contract Person 类比 class Person extends Contract{}
contract Person{
  uint _height;//身高
  uint _age;//年龄
  address _owner;//合约的拥有者

  //方法名和合约名相同时就属于构造函数,在创建对象时,构造函数会自动最先被调用
  function Person(){
    _height = 180;
    _age = 29;
    _owner = msg.sender;
  }

  function owner() constant returns (address){
    return _owner;
  }

  //set方法,可以修改_height属性
  function setHeight(uint height){
    _height = height;
  }

  //get方法,可以查看读取_height属性
  //读取方法不需要消耗gas
  //constant 代表方法只读
  function height() constant returns(uint) {
    return _height;
  }

  function setAge(uint age){
    _age = age;
  }

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

  function kill() constant{
    if(_owner == msg.sender){
      //析构函数
      selfdestruct(_owner);
    }
  }
}

2、在线编辑器中Compile编译SmartContract智能合约

3、运行该智能合约

contract address合约地址:0x692a70d2e424a56d2c6c27aa97d1a86395877b3a

from钱包地址:0xca35b7d915458ef540ade6068dfe2f44e8fa733c,是会被扣除手续费的地址

P12 、3-Solidity 合约中属性和行为的访问权限(一)

1、属性的访问权限

  属性:状态变量

代码语言:javascript
复制
pragma solidity ^0.4.4;

//public > internal > private

contract Person{
  //internal 是合约属性默认的刚问权限
  uint internal _age;
  uint _weight;
  uint private _height;
  uint public _money;

  function _money() constant returns(uint){
    return 120;
  }
}

说明:属性默认类型为‘internal’,‘internal’和‘private’类型的属性都不能被外部访问,只有当属性类型为‘public’类型时,会生成一个和属性名相同并且返回值就是当前属性的get函数(好像现在高版本不能这样用了,编译会报错)。

比如:

代码语言:javascript
复制
uint public _money;

会生成一个

代码语言:javascript
复制
function _money() constant returns (uint){
    return _money;
}

函数

当然,生成的这个get函数会覆盖掉public类型的属性自动生成的get函数。

比如,

代码语言:javascript
复制
pragma solidity ^0.4.4;

//public > internal > private

contract Person{
  //internal 是合约属性默认的刚问权限
  uint internal _age;
  uint _weight;
  uint private _height;
  uint public _money;

  function _money() constant returns(uint){
    return 120;
  }
}

在这个代码里面,_money函数返回值为120,而不是返回0。

P13 、4-Solidity 合约中属性和行为的访问权限(二)

 2、方法/行为访问权限

  方法/行为:合约里面的函数

代码语言:javascript
复制
pragma solidity ^0.4.4;
//public > internal > private
contract Person{
  function age() constant returns (uint){
    return 55;
  }

  function weight() constant returns (uint){
    return 180;
  }

  function height() constant returns (uint){
    return 172;
  }

  function money() constant returns (uint){
    return 32000;
  }
}

合约中的方法默认为‘public’类型

代码语言:javascript
复制
pragma solidity ^0.4.4;
//public > internal > private
contract Person{
  function age() constant returns (uint){
    return 55;
  }

  function weight() constant public returns (uint){
    return 180;
  }

  function height() constant internal returns (uint){
    return 172;
  }

  function money() constant private returns (uint){
    return 32000;
  }
}

合约中的方法默认为‘public’类型才能被访问

P14 、5-Solidity 合约中属性和行为的访问权限(三)

属性和方法自己合约内部的访问权限总结

代码语言:javascript
复制
pragma solidity ^0.4.4;
//public > internal > private
contract Animal{
  //internal 是合约属性默认的刚问权限
  uint _weight;
  uint private _height;
  uint internal _age;
  uint public _money;

    //public
  function test() constant returns (uint){
    this._money();
    this._money;
    //this._height();报错
    return _weight;
  }
  
  function test1() constant public returns (uint){
    return _height;
  }
  
  function test2() constant internal returns (uint){
    return _age;
  }
  
  function test3() constant private returns (uint){
    return _money;
  }
  
  function testInternal() constant returns(uint){
      return this.test();
  }
  
    function testInternal1() constant returns(uint){
      return this.test1();
  }
  
   function testInternal2() constant returns(uint){
      //return this.test2();   会报错
      return test2();
  }
  
   function testInterna3() constant returns(uint){
      return test3();
  }
  
}

1、属性默认权限为‘internal’,只有‘public’类型的属性才可以供外部访问,‘internal’和‘private’类型的属性只能在合约内部使用

2、函数的权限默认为‘public’,‘public’类型的函数可供外部访问,而‘internal’和‘private’类型的函数不能够通过指针(this)进行访问,哪怕是在内部通过this访问都会报错,在合约内部访问,我们可以直接访问函数就可以了。

备注:不管是属性还是方法,只有是‘public’类型时,才可以通过合约地址进行访问,合约内部的this就是当前合约的地址。在合约内部如果要访问‘internal’和‘private’类型的属性或者是函数,直接访问即可,不要试图通过this去访问。

P15 、6-Solidity合约单继承与多继承

solidity合约继承:继承直接使用 is 关键字

代码语言:javascript
复制
pragma solidity ^0.4.4;
//public > internal > private
contract Animal{
  //internal 是合约属性默认的刚问权限
  uint _weight;
  uint private _height;
  uint internal _age;
  uint public _money;

    //public
  function test() constant returns (uint){
    this._money();
    this._money;
    //this._height();报错
    return _weight;
  }
  
  function test1() constant public returns (uint){
    return _height;
  }
  
  function test2() constant internal returns (uint){
    return _age;
  }
  
  function test3() constant private returns (uint){
    return _money;
  }
  
}

contract Dog is Animal{
    
    function testWeight() constant returns(uint){
        return _weight;
    }
    
    //不能被继承
    function testHeight() constant returns(uint){
        return _height;
    }
    
    function testAge() constant returns(uint){
        return _age;
    }
    
    function testMoney() constant returns(uint){
        return _money;
    }
}

子合约继承的属性和方法权限的总结:

  • 子合约只可以继承‘public’类型的函数
  • 子合约可以继承‘public’和‘internal’类型的属性。

 合约多继承:

代码语言:javascript
复制
pragma solidity ^0.4.4;
//public > internal > private
contract Animal{
  //internal 是合约属性默认的刚问权限
  uint _weight;
  uint private _height;
  uint internal _age;
  uint public _money;

    //public
  function test() constant returns (uint){
    this._money();
    this._money;
    //this._height();报错
    return _weight;
  }
  
  function test1() constant public returns (uint){
    return _height;
  }
  
  function test2() constant internal returns (uint){
    return _age;
  }
  
  function test3() constant private returns (uint){
    return _money;
  }
  
}

contract Animal1{
    uint _sex;//1为雄性,0为雌性
    
    function Animal1(){
        _sex=1;
    }
    
    function sex() constant returns (uint){
        return _sex;
    }
}

contract Dog is Animal,Animal1{
    
    
    function testWeight() constant returns(uint){
        return _weight;
    }
    
    function testAge() constant returns(uint){
        return _age;
    }
    
    function testMoney() constant returns(uint){
        return _money;
    }
    
}

P16 、7-Solidity合约函数的重写

收到

代码语言:javascript
复制
pragma solidity ^0.4.4;
//public > internal > private
contract Animal{
  //internal 是合约属性默认的刚问权限
  uint _weight;
  uint private _height;
  uint internal _age;
  uint public _money;

    //public
  function test() constant returns (uint){
    this._money();
    this._money;
    //this._height();报错
    return _weight;
  }
  
  function test1() constant public returns (uint){
    return _height;
  }
  
  function test2() constant internal returns (uint){
    return _age;
  }
  
  function test3() constant private returns (uint){
    return _money;
  }
  
}

contract Animal1{
    uint _sex;//1为雄性,0为雌性
    
    function Animal1(){
        _sex=1;
    }
    
    function sex() constant returns (uint){
        return _sex;
    }
}

contract Dog is Animal,Animal1{
    
    
    function testWeight() constant returns(uint){
        return _weight;
    }
    
    function testAge() constant returns(uint){
        return _age;
    }
    
    function testMoney() constant returns(uint){
        return _money;
    }
    
    function sex() constant returns(uint){
        return 0;
    }
    
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-01-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
区块链
云链聚未来,协同无边界。腾讯云区块链作为中国领先的区块链服务平台和技术提供商,致力于构建技术、数据、价值、产业互联互通的区块链基础设施,引领区块链底层技术及行业应用创新,助力传统产业转型升级,推动实体经济与数字经济深度融合。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档