Documentation

The Java™ Tutorials
Hide TOC
Declaring Member Variables声明成员变量
Trail: Learning the Java Language
Lesson: Classes and Objects
Section: Classes

Declaring Member Variables声明成员变量

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:字段声明按顺序由三个组件组成:

  1. Zero or more modifiers, such as public or private.零个或多个修饰符,如publicprivate
  2. The field's type.字段的类型。
  3. The field's name.字段的名称。

The fields of Bicycle are named cadence, gear, and speed and are all of data type integer (int).Bicycle的字段名为cadencegearspeed,并且都是数据类型integer(int)。The public keyword identifies these fields as public members, accessible by any object that can access the class.public关键字将这些字段标识为公共成员,可由任何可以访问该类的对象访问。

Access Modifiers访问修改函数

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.目前,只考虑publicprivateOther access modifiers will be discussed later.其他访问修饰符将在后面讨论。

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;
    }
}

Types类型

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.您可以使用基本类型,如intfloatboolean等,也可以使用引用类型,如字符串、数组或对象。

Variable Names变量名

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, Variables—Naming.所有变量,无论是字段、局部变量还是参数,都遵循与“语言基础”课程“变量—命名”中所述相同的命名规则和约定。

In this lesson, be aware that the same naming rules and conventions are used for method and class names, except that在本课中,请注意,方法名和类名使用相同的命名规则和约定,除了


Previous page: Declaring Classes
Next page: Defining Methods