导入
继承
了解 Solidity 中的继承。
一个基本的继承示例
1// A 是 B 的派生合约 2contract A is B { 3 // 实现代码 4}
在 Solidity 中继承函数
1// 父合约 A 2contract A { 3 function run(int x, int y) public virtual { 4 // 实现代码 5 } 6} 7 8// 子合约 B 继承 A 的函数 9contract B is A { 10 function run(int a, int b) public override { 11 // 函数实现 12 } 13}
...
单层继承
1// SPDX-License-Identifier: MIT 2pragma solidity ^0.8.17; 3 4// 表示通用车辆的合约 5contract Vehicle { 6 uint internal speed; // 用于存储速度的内部状态变量 7 8 // 设置车辆速度的函数 9 function setSpeed(uint _speed) external { 10 speed = _speed; 11 } 12 13 // 获取车辆当前速度的函数 14 function getSpeed() external view returns(uint) { 15 return speed; 16 } 17} 18 19// 表示特定类型车辆:汽车的合约 20contract Car is Vehicle { 21 // 增加汽车速度的函数 22 function accelerate() external { 23 speed += 10; // 速度增加 10 单位 24 } 25} 26 27// 表示特定类型车辆:自行车的合约 28contract Bike is Vehicle { 29 // 增加自行车速度的函数 30 function pedal() external { 31 speed += 5; // 速度增加 5 单位 32 } 33} 34 35// 用于与车辆交互的合约 36contract VehicleManager { 37 Car car = new Car(); // 创建一个 Car 对象 38 Bike bike = new Bike(); // 创建一个 Bike 对象 39 40 // 测试车辆功能的函数 41 function testVehicles() public returns (uint, uint) { 42 car.setSpeed(50); // 将汽车速度设为 50 43 bike.setSpeed(30); // 将自行车速度设为 30 44 45 car.accelerate(); // 提升汽车速度 46 bike.pedal(); // 提升自行车速度 47 48 // 返回汽车和自行车的速度 49 return (car.getSpeed(), bike.getSpeed()); 50 } 51}
...
多层继承
1// SPDX-License-Identifier: MIT 2pragma solidity ^0.8.17; 3 4// 基础合约,表示员工 5contract Employee { 6 uint internal salary; 7 string internal role; 8 9 // 设置薪资的函数 10 function setSalary(uint _salary) external { 11 salary = _salary; 12 } 13 14 // 设置职位的函数 15 function setRole(string calldata _role) external { 16 role = _role; 17 } 18 19 // 获取薪资的函数 20 function getSalary() external view returns(uint) { 21 return salary; 22 } 23 24 // 获取职位的函数 25 function getRole() external view returns(string memory) { 26 return role; 27 } 28} 29 30// 表示经理的合约,继承自 Employee 31contract Manager is Employee { 32 uint internal bonus; 33 34 // 设置奖金的函数 35 function setBonus(uint _bonus) external { 36 bonus = _bonus; 37 } 38 39 // 获取奖金的函数 40 function getBonus() external view returns(uint) { 41 return bonus; 42 } 43} 44 45// 表示高管的合约,继承自 Manager 46contract Executive is Manager { 47 uint internal stockOptions; 48 49 // 设置股票期权的函数 50 function setStockOptions(uint _options) external { 51 stockOptions = _options; 52 } 53 54 // 获取股票期权的函数 55 function getStockOptions() external view returns(uint) { 56 return stockOptions; 57 } 58} 59 60// 用于与员工、经理和高管合约交互的合约 61contract Organization { 62 Executive executive = new Executive(); 63 64 // 管理组织信息的函数 65 function manageOrganization() public returns (uint, string memory, uint, uint) { 66 executive.setSalary(100000); // 设置薪资 67 executive.setRole("CEO"); // 设置职位 68 executive.setBonus(50000); // 设置奖金 69 executive.setStockOptions(1000); // 设置股票期权 70 71 // 返回高管的薪资、职位、奖金和股票期权 72 return ( 73 executive.getSalary(), 74 executive.getRole(), 75 executive.getBonus(), 76 executive.getStockOptions() 77 ); 78 } 79}
...
层次继承
1// SPDX-License-Identifier: MIT 2pragma solidity ^0.8.17; 3 4// 基础合约,表示通用课程 5contract Course { 6 string public name; 7 uint256 public duration; // 单位:小时 8 uint256 public price; // 单位:wei 9 10 constructor(string memory _name, uint256 _duration, uint256 _price) { 11 name = _name; 12 duration = _duration; 13 price = _price; 14 } 15 16 // 获取课程价格的函数 17 function getPrice() external view virtual returns (uint256) { 18 return price; 19 } 20} 21 22// 派生合约,表示编程类课程 23contract ProgrammingCourse is Course { 24 string public language; 25 26 constructor(string memory _name, uint256 _duration, uint256 _price, string memory _language) 27 Course(_name, _duration, _price) 28 { 29 language = _language; 30 } 31 32 // 重写函数以提供编程课程的特定功能 33 function getPrice() external view override returns (uint256) { 34 // 编程课程可根据语言附加额外价格 35 if (keccak256(abi.encodePacked(language)) == keccak256(abi.encodePacked("Solidity"))) { 36 return price * 2; // Solidity 课程更贵 37 } else { 38 return price; 39 } 40 } 41} 42 43// 派生合约,表示数学类课程 44contract MathematicsCourse is Course { 45 uint256 public level; 46 47 constructor(string memory _name, uint256 _duration, uint256 _price, uint256 _level) 48 Course(_name, _duration, _price) 49 { 50 level = _level; 51 } 52 53 // 重写函数以提供数学课程的特定功能 54 function getPrice() external view override returns (uint256) { 55 // 数学课程的价格根据级别增加 56 return price * level; 57 } 58} 59 60// 用于在学习平台中管理课程的合约 61contract CourseManager { 62 Course[] public courses; 63 64 // 将新课程添加到列表中的函数 65 function addCourse(Course _course) external { 66 courses.push(_course); 67 } 68 69 // 获取所有课程总价格的函数 70 function getTotalPrice() external view returns (uint256) { 71 uint256 totalPrice; 72 for (uint256 i = 0; i < courses.length; i++) { 73 totalPrice += courses[i].getPrice(); 74 } 75 return totalPrice; 76 } 77}