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发行说明。
To declare a class that implements an interface, you include an 要声明实现接口的类,请在类声明中包含implements
clause in the class declaration. implements
子句。Your class can implement more than one interface, so the 您的类可以实现多个接口,因此implements
keyword is followed by a comma-separated list of the interfaces implemented by the class. implements
关键字后面是该类实现的接口的逗号分隔列表。By convention, the 按照惯例,implements
clause follows the extends
clause, if there is one.implements
子句位于extends
子句之后(如果有)。
Consider an interface that defines how to compare the size of objects.考虑一个接口,它定义了如何比较对象的大小。
public interface Relatable { // this (object calling isLargerThan) // and other must be instances of // the same class returns 1, 0, -1 // if this is greater than, // equal to, or less than other public int isLargerThan(Relatable other); }
If you want to be able to compare the size of similar objects, no matter what they are, the class that instantiates them should implement 如果您希望能够比较相似对象的大小,不管它们是什么,实例化它们的类应该实现Relatable
.Relatable
。
Any class can implement 如果有某种方法可以比较从类实例化的对象的相对“大小”,那么任何类都可以实现Relatable
if there is some way to compare the relative "size" of objects instantiated from the class. Relatable
。For strings, it could be number of characters; for books, it could be number of pages; for students, it could be weight; and so forth. 对于字符串,可以是字符数;对于书籍,可以是页数;对学生来说,可能是体重;等等For planar geometric objects, area would be a good choice (see the 对于平面几何对象,面积是一个很好的选择(请参见下面的RectanglePlus
class that follows), while volume would work for three-dimensional geometric objects. RectanglePlus
类),而体积则适用于三维几何对象。All such classes can implement the 所有这些类都可以实现isLargerThan()
method.isLargerThan()
方法。
If you know that a class implements 如果您知道一个类实现了Relatable
, then you know that you can compare the size of the objects instantiated from that class.Relatable
,那么您就知道可以比较从该类实例化的对象的大小。
Here is the 下面是创建对象部分中提供的Rectangle
class that was presented in the Creating Objects section, rewritten to implement Relatable
.Rectangle
类,它被重写以实现Relatable
。
public class RectanglePlus implements Relatable { public int width = 0; public int height = 0; public Point origin; // four constructors public RectanglePlus() { origin = new Point(0, 0); } public RectanglePlus(Point p) { origin = p; } public RectanglePlus(int w, int h) { origin = new Point(0, 0); width = w; height = h; } public RectanglePlus(Point p, int w, int h) { origin = p; width = w; height = h; } // a method for moving the rectangle public void move(int x, int y) { origin.x = x; origin.y = y; } // a method for computing // the area of the rectangle public int getArea() { return width * height; } // a method required to implement // the Relatable interface public int isLargerThan(Relatable other) { RectanglePlus otherRect = (RectanglePlus)other; if (this.getArea() < otherRect.getArea()) return -1; else if (this.getArea() > otherRect.getArea()) return 1; else return 0; } }
Because 因为RectanglePlus
implements Relatable
, the size of any two RectanglePlus
objects can be compared.RectanglePlus
实现了Relatable
,所以可以比较任意两个RectanglePlus
对象的大小。
isLargerThan
method, as defined in the Relatable
interface, takes an object of type Relatable
. Relatable
接口中定义的isLargerThan
方法接受Relatable
类型的对象。other
to a RectanglePlus
instance. other
强制转换为RectanglePlus
实例。getArea
directly on the other
instance (other.getArea()
) would fail to compile because the compiler does not understand that other
is actually an instance of RectanglePlus
. other
实例上调用getArea
(other.getArea()
)将无法编译,因为编译器不理解other
实际上是RectanglePlus
的实例。