solidity 支持多继承,通过关键字 is 实现,继承的合约可以直接访问父合约的public,internal权限的变量或函数

1.继承与构造函数
  • 有两种方法初始化父类合约,如contract A 、contract B
  • 在继承的时候,父构造函数总是按照继承的顺序调用,跟子合约中父类的构造函数的顺序无关,如contract B、contract C
pragma solidity ^0.8.0;contract X {string x;constructor(string memory _x){x = _x;}
}contract Y {string y;constructor(string memory _y){y = _y;}
}contract A is X("hello x"),Y("hello y"){string a;constructor(string memory _a){a = _a;}
}// 构造函数调用顺序
// X ->  Y - >B
contract B is X,Y{string a;constructor(string memory _a,string memory _x,string memory _y)X(_x) Y(_y){a = _a;}
}// 构造函数调用顺序
// X ->  Y - >C
contract C is X,Y{string a;constructor(string memory _a,string memory _x,string memory _y) Y(_y) X(_x){a = _a;}
}
2.继承与虚函数

父合约标记为 virtual 函数可以在子合约里进行重写,重写的函数需要使用关键字 override 修饰。

pragma solidity ^0.8.0;contract X{function f() public virtual pure returns(string memory){return "xf";}
}contract Y is X{
function f() public virtual override pure returns(string memory){return "yf";}
}

如果多继承中,父合约有相同的virtual函数,则override 关键字后必须指定所有父合约名

pragma solidity ^0.8.0;contract X{function f() public virtual pure returns(string memory){return "xf";}
}contract Y{function f() public virtual pure returns(string memory){return "yf";}
}contract Z is X,Y{function f() public pure  override(X,Y)  returns(string memory){return "zf";}}
3.继承与父合约调用
  • 可以使用super关键字或父合同名称调用父合同的函数
  • 使用super关键字,最近的父类合约( immediate parent contracts)也会被调用
pragma solidity ^0.8.10;/* Inheritance treeA/  \
B   C\ /D
*/contract A {event Log(string message);function foo() public virtual {emit Log("A.foo called");}function bar() public virtual {emit Log("A.bar called");}
}contract B is A {function foo() public virtual override {emit Log("B.foo called");A.foo();}function bar() public virtual override {emit Log("B.bar called");super.bar();}
}contract C is A {function foo() public virtual override {emit Log("C.foo called");A.foo();}function bar() public virtual override {emit Log("C.bar called");super.bar();}
}contract D is B, C {//C.foo called//A.foo calledfunction foo() public override(B, C) {super.foo();}//C.bar called//B.bar called//A.bar calledfunction bar() public override(B, C) {super.bar();}
}
4.继承规则
  • 多重继承时,当一个函数在多个父函数定义,子合约调用该函数是遵循从右到左的原则,如contract A和contract B
  • 多重继承时需要按照从“最接近的基类”(most base-like)到“最远的继承”(most derived)的顺序来指定所有的基类,如contract C
pragma solidity ^0.8.0;contract X {function foo()public virtual pure returns(string memory){return "X";}
}contract Y is X{function foo()public virtual override pure returns(string memory){return "Y";}
}contract Z is X{function foo()public virtual override pure returns(string memory){return "Z";}
}contract A is Y,Z{function foo()public virtual override(Y,Z) pure returns(string memory){return super.foo(); //return Z}
}contract B is Z,Y{function foo()public virtual override(Y,Z) pure returns(string memory){return super.foo(); //return Y}
}/*X/ \Y   Z/ \ /
C  A,B
contract C is Y,X{} //这种继承顺序会报:Linearization of inheritance graph impossible
*/
contract C is X,Y{function foo()public virtual override(X,Y) pure returns(string memory){return super.foo(); //return Y}
}

solidity:6.继承相关推荐

  1. solidity 合约继承

    继承通过关键字 is 来实现,例如: pragma solidity ^0.8.0; contract Person{string name;uint age; } contract Man is P ...

  2. Solidity 简易教程0x001

    Solidity是以太坊的主要编程语言,它是一种静态类型的 JavaScript-esque 语言,是面向合约的.为实现智能合约而创建的高级编程语言,设计的目的是能在以太坊虚拟机(EVM)上运行. 本 ...

  3. Solidity基础入门讲解

    Solidity的语言类型: 静态类型的语言:编译前变量类型需要先确定 变量可以分为: 值类型:赋值或者传参时总是进行值拷贝 引用类型:传递的是地址 这里先给出一个总览图: 整型: 和其他语言类型,可 ...

  4. Solidity – 代码注释

    章节 Solidity – 介绍 Solidity – 环境搭建 Solidity – 基础语法 Solidity – 第一个程序 Solidity – 代码注释 Solidity – 数据类型 So ...

  5. Solidity - 介绍

    章节 Solidity – 介绍 Solidity – 环境搭建 Solidity – 基础语法 Solidity – 第一个程序 Solidity – 代码注释 Solidity – 数据类型 So ...

  6. 基于centos7.0+mono+jexus配置https(阿里云服务器最全配置jexus https教程)

    终于经过一下午的折腾,https可以正常访问: 效果如下: 怎么在jexus上配置https.png 我的环境为: 购买的阿里云centos 7.0 服务器,后台采用的是c#开发,因此部署的是mono ...

  7. 有趣的智能合约蜜罐分析(上)

    智能合约蜜罐概述 研究安全的读者应该都清楚,蜜罐本质上是一种对攻击方进行欺骗的技术,通过布置一些作为诱饵的主机.网络服务或者信息,诱使攻击方对它们实施攻击.蜜罐设计的初衷就是让黑客来入侵系统,并借此收 ...

  8. Solidity基础教程:合约的继承与方法的重写

    Solidity基础教程:合约的继承与方法的重写 合约继承 合约继承使用is关键字 contract ERC721 is Context, ERC165, IERC721, IERC721Metada ...

  9. solidity数据类型(四)storage memory calldata modifier前置条件 继承 接口合约 导入库 using...for solc编译

    1 数据存储位置 数据测存储类型有storage 和 memory 函数的传入参数和返回参数 都是 memory类型(external函数的入参为calldata类型,只可读,不可重写) 函数局部变量 ...

  10. Solidity学习::(6)智能合约继承

    例子1:多继承下有重名函数,执行哪个函数的问题 contract owned {funtion owned() { owner =msg.sender;} //构造函数address owner; } ...

最新文章

  1. mysql mha官网下载_mysql MHA 及多主复制
  2. CSS-3 Animation 的使用
  3. mqtt连接失败_Netty实战:如何让单机下Netty支持百万长连接?
  4. C语言编程课后训练,C语言编程课后训练20道题.doc
  5. centos6.10中部署percona-mysql双实例的方法
  6. mysql 存储过程求和_MySQL - 存储过程和函数
  7. clion中链接openssl库
  8. C++ 11使用thread类多线程编程
  9. ZOJ 1076 Gene Assembly
  10. 创业篇——小老板的日常管理
  11. 全网首发:GB18030中,蒙文的错误
  12. python代码混淆工具_Intensio-Obfuscator:一款专业Python代码混淆处理工具
  13. 5.3 上兴远程控制
  14. 已解决报错UnboundLocalError: local variable ‘title‘ referenced before assignment
  15. DSPE-PEG7-Mal新研博美 小分子PEG的循环节可以做到1-36个
  16. matlab状态空间模型构建函数ss
  17. 转载一个特征提取的方法——AE
  18. OpenCV中waitKey()函数的深度解析
  19. 第二十三章 类关键字 - Language
  20. 地平线黎明时分dlc评测_我从第一个玩法中学到的地平线零黎明的提示

热门文章

  1. 红客是什么呢?零基础如何成为红客!
  2. IT学不好没什么,大不了躺平
  3. gta5nat严格怎么办_如何解决在游玩 GTA 在线模式时出现的 NAT 类型为“严格”(Strict)的错误...
  4. Clickhouse 取整函数
  5. a++和++a;a+=和a=a+1的区别
  6. CodeForces - 747D Winter Is Coming(xjb乱搞)
  7. 平面设计软件都有哪些?推荐这7款
  8. 互联网老辛2022年2月社群分享精华
  9. 企业邮箱管理系统,如何使用群组发送邮件?
  10. 全方位揭秘!大数据从0到1的完美落地之HDFS的工作机制