-
상속 & 이벤트Block-Chain/Solidity 2023. 1. 5. 13:26
Solidity의 상속과 이벤트에 대해 정리하였습니다.
< 상속의 정의 >
A컨트랙의 변수 및 함수를 B컨트랙이 접근 및 사용하기 위해 상속이 사용됩니다.
- is 키워드를 이용해 상속
contract 컨트랙트1 is 컨트랙트2 { }
Ex) contract son_contract_name is father_contract_name {} 양식를 이용해 상속
contract Father { string public familyName = "Kim"; string public givenName = "Jung"; uint256 public money = 100; constructor(string memory _givenName) { givenName = _givenName; } function getFamilyName() view public returns(string memory) { return familyName; } function getGivenName() view public returns(string memory) { return givenName; } function getMoney() view public returns(uint256) { return money; } } contract Son is Father("James") { } // 해당 방법도 가능 contract Son is Father { constructor() Father("James") { } }
< Overriding >
Overriding이란 변수나 함수를 상속받을 때 해당 값을 덮어씌워 자식만의 특별한 값으로 만드는 것으로 말합니다.
- 덮어씌울 변수(함수) : 부모 컨트랙트에 vitrual, 자식 컨트랙트에 override 키워드를 작성하여야 함.
Ex)
contract Father { uint256 money = 100; function getMoney() view public virtual returns(uint256) { return money; } } contract Son is Father { uint256 public earning = 0; function work() public { earning += 100; } function getMoney() view public override returns(uint256) { return money + earning; } }
< 두 개 이상 상속하기 >
Ex)
contract Father { uint256 public fatherMoney = 100; function getFatherName() public pure virtual returns(string memory) { return "KimJung"; } function getMoney() public view returns(uint256) { return fatherMoney; } } contract Mother { uint256 public motherMoney = 100; function getFatherName() public pure returns(string memory) { return "Leesol"; } function getMoney() public view virtual returns(uint256) { return motherMoney; } } contract Son is Father, Mother { function getMoney() public view override(Father, Mother) returns(uint256) { return fatherMoney + motherMoney; } }
< event 정의 >
event라는 것은 블록체인 네트워크의 블록에 특정값을 기록하는 것을 말합니다.
이벤트 예시 : 송금하기라는 함수가 있다고 가정했을 때, 송금하기 버튼을 누르면 누른 사람의 계좌와 금액이 이벤트로 출력되어 블록체인 네트워크 안에 기록이 됨.- event 정의 방법
event 이벤트이름(타입 이름);
- event 호출 방법
emit 이벤트이름(타입 이름);
Ex)
contract Bank { event info(string name, uint256 money); function sendMoney() public { emit info("KimDaeJin", 1000); } }
< event 정의 >
블록들에 출력된(기록된) 이벤트들중 필터링하여 원하는 이벤트만 가지고 오기 위해 indexed를 사용합니다.
- event 정의 시 indexed 키워드 사용
event 이벤트이름(타입 indexed 이름);
Ex)
contract Lec14 { event numberTracker(uint256 num, string str); event numberTracker2(uint256 indexed num, string str); uint256 num =0; function PushEvent(string memory _str) public { emit numberTracker(num,_str); emit numberTracker2(num,_str); num ++; } }
< super >
부모 컨트랙트의 이벤트를 그대로 가져오기 위해 super 키워드를 사용합니다.
Ex) super의 미사용/사용 : super.who()는 부모 컨트랙트의 who() 함수 내용을 가져옴.
contract Father { event FatherName(string name); function who() public virtual{ emit FatherName("KimDaeho"); } } contract Son is Father{ event sonName(string name); function who() public override{ emit FatherName("KimDaeho"); emit sonName("KimJin"); } }
contract Father { event FatherName(string name); function who() public virtual{ emit FatherName("KimDaeho"); } } contract Son is Father{ event sonName(string name); function who() public override{ super.who(); emit sonName("KimJin"); } }
< 상속의 순서 >
같은 함수(변수)를 상속받을 때, 최근에 상속받은 컨트랙트의 함수(변수)를 상속받습니다.
Ex) super.who() 는 엄마의 who() 함수내용을 상속받습니다.
contract Father { event FatherName(string name); function who() public virtual { emit FatherName("James") } } contract Mother { event MotherName(string name); function who() public virtual { emit MotherName("Leesol") } } contract Son is Father, Mother { functin who() public override(Father, Mother) { super.who(); } }
'Block-Chain > Solidity' 카테고리의 다른 글
조건문 & 반복문 (0) 2023.02.15 Mapping & Array & struct(구조체) (0) 2023.01.16 Instance (0) 2022.12.28 Function (0) 2022.12.26 Solidity란 & 기본 개념 (0) 2022.12.23