本篇可以认为是 第85篇 笔记-用合约创建合约 的续篇;
介绍在工厂模式下,如何随意创建并管理新合约;
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;
contract Car {
address public owner;
string public model;
address public carAddr;
constructor(address _owner, string memory _model) payable {
owner = _owner;
model = _model;
carAddr = address(this);
}
}
contract CarFactory {
Car[] public cars;
function create(address _owner, string memory _model) public {
Car car = new Car(_owner, _model);
cars.push(car);
}
function createAndSendEther(address _owner, string memory _model)
public
payable
{
Car car = (new Car){value: msg.value}(_owner, _model);
cars.push(car);
}
function create2(
address _owner,
string memo