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发行说明。
The introduction to object-oriented concepts in the lesson titled Object-oriented Programming Conceptsused a bicycle class as an example, with racing bikes, mountain bikes, and tandem bikes as subclasses.在题为面向对象编程概念的课程中,对面向对象概念的介绍以自行车类为例,将赛车、山地自行车和串联自行车作为子类。Here is sample code for a possible implementation of a 下面是一个可能实现的Bicycle
class, to give you an overview of a class declaration.Bicycle
类的示例代码,让您概括了解类声明。Subsequent sections of this lesson will back up and explain class declarations step by step. For the moment, don't concern yourself with the details.本课程的后续部分将逐步备份和解释类声明。目前,不要关心细节。
public class Bicycle { // the Bicycle class has // three fields public int cadence; public int gear; public int speed; // the Bicycle class has // one constructor public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } // the Bicycle class has // four methods public void setCadence(int newValue) { cadence = newValue; } public void setGear(int newValue) { gear = newValue; } public void applyBrake(int decrement) { speed -= decrement; } public void speedUp(int increment) { speed += increment; } }
A class declaration for a 作为MountainBike
class that is a subclass of Bicycle
might look like this:Bicycle
子类的MountainBike
类的类声明可能如下所示:
public class MountainBike extends Bicycle { // the MountainBike subclass has // one field public int seatHeight; // the MountainBike subclass has // one constructor public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) { super(startCadence, startSpeed, startGear); seatHeight = startHeight; } // the MountainBike subclass has // one method public void setHeight(int newValue) { seatHeight = newValue; } }
MountainBike
inherits all the fields and methods of Bicycle
and adds the field seatHeight
and a method to set it (mountain bikes have seats that can be moved up and down as the terrain demands).MountainBike
继承了自行车的所有字段和方法,并添加了字段seatHeight
和设置它的方法(山地自行车的座椅可以根据地形需要上下移动)。