2020.코딩일지
[solidity] instance(분신)-constructor/상속/overriding (feat.솔리디티깨부수기) 본문
Block Chain
[solidity] instance(분신)-constructor/상속/overriding (feat.솔리디티깨부수기)
개발하는라푼젤 2022. 11. 28. 23:13728x90
1. instance(분신만들기)-constructor생성자
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract A {
string public name;
uint256 public age;
constructor(string memory _name, uint256 _age) {
name = _name;
age = _age;
}
function change(string memory _name, uint256 _age) public {
name = _name;
age = _age;
}
}
contract B{
A instance = new A("Alice", 20);
function change(string memory _name, uint256 _age) public {
instance.change(_name, _age);
}
function get() public view returns(string memory, uint256) {
return (instance.name(), instance.age());
}
}
2. 상속
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Father {
string public familyName = "kim";
string public givenName = "go";
uint256 public money = 100;
constructor(string memory _givenName) public {
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") {
}
3. overriding
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Father {
string public familyName = "kim";
string public givenName = "go";
uint256 public money = 100;
constructor(string memory _givenName) public {
givenName = _givenName;
}
function getFamilyName() view public returns(string memory) {
return familyName;
}
function getGivenName() view public returns(string memory) {
return givenName;
}
function getMoney() view public virtual returns(uint256) { //⭐️가져다 쓸 함수에 virtual써주기
return money;
}
}
contract Son is Father("James") {
// constructor() Father("James"){
// }
uint256 public earning = 0;
function work() public {
earning += 100;
}
function getMoney() view override public returns(uint256){ //⭐️override 쓰고 사용하기
return money + earning;
}
}
4. 상속 2개받기
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Father {
uint256 public fatherMoney = 100;
function getFatherName() public pure returns(string memory) {
return "fafa";
}
function getMoney() public view virtual returns(uint256) {
return fatherMoney;
}
}
contract Mother {
uint256 public motherMoney = 500;
function getMotherName() public pure returns(string memory) {
return "mama";
}
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;
}
}
'Block Chain' 카테고리의 다른 글
[solidity] mapping/array (feat.솔리디티깨부수기) (11) | 2022.11.29 |
---|---|
[solidity] event/indexed/super (feat.솔리디티깨부수기) (0) | 2022.11.29 |
[solidity] 저장영역4가지 (feat.솔리디티깨부수기) (0) | 2022.11.28 |
[solidity] 가시성지정자(public,external,private,internal)/view pure (feat.솔리디티깨부수기) (0) | 2022.11.28 |
[solidity] DataType/function/ (feat.솔리디티깨부수기) (0) | 2022.11.28 |
Comments