다른 컨트렉트와 상호작용하기
인터페이스와 컨트렉트 == 차이점은?
인터페이스는 다른 컨트렉트와 상호작용하고자 하는 함수만을 정의하고 다른 함수나 상태변수를 선언하지 않는다.
또한 함수의 몸체 즉 코드블럭을 정의하지 않고 세미콜론으로 함수의 선언을 마친다.
예를들어 학생의 주소를 인자값으로 넣었을때 이름과 번호만을 리턴하는 함수를 담고있는 studentInterface를 생성한다는 가정하에 코드를 만들어보자.
contract studentInterface {
function getStudent(address _studentAddress) external view returns{
string studentName,
uint studentNum,
};
}
그럼 이제 다른 컨트렉트에서 studentContract라는 studentInterface 를 생성해 보겠다.
contract Mycontract {
address studentAddres = 123456789;
studentInterface studentContract = studentInterface(studentAddress);
}
사실 여기에서 왜 주소로 contract를 초기화 한다고 하는건지 이해가 안된당...ㅋㅋ
하여간 이제부터 studentContract로 studentInterface안에 있는 getStudent함수에 접근할 수 있게 되었다.
예 ) studentContract.getStudent(123456789);
getStudent처럼 다수의 값을 반환하는 함수들은 어떻게 처리할까?
예를들어 새로운 함수 newStudent가 있다고 해보자.
이 함수안에서 특정값을 보내서 getStudent의 리턴값을 받고 싶다면 어떻게 해야할까?
1. 일단 리턴값을 받을 변수를 미리 선언 해 두어야 한다.
- 여기에선 getName과 getNumber라는 변수를 사용하겠다.
function newStudent() {
string getName;
string getNumber;
}
2. 구조할당문처럼 각 변수에 함수의 리턴값들을 할당한다.
function newStudent() {
string getName;
string getNumber;
(getName, getNumber) = studentContract.getStudent(123456789);
}
3. 만약 한개의 값만 받고 싶다면 받지 않을 값의 자리는 ,로 비워두면 된다.
function newStudent() {
string getName;
(,getName) = studentContract.getStudent(123456789);
}
=================================================================
https://share.cryptozombies.io/ko/lesson/2/share/new?id=Y3p8MTM2NTk2
제 크립토좀비 OGURL이/가 방금 크립토키티 한 마리를 잡아 먹고 레벨 업을 했네요!
제가 방금 전에 크립토좀비 레슨 2를 마쳤어요. 되살아난 크립토키티를 제 좀비 군대에 추가했어요. 와서 확인해 보세요!
share.cryptozombies.io
'Solidity' 카테고리의 다른 글
[solidity] This란? (0) | 2022.02.23 |
---|---|
[크립토 좀비] 솔리디티 openzeppelin (0) | 2021.11.30 |
솔리디티 크립토좀비 푸는 중 (0) | 2021.10.09 |
[ 솔리디티 이더리움 , 크립토좀비 ] requirea문 (0) | 2021.10.08 |
[ 솔리디티 이더리움 , 크립토좀비 ] mapping (0) | 2021.10.08 |