The Java™ Tutorials - Learning the Java Language - Classes and Objects - Classes - Declaring Classes

Declaring Classes 声明类

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.

意味着 MyClassMySuperClass 的子类,并且它实现了 YourInterface 接口。

You can also add modifiers like public or private at the very beginning—so you can see that the opening line of a class declaration can become quite complicated. The modifiers public and private, which determine what other classes can access MyClass, are discussed later in this lesson. The lesson on interfaces and inheritance will explain how and why you would use the extends and implements keywords in a class declaration. For the moment you do not need to worry about these extra complications.

您还可以在最开始添加诸如 *public 或 private 之类的修饰符 —— 因此您可以看到类声明的开始行可能变得非常复杂。决定其他哪些类可以访问 MyClass 的修饰符 publicprivate 将在本课的后面讨论。关于接口和继承的课程将解释如何以及为什么在类声明中使用 extendsimplements 关键字。目前你不需要担心这些额外的复杂性。

In general, class declarations can include these components, in order:

一般来说,类声明可以包含这些组件,顺序如下:

  1. Modifiers such as public, private, and a number of others that you will encounter later. (However, note that the private modifier can only be applied to Nested Classes.) 例如 publicprivate,以及稍后将会遇到的一些其他修饰符。(但是,请注意 private 修饰符只能应用于嵌套类。)
  2. The class name, with the initial letter capitalized by convention. 按照约定,类名的首字母大写。
  3. The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent. 如果类有显式继承自一个父类的话,在其父类名称的前面加上关键字 extends。一个类只能继承自一个父类。
  4. A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface. 如果存在由类实现的以逗号分隔的接口列表,在其前面加关键字 implements。一个类可以实现多个接口。
  5. The class body, surrounded by braces, {}. 类的主体由大括号包围,{}。

原文

The Java™ Tutorials - Learning the Java Language - Classes and Objects - Classes - Declaring Classes

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值