Documentation

The Java™ Tutorials
Hide TOC
Implementing an Interface实现接口
Trail: Learning the Java Language
Lesson: Interfaces and Inheritance
Section: Interfaces

Implementing an Interface实现接口

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子句之后(如果有)。

A Sample Interface, Relatable一个示例接口,可关联

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. 如果有某种方法可以比较从类实例化的对象的相对“大小”,那么任何类都可以实现RelatableFor 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,那么您就知道可以比较从该类实例化的对象的大小。

Implementing the Relatable Interface实现相关接口

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对象的大小。


Note: The isLargerThan method, as defined in the Relatable interface, takes an object of type Relatable. Relatable接口中定义的isLargerThan方法接受Relatable类型的对象。The line of code, shown in bold in the previous example, casts other to a RectanglePlus instance. 前一个示例中以粗体显示的代码行将other强制转换为RectanglePlus实例。Type casting tells the compiler what the object really is. 类型转换告诉编译器对象实际上是什么。Invoking 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实例上调用getAreaother.getArea())将无法编译,因为编译器不理解other实际上是RectanglePlus的实例。

Previous page: Defining an Interface
Next page: Using an Interface as a Type