22일차 - 다단계 상속
특사-VC / 30일의 견고함
스마트 계약 개발을 배우기 위한 30일간의 Solidity 단계별 가이드.
Solidity Series의
22 중 Day30입니다.오늘은 Solidity에서 다단계 상속에 대해 배웠습니다.
다단계 상속
단일 상속과 매우 유사하지만 차이점은 부모와 자식 간의 관계 수준이 있다는 것입니다. 상위에서 파생된 하위 계약은 여기에서 파생된 계약의 상위 역할도 합니다.
예: 아래 예에서 계약 A는 계약 B로 상속되고 계약 B는 계약 C로 상속되어 다단계 상속을 보여줍니다.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
// Defining parent contract A
contract A {
string internal x;
string a = "Multi";
string b = "Level";
// Defining external function to return concatenated string
function getA() external {
x = string(abi.encodePacked(a, b));
}
}
// Defining child contract B inheriting parent contract A
contract B is A {
string public y;
string c = "Inheritance";
// Defining external function to return concatenated string
function getB() external payable returns (string memory) {
y = string(abi.encodePacked(x, c));
}
}
// Defining child contract C inheriting parent contract A
contract C is B {
function getC() external view returns (string memory) {
return y;
}
}
// Defining calling contract
contract caller {
// Creating object of child C
C cc = new C();
// Defining public function to return final concatenated string
function testInheritance() public returns (string memory) {
cc.getA();
cc.getB();
return cc.getC();
}
}
산출:
testInheritance 함수를 호출하면 출력은 "MultiLevelInheritance"입니다.
Reference
이 문제에 관하여(22일차 - 다단계 상속), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/envoy_/day-22-multi-level-inheritance-1m9e텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)