您的第一个 Solidity 合约
使用 Remix IDE 编写并编译一个简单的“Hello World”智能合约,亲手体验 Solidity 语法。
您的第一个 Solidity 合约 是 CoddyKit 上的免费 Blockchain Smart Contracts with Solidity 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Blockchain Smart Contracts with Solidity 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Blockchain Smart Contracts with Solidity 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Your First Smart Contract!
Time to write your first smart contract! We'll build a simple Hello World in Solidity using the browser-based Remix IDE - no setup needed.
Meet Remix IDE
Remix IDE lets you write, compile, debug, and deploy Solidity contracts right in your browser. It's perfect for learning and quick prototyping.
Solidity Version Pragma
Every Solidity file opens with a pragma that pins the compiler version. The code below means versions 0.8.0 up to (but not including) 0.9.0 are allowed.
pragma solidity ^0.8.0;Defining Your Contract
Solidity code lives inside a contract, much like a class in OOP. You declare it with the contract keyword, a name, and curly braces, as shown.
contract MyFirstContract {
// Your contract's code goes here
}Adding a State Variable
A state variable is stored permanently on chain and defines your contract's state. Here we add a string called greeting; public makes it readable by anyone.
contract MyFirstContract {
string public greeting;
}The Constructor Function
The constructor runs exactly once, at deployment, to initialize state. Here it sets greeting to "Hello CoddyKit!" the moment the contract goes live.
contract MyFirstContract {
string public greeting;
constructor() {
greeting = "Hello CoddyKit!";
}
}Writing a View Function
A view function reads contract state without changing it. This getGreeting is public and returns a string from memory, as shown below.
contract MyFirstContract {
string public greeting;
constructor() {
greeting = "Hello CoddyKit!";
}
function getGreeting() public view returns (string memory) {
return greeting;
}
}Our Full 'Hello World' Contract
Here's the full Hello World contract. Paste it into Remix, compile, deploy to a test chain, then call getGreeting() to see it work.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleGreeting {
string public greeting;
constructor() {
greeting = "Hello CoddyKit!";
}
function getGreeting() public view returns (string memory) {
return greeting;
}
}Compiling Your Contract
Before deploying, a contract must be compiled - turning your Solidity into EVM bytecode. Remix has a built-in compiler with a handy Auto compile option.
Quick Check: Contract Structure
Which part of a Solidity smart contract specifies the minimum compatible compiler version?
Recap: Your First Contract
Recap: you built a Solidity contract with a pragma, a contract block, a state variable, a constructor, and a view function - all in Remix. Nice work!
用 AI 导师学习 Blockchain Smart Contracts with Solidity — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「您的第一个 Solidity 合约」课时是免费的吗?
是的 — 「您的第一个 Solidity 合约」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Blockchain Smart Contracts with Solidity 课程的其余内容,请升级到 CoddyKit PRO。 Blockchain Smart Contracts with Solidity 课程共包含 4 节课。
「您的第一个 Solidity 合约」这节课中我会学到什么?
使用 Remix IDE 编写并编译一个简单的“Hello World”智能合约,亲手体验 Solidity 语法。 你通过在浏览器中直接运行的动手代码来练习 Blockchain Smart Contracts with Solidity,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Blockchain Smart Contracts with Solidity 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Blockchain Smart Contracts with Solidity 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「您的第一个 Solidity 合约」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Blockchain Smart Contracts with Solidity 课中编写并运行代码吗?
能。每节 Blockchain Smart Contracts with Solidity 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 什么是区块链技术
- Ethereum 基础与 EVM
- 您的第一个 Solidity 合约
- 钱包、密钥与交易