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发行说明。
A class contains constructors that are invoked to create objects from the class blueprint.类包含被调用以从类蓝图创建对象的构造函数。Constructor declarations look like method declarationsexcept 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.您可以在构造函数的声明中使用访问修饰符来控制哪些其他类可以调用构造函数。
MyClass
constructor, it cannot directly create MyClass
objects.MyClass
构造函数,它就不能直接创建MyClass
对象。