// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/*
结构体
结构体具有多重属性,结构体可以在一个合约之外声明并导入到另一个合约
结构体语法
struct <struct_name>{
<data type> variable_1;
<data type> variable_2;
<data type> variable_3;
<data type> variable_4;
...
<data type> variable_n;
}
struct <struct_name> a;
例如:
struct A{
uint v1;
uint v2;
}
struct A a;
a.v1;
a.v2;
*/
//结构体的定义,结构变量的声明,基本操作
contract Struct{
//
struct Student {
string name;
uint score;
}
Student[] public students;
function add(string memory _name,uint _score) public{
//1-类似于函数的调用
//students.push(Student(_name,_score));
//2-先初始化一个空结构在赋值
Student memory student;
student.name=_name;
student.score = _score;
students.push(student);
}
//
function get(uint idx) public view returns(Student memory){
Student memory student = students[idx];
return student;
}
function getDetails(uint idx) public view returns(string memory,uint){
Student memory student = students[idx];
return(student.name,student.score);
}
function set(uint idx,uint _score) public{
students[idx].score = _score;
}
}
solidity 结构体01
最新推荐文章于 2023-05-25 15:43:01 发布