玩以太坊链上项目的必备技能(OOP-抽象合约-Solidity之旅十)

本文探讨了Solidity中的抽象合约概念,如何在继承关系中处理未实现的抽象函数,以及如何通过virtual和override关键字确保正确编译。重点介绍了在0.8.x及以上版本中,如何定义和使用抽象合约来提高代码可扩展性和文档性。
摘要由CSDN通过智能技术生成

抽象合约(abstract contract)

前文在讲合约继承基类构造函数的参数时,有提到抽象合约,也就是说,如果派生合约未能给其继承的基合约指定构造函数参数时,那么,该派生合约必须声明为抽象合约(abstract contract)

我们知道Java抽象类的定义,其一抽象类不能实例化,其二是抽象类中可以拥有 抽象方法(是一种没有方法体的、只有方法签名的方法。)

而在 Solidity 中的抽象合约Java抽象类有异曲同工之妙。即假使合约中至少有一个函数没有实现(没有方法体,只有方法签名的方法),那么便将该合约定义为抽象合约(abstract contract)。当然咯,前文说到继承提到的,派生合约未能给其基合约的构造函数传递指定参数,这时,该合约便只能声明为抽象的。

在 Solidity 0.8.x版本以上,抽象合约抽象函数需加上virtual修饰,而对于的派生合约中的函数实现也得加上override修饰,否则编译过不了。

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;

//base contract
abstract contract Animal  {
  function eat() virtual public ;
}

contract Bird is Animal {
   function eat() override public {

   }
}

在这里插入图片描述

假使派生合约未能给定所有基类的制定参数(基类构造函数的参数),那该合约也必须声明为抽象的。

在这里插入图片描述

解决上图所出现的问题,有两种方式,要么派生合约 contract Snake 给定所有基类构造函数的制定参数;要么将派生合约 Snake声明为抽象(abstract)的。

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;

//base contract
contract Animal  {
  string public name;
   constructor(string memory _name){
      name = _name;
   }
}
//爬行动物是动物
contract Reptile {
   string public Rname;
   constructor(string memory _name){
      Rname = _name;
   }
}

abstract contract Snake is Reptile,Animal {
   //这是一只眼镜蛇 多个基类使用空格隔开
   constructor()  Animal("cobra"){}
}



在这里插入图片描述

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;

//base contract
contract Animal  {
  string public name;
   constructor(string memory _name){
      name = _name;
   }
}
//爬行动物是动物
contract Reptile {
   string public Rname;
   constructor(string memory _name){
      Rname = _name;
   }
}

contract Snake is Reptile,Animal {
   //这是一只眼镜蛇 多个基类使用空格隔开
   constructor() Reptile("diba") Animal("cobra"){}
}


// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;

//base contract
contract Animal  {
  string public name;
   constructor(string memory _name){
      name = _name;
   }
}
//爬行动物是动物
contract Reptile {
   string public Rname;
   constructor(string memory _name){
      Rname = _name;
   }
}

contract Snake is Reptile,Animal {
   //这是一只眼镜蛇 多个基类使用空格隔开
   constructor() Reptile("diba") Animal("cobra"){}
}



在这里插入图片描述

派生合约继承自抽象合约,而并没有去实现抽象合约中的抽象函数,那么,该合约依然需要标记为抽象(abstract)的。

抽象合约将合约的定义与其实现脱钩,从而提供了更好的可扩展性和自文档性,并消除了代码重复。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

甄齐才

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值