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发行说明。
In the real world, you'll often find many individual objects all of the same kind.在现实世界中,您经常会发现许多相同类型的单独对象。There may be thousands of other bicycles in existence, all of the same make and model.可能还有成千上万辆其他的自行车,都是同样的品牌和型号。Each bicycle was built from the same set of blueprints and therefore contains the same components.每辆自行车都是由同一套蓝图制成的,因此包含相同的部件。In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles.在面向对象的术语中,我们说您的自行车是被称为自行车的对象类的实例。A class is the blueprint from which individual objects are created.类是创建单个对象的蓝图。
The following 以下Bicycle
class is one possible implementation of a bicycle:Bicycle
类别是自行车的一种可能实现:
class Bicycle { int cadence = 0; int speed = 0; int gear = 1; void changeCadence(int newValue) { cadence = newValue; } void changeGear(int newValue) { gear = newValue; } void speedUp(int increment) { speed = speed + increment; } void applyBrakes(int decrement) { speed = speed - decrement; } void printStates() { System.out.println("cadence:" + cadence + " speed:" + speed + " gear:" + gear); } }
The syntax of the Java programming language will look new to you, but the design of this class is based on the previous discussion of bicycle objects.Java编程语言的语法对您来说是新的,但是这个类的设计是基于前面对bicycle对象的讨论。The fields 字段cadence
, speed
, and gear
represent the object's state, and the methods (changeCadence
, changeGear
, speedUp
etc.) define its interaction with the outside world.cadence
、speed
和gear
表示对象的状态,而方法(changeCadence
、changeGear
、speedUp
等)定义了它与外部世界的互动。
You may have noticed that the 您可能已经注意到Bicycle
class does not contain a main
method.Bicycle
类不包含main
方法。That's because it's not a complete application; it's just the blueprint for bicycles that might be used in an application.那是因为它不是一个完整的应用程序;这只是自行车的蓝图,可能会在应用程序中使用。The responsibility of creating and using new 创建和使用新Bicycle
objects belongs to some other class in your application.Bicycle
对象的责任属于应用程序中的其他类。
Here's a 下面是一个BicycleDemo
class that creates two separate Bicycle
objects and invokes their methods:BicycleDemo
类,它创建两个单独的Bicycle
对象并调用它们的方法:
class BicycleDemo { public static void main(String[] args) { // Create two different // Bicycle objects Bicycle bike1 = new Bicycle(); Bicycle bike2 = new Bicycle(); // Invoke methods on // those objects bike1.changeCadence(50); bike1.speedUp(10); bike1.changeGear(2); bike1.printStates(); bike2.changeCadence(50); bike2.speedUp(10); bike2.changeGear(2); bike2.changeCadence(40); bike2.speedUp(10); bike2.changeGear(3); bike2.printStates(); } }
The output of this test prints the ending pedal cadence, speed, and gear for the two bicycles:此测试的输出打印两辆自行车的结束踏板步频、速度和档位:
cadence:50 speed:10 gear:2 cadence:40 speed:20 gear:3