0
任何人都可以解釋mapping
的工作原理以及它爲什麼使用它?像數組是一個項目的集合。我沒有紮實的經驗,我剛剛開始。我在solidity官方文檔頁面上找到了這個代碼。繪圖如何在穩固中起作用,並且映射類似於其他流行語言中的另一種概念
pragma solidity ^0.4.11;
Contract CrowdFunding {
// Defines a new type with two fields.
struct Funder {
address addr;
uint amount;
}
struct Campaign {
address beneficiary;
uint fundingGoal;
uint numFunders;
uint amount;
mapping (uint => Funder) funders;
}
uint numCampaigns;
mapping (uint => Campaign) campaigns;
function newCampaign(address beneficiary, uint goal) returns (uint campaignID) {
campaignID = numCampaigns++; // campaignID is return variable
// Creates new struct and saves in storage. We leave out the mapping type.
campaigns[campaignID] = Campaign(beneficiary, goal, 0, 0);
}
function contribute(uint campaignID) payable {
Campaign storage c = campaigns[campaignID];
// Creates a new temporary memory struct, initialised with the given values
// and copies it over to storage.
// Note that you can also use Funder(msg.sender, msg.value) to initialise.
c.funders[c.numFunders++] = Funder({addr: msg.sender, amount: msg.value});
c.amount += msg.value;
}
function checkGoalReached(uint campaignID) returns (bool reached) {
Campaign storage c = campaigns[campaignID];
if (c.amount < c.fundingGoal)
return false;
uint amount = c.amount;
c.amount = 0;
c.beneficiary.transfer(amount);
return true;
}
}
很好的解釋。此外,映射解釋在這裏:https://ethereum.stackexchange.com/questions/9893/how-does-mapping-in-solidity-work 我想補充說,有可能有一個映射內部映射在Solidity ;) –