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发行说明。
There are several kinds of variables:有几种类型的变量:
The Bicycle
class uses the following lines of code to define its fields:Bicycle
类使用以下代码行定义其字段:
public int cadence; public int gear; public int speed;
Field declarations are composed of three components, in order:字段声明按顺序由三个组件组成:
public
or private
.public
或private
。The fields of Bicycle
are named cadence
, gear
, and speed
and are all of data type integer (int
).Bicycle
的字段名为cadence
、gear
和speed
,并且都是数据类型integer(int
)。The public
keyword identifies these fields as public members, accessible by any object that can access the class.public
关键字将这些字段标识为公共成员,可由任何可以访问该类的对象访问。
The first (left-most) modifier used lets you control what other classes have access to a member field.使用的第一个(最左侧)修饰符可以控制哪些其他类可以访问成员字段。For the moment, consider only 目前,只考虑public
and private
.public
和private
。Other access modifiers will be discussed later.其他访问修饰符将在后面讨论。
public
private
In the spirit of encapsulation, it is common to make fields private.本着封装的精神,通常将字段设置为私有。This means that they can only be directly accessed from the Bicycle class.这意味着它们只能从Bicycle
类直接访问。We still need access to these values, however.然而,我们仍然需要访问这些值。This can be done indirectly by adding public methods that obtain the field values for us:这可以通过添加为我们获取字段值的公共方法间接实现:
public class Bicycle { private int cadence; private int gear; private int speed; public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } public int getCadence() { return cadence; } public void setCadence(int newValue) { cadence = newValue; } public int getGear() { return gear; } public void setGear(int newValue) { gear = newValue; } public int getSpeed() { return speed; } public void applyBrake(int decrement) { speed -= decrement; } public void speedUp(int increment) { speed += increment; } }
All variables must have a type.所有变量都必须有一个类型。You can use primitive types such as 您可以使用基本类型,如int
, float
, boolean
, etc. Or you can use reference types, such as strings, arrays, or objects.int
、float
、boolean
等,也可以使用引用类型,如字符串、数组或对象。
All variables, whether they are fields, local variables, or parameters, follow the same naming rules and conventions that were covered in the Language Basics lesson, VariablesNaming.所有变量,无论是字段、局部变量还是参数,都遵循与“语言基础”课程“变量命名”中所述相同的命名规则和约定。
In this lesson, be aware that the same naming rules and conventions are used for method and class names, except that在本课中,请注意,方法名和类名使用相同的命名规则和约定,除了