Skip to Content
学习 Java,第 6 版
book

学习 Java,第 6 版

by Marc Loy, Patrick Niemeyer, Daniel Leuck
May 2025
Intermediate to advanced
552 pages
7h 22m
Chinese
O'Reilly Media, Inc.
Content preview from 学习 Java,第 6 版

附录 B. 练习答案

本附录包含每章末复习题的答案(通常还有一些背景知识)。代码练习答案与示例程序的源代码下载一起包含在练习文件夹中。附录 A提供了获取源代码并在 IntelliJ IDEA 中进行设置的详细信息。

第 1 章:现代语言

  1. 目前由哪家公司维护 Java?

    Java 于 20 世纪 90 年代在 Sun Microsystems 公司开发,甲骨文公司于 2009 年收购了 Sun 公司(因此也收购了 Java)。甲骨文公司保持所有权,并且是其商业 JDK 和开源 OpenJDK 开发和发布的积极合作伙伴。

  2. Java 开放源码开发工具包的名称是什么?

    JDK 的开源版本被称为 OpenJDK。

  3. 请说出在 Java 安全运行字节码的方法中发挥作用的两个主要组件。

    Java 有许多与安全有关的功能,但在每个 Java 应用程序中起作用的主要组件是类加载器和字节码校验器。

第 2 章:首次应用

  1. 应该使用什么命令来编译 Java 源文件?

    如果您在终端中工作,javac命令会编译 Java 源文件。虽然在使用 IntelliJ IDEA 等集成开发环境时,这些细节往往被隐藏起来,但集成开发环境也在幕后使用javac

  2. 运行 Java 应用程序时,JVM 如何知道从哪里开始?

    任何可执行的 Java 类都必须定义一个main() 方法。JVM 使用该方法作为入口点。

  3. 创建新类时,可以扩展多个类吗?

    Java 不支持从多个独立类直接多重继承。

  4. 创建新类时,可以实现多个接口吗?

    是的。Java 允许您根据需要实现尽可能多的接口。使用接口可以为程序员提供多重继承的大部分有用功能,而不会有很多缺陷。

  5. 哪个类表示图形应用程序中的 Windows?

    JFrame 类代表 Java 图形应用程序中使用的主窗口,不过后面几章将向您介绍一些低级类,它们也可以创建专门的窗口。

代码练习

一般情况下,我们不会在本附录中列出代码解决方案,但我们希望能够方便地检查您对第一个程序的解决方案。再见,Java!"的简单文本版本应该是这样的:

public class GoodbyeJava {
  public static void main(String[] args) {
    System.out.println("Goodbye, Java!");
  }
}

对于图形版本,您的代码应与下面相似:

import javax.swing.*;

public class GoodbyeJava {
  public static void main(String[] args) {
    JFrame frame = new JFrame("Chapter 2 Exercises");
    frame.setSize(300, 150);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel label = new JLabel("Goodbye, Java!", JLabel.CENTER);
    frame.add(label);
    frame.setVisible(true);
  }
}

请注意,我们在HelloJava2 中添加了额外的EXIT_ON_CLOSE ,以便在关闭应用时正确退出。如果您使用的是 IDEA,您可以使用 IDE 中的绿色播放按钮运行任一个类。如果你使用的是终端,可以切换到GoodbyeJava.java所在的目录,然后键入以下命令:

% javac ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

《学习 Python》第 5 版

《学习 Python》第 5 版

Mark Lutz
CSS:权威指南,第 5 版

CSS:权威指南,第 5 版

Eric Meyer, Estelle Weyl
詳解 システム・パフォーマンス 第2版

詳解 システム・パフォーマンス 第2版

Brendan Gregg, 西脇 靖紘, 長尾 高弘

Publisher Resources

ISBN: 9798341656772