资讯动态

solidity学习

发布时间:2026/10/25 2:03:07 来源:尧图企业网站定制
变量// SPDX-License-Identifier: MIT pragma solidity ^0.8.20; contract HelloWorld{ bool boolVar true; bool boolVar2 false; uint256 uintVAR 256; int256 intVAR -256; bytes32 bytes8Var Hellffffffffffffffffffffffffofff; string stringVar Hello Worldeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee; address addVar 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4; }函数public private Internal External存储模式storage : 永久形存储(合约中的变量默认为storage)暂时形存储memory可以修改calldata不可修改函数、结构体、数组、映射// SPDX-License-Identifier: MIT pragma solidity ^0.8.20; // struct: 结构体 // array: 数组 // mapping: 映射 contract HelloWorld{ string stringVar unicode去你妈的深圳; // 中文需要前面加 unicode Solidity 默认只支持 ASCII 字符 struct Info{ string phrase; uint id; address addr; } Info[] infos; mapping (uint Info) infoMapping; function sayHello(uint _id) public view returns (string memory ){ if(infoMapping[_id].addr address(0x0)){ return addinfo(stringVar); }else { return addinfo(infoMapping[_id].phrase); } // for (uint i 0; iinfos.length; i){ // if(infos[i].id _id){ // return addinfo(infos[i].phrase); // } // } // return addinfo(stringVar); } function setHelloWorld(string memory newString, uint _id) public { // uint 为基础数据类型 不用加memory // stringVar newString; Info memory info Info(newString,_id,msg.sender); // infos.push(info); infoMapping[_id] info; } function addinfo(string memory helloWorldStr) internal pure returns (string memory ){ return string.concat(helloWorldStr, from Franks contract); } }合约工厂操作合约// SPDX-License-Identifier: MIT pragma solidity ^0.8.20; import { HelloWorld } from ./test.sol; // 1、直接引入一个文件系统下的合约 // 2、引入github上的合约 // 3、通过包引入第三方的包 contract HelloWorldFactory{ HelloWorld hw; HelloWorld[] hws; function creatHelloWorld() public { hw new HelloWorld(); hws.push(hw); } function getHelloWorldByIndex(uint _index) public view returns (HelloWorld) { return hws[_index]; } function callSayHelloFromFactory(uint _index, uint _id) public view returns(string memory){ return hws[_index].sayHello(_id); } function callSetHelloWorldFromFactory(uint _index,string memory newString, uint _id)public { hws[_index].setHelloWorld(newString,_id); } }

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑