The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
You've seen classes defined in the following way:您已经看到了以以下方式定义的类:
class MyClass { // field, constructor, and // method declarations }
This is a class declaration.这是一个类声明。The class body (the area between the braces) contains all the code that provides for the life cycle of the objects created from the class: constructors for initializing new objects, declarations for the fields that provide the state of the class and its objects, and methods to implement the behavior of the class and its objects.类主体(大括号之间的区域)包含为从类创建的对象的生命周期提供的所有代码:初始化新对象的构造函数、提供类及其对象状态的字段声明,以及实现类及其对象行为的方法。
The preceding class declaration is a minimal one.前面的类声明是最小的声明。It contains only those components of a class declaration that are required.它只包含类声明中需要的组件。You can provide more information about the class, such as the name of its superclass, whether it implements any interfaces, and so on, at the start of the class declaration.您可以在类声明的开头提供有关该类的更多信息,例如其超类的名称、它是否实现了任何接口等。For example,例如
class MyClass extends MySuperClass implements YourInterface { // field, constructor, and // method declarations }
means that 意味着MyClass
is a subclass of MySuperClass
and that it implements the YourInterface
interface.MyClass
是MySuperClass
的一个子类,它实现了YourInterface
接口。
You can also add modifiers like public or private at the very beginningso you can see that the opening line of a class declaration can become quite complicated.您还可以在开始时添加诸如public
或private
之类的修饰符因此,您可以看到类声明的开头行可能变得非常复杂。The modifiers public and private, which determine what other classes can access 修饰符MyClass
, are discussed later in this lesson.public
和private
决定哪些其他类可以访问MyClass
,将在本课后面讨论。The lesson on interfaces and inheritance will explain how and why you would use the extends and implements keywords in a class declaration.关于接口和继承的课程将解释如何以及为什么在类声明中使用extends
和implements
关键字。For the moment you do not need to worry about these extra complications.目前,你不必担心这些额外的并发症。
In general, class declarations can include these components, in order:通常,类声明可以包括以下组件:
public
、private
以及稍后将遇到的其他一些修饰符。private
修饰符只能应用于嵌套类。)extends
。implements
。{}
包围。