# 「The First Step from Java to Solidity」 **Published by:** [Untitled](https://paragraph.com/@0x79cfca78f0e0d76466013c0ea6a09d5a3e1ab837/) **Published on:** 2025-10-13 **URL:** https://paragraph.com/@0x79cfca78f0e0d76466013c0ea6a09d5a3e1ab837/the-first-step-from-java-to-solidity ## Content 这是我的第一个智能合约,用 Solidity 实现了一个简单的计数器。 合约源码 // SPDX-License-Identifier: MIT pragma solidity ^0.8.26; contract ClickCounter { uint256 public counter = block.number; function getCounter() public view returns (uint) { return block.number - counter; } function click() public { counter++; } function unclick() public { counter--; } } 编码解析 合约名称叫 ClickCounter,contract 类似 Java 的 class 关键字,用于声明一个合约 定义了一个全局变量 uint256 public counter = block.number; uint256:无符号整型 public:允许外部访问,会自动生成一个 getter 方法,相当于 Java 的对象可以直接访问 block.number:初始值为当前区块号 实现了三个核心方法 getCounter:会返回「当前区块号 - 初始区块号」,就像一个基于时间的计数器 click :将 counter 加 1 unclick:将 counter 减 1 我在 Cursor 中编写合约,将代码粘贴到 Remix 编辑器中会弹出安全警告,这对于初学者和非合约开发者非常友好,能在一定程度上避免误用非法脚本的可能,进而导致资金丢失(很友好:) New code pasted 上链过程 上链步骤也比较简单,先点击 Compile 编译代码,再在左侧 Tab 切换到 Deploy & run transactions 点击 Deploy 便可直接发布到相应的 Environment 上 编译部署 其中 Environment 那里使用 WalletConnect 切换到对应的 Sepolia Testnet Choose network 上链后右侧控制台会出现当前交易的信息,区块信息。基本都是一长串的 hash 值 左侧控制台包括了刚才编写的几个 public 方法,点击就会触发相应的方法,其中多了一个 counter 的 button 是因为代码里的 counter 被 public 修饰所以会多一个访问方法 Transaction details 由于我的合约是部署 sepolia 链上,这是一个以太坊的测试链,将 transaction hash 或者 block hash 粘贴到官方搜索框可以一键查询出交易的信息 https://sepolia.etherscan.io/ 打卡记录下我的第一个智能合约 My first smart contract 小结 这次体验最大的感受是所有的开发部署记录,交易信息都可以运行在公开的链上,并且都是不可篡改的,公开透明,这种感觉太棒了 ## Publication Information - [Untitled](https://paragraph.com/@0x79cfca78f0e0d76466013c0ea6a09d5a3e1ab837/): Publication homepage - [All Posts](https://paragraph.com/@0x79cfca78f0e0d76466013c0ea6a09d5a3e1ab837/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@0x79cfca78f0e0d76466013c0ea6a09d5a3e1ab837): Subscribe to updates