Documentation

The Java™ Tutorials
Hide TOC
Providing Constructors for Your Classes为类提供构造函数
Trail: Learning the Java Language
Lesson: Classes and Objects
Section: Classes

Providing Constructors for Your Classes为类提供构造函数

A class contains constructors that are invoked to create objects from the class blueprint.类包含被调用以从类蓝图创建对象的构造函数。Constructor declarations look like method declarations—except that they use the name of the class and have no return type.构造函数声明看起来像方法声明—但它们使用类的名称,并且没有返回类型。For example, Bicycle has one constructor:例如,Bicycle有一个构造函数:

public Bicycle(int startCadence, int startSpeed, int startGear) {
    gear = startGear;
    cadence = startCadence;
    speed = startSpeed;
}

To create a new Bicycle object called myBike, a constructor is called by the new operator:要创建名为myBike的新Bicycle对象,new运算符将调用构造函数:

Bicycle myBike = new Bicycle(30, 0, 8);

new Bicycle(30, 0, 8) creates space in memory for the object and initializes its fields.在内存中为对象创建空间并初始化其字段。

Although Bicycle only has one constructor, it could have others, including a no-argument constructor:尽管Bicycle只有一个构造函数,但它也可以有其他构造函数,包括无参数构造函数:

public Bicycle() {
    gear = 1;
    cadence = 10;
    speed = 0;
}

Bicycle yourBike = new Bicycle(); invokes the no-argument constructor to create a new Bicycle object called yourBike.调用无参数构造函数以创建名为yourBike的新Bicycle对象。

Both constructors could have been declared in Bicycle because they have different argument lists.这两个构造函数都可以在Bicycle中声明,因为它们具有不同的参数列表。As with methods, the Java platform differentiates constructors on the basis of the number of arguments in the list and their types.与方法一样,Java平台根据列表中参数的数量及其类型来区分构造函数。You cannot write two constructors that have the same number and type of arguments for the same class, because the platform would not be able to tell them apart. Doing so causes a compile-time error.不能为同一个类编写具有相同数量和类型参数的两个构造函数,因为平台无法区分它们。这样做会导致编译时错误。

You don't have to provide any constructors for your class, but you must be careful when doing this.您不必为类提供任何构造函数,但在这样做时必须小心。The compiler automatically provides a no-argument, default constructor for any class without constructors.编译器会自动为任何没有构造函数的类提供一个无参数的默认构造函数。This default constructor will call the no-argument constructor of the superclass.此默认构造函数将调用超类的无参数构造函数。In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does.在这种情况下,如果超类没有无参数构造函数,编译器会抱怨,因此必须验证它是否有。If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor.如果您的类没有显式的超类,那么它有一个隐式的Object超类,它确实有一个无参数构造函数。

You can use a superclass constructor yourself.您可以自己使用超类构造函数。The MountainBike class at the beginning of this lesson did just that.本课程开始时的MountainBike类就是这么做的。This will be discussed later, in the lesson on interfaces and inheritance.这将在后面关于接口和继承的课程中讨论。

You can use access modifiers in a constructor's declaration to control which other classes can call the constructor.您可以在构造函数的声明中使用访问修饰符来控制哪些其他类可以调用构造函数。


Note: If another class cannot call a MyClass constructor, it cannot directly create MyClass objects.如果另一个类不能调用MyClass构造函数,它就不能直接创建MyClass对象。

Previous page: Defining Methods
Next page: Passing Information to a Method or a Constructor