Skip to main content
The Trailblazer Community will undergo maintenance on Saturday, November 15, 2025 and Sunday, November 16, 2025. Please plan your activities accordingly.

把对象实例化并调用执行匿名中的方法

备注

备注

用中文(简体)学习?在中文(简体)Trailhead Playground 中开始挑战,用括号中提供的译文完成挑战。仅复制并粘贴英文值,因为挑战验证基于英文数据。如果在中文(简体)组织中没有成功通过挑战,我们建议您 (1) 将区域设置切换为美国,(2) 按此处说明将语言切换为英文,(3) 再次单击“检查挑战”按钮。

查看 Trailhead 本地化语言徽章详细了解如何利用 Trailhead 译文。

跟随 Trail Together 进行学习

进行这一步骤时,想要跟专家一起学习吗?观看此视频,它是 Trail Together 系列的一部分。

(这部分内容从 11:30 开始,如果您想要倒回去再次观看步骤的开头部分可以从这里开始。)

创建对象

在这里您将基于 BankAcct 类创建一个支票帐户对象。技术上这称作对象实例化:为一个由它的类定义的对象创建一个特定的实例。然后,您调用 makeDeposit 方法来运行代码,给帐户余额充值。

  1. 在 Developer Console 中,请打开 Execute Anonymous(执行匿名)窗口。(单击 Debug | Open Execute Anonymous Window(调试|打开执行匿名窗口)。)
  2. 把现有代码替换为这个代码:
    //From the BankAcct class, instantiate an object named chkAcct
    BankAcct chkAcct = new BankAcct();
    //Set the accttype attribute of the chkAcct object to Checking
    chkAcct.accttype = 'Checking';
    //Set the acctName attribute of the chkAcct object to D.Castillo-Chk
    chkAcct.acctName = 'D.Castillo-Chk';
    //Invoke the makeDeposit method with 150 as the argument
    chkAcct.makeDeposit(150);
    System.debug(chkAcct.getBalance());
  3. 确认 Open Log(打开日志)被选中,然后单击 Execute(执行)。执行日志打开。
  4. 选中 Debug Only(仅限调试)
  5. 查看调试消息,然后关闭日志。
在 Salesforce 帮助中分享 Trailhead 反馈

我们很想听听您使用 Trailhead 的经验——您现在可以随时从 Salesforce 帮助网站访问新的反馈表单。

了解更多 继续分享反馈