// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
//给汽车指定品牌和价格
contract Car{
string Brand;
uint public price; //price设置为public相当于有一个返回函数
function setBrand(string memory newBrand,uint newPrice) public {
Brand=newBrand;
price=newPrice;
}
function getBrand() public view returns(string memory){
return Brand;
}
}
字符串改成bytes32类型,则需要web3.toHex转换
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
//给汽车指定品牌和价格
contract Car{
bytes32 Brand;
uint public price; //price设置为public
function setBrand(bytes32 newBrand,uint newPrice) public {
Brand=newBrand;
price=newPrice;
}
function getBrand() public view returns(bytes32 ){
return Brand;
}
}
web3.toHex("Audi") 输出:0x41756469
web3.toUtf8()
初始化赋值
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
//给汽车指定品牌和价格
contract Car{
string Brand;
uint public price;
//定义一个初始值
constructor(string memory initBrand,uint initPrice) public{
Brand=initBrand;
price=initPrice;
}
function setBrand(string memory newBrand,uint newPrice) public {
Brand=newBrand;
price=newPrice;
}
function getBrand() public view returns(string memory){
return Brand;
}
}