Java中的接口是什么? [英] What is an interface in Java?

查看:78
本文介绍了Java中的接口是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如这个问题的对立点:Java中的界面是什么?

Just as a counterpoint to this question: what is an interface in Java?

推荐答案

接口是抽象类的一种特殊形式,它不实现任何方法。在Java中,您可以创建如下界面:

An interface is a special form of an abstract class which does not implement any methods. In Java, you create an interface like this:

interface Interface
{
    void interfaceMethod();
}

由于界面无法实现任何方法,因此暗示整个事物,包括所有方法,都是公共的和抽象的(Java术语中的抽象意味着不是由这个类实现的)。所以上面的界面与下面的界面相同:

Since the interface can't implement any methods, it's implied that the entire thing, including all the methods, are both public and abstract (abstract in Java terms means "not implemented by this class"). So the interface above is identical to the interface below:

public interface Interface
{
    abstract public void interfaceMethod();
}

要使用此界面,只需实现界面即可。许多类可以实现一个接口,一个类可以实现许多接口:

To use this interface, you simply need to implement the interface. Many classes can implement an interface, and a class can implement many interfaces:

interface InterfaceA
{
     void interfaceMethodA();
}

interface InterfaceB
{
    void interfaceMethodB();
}

public class ImplementingClassA
    implements InterfaceA, InterfaceB
{
    public void interfaceMethodA()
    {
        System.out.println("interfaceA, interfaceMethodA, implementation A");
    }

    public void interfaceMethodB()
    {
        System.out.println("interfaceB, interfaceMethodB, implementation A");
    }
}

public class ImplementingClassB
    implements InterfaceA, InterfaceB
{
    public void interfaceMethodA()
    {
         System.out.println("interfaceA, interfaceMethodA, implementation B");
    }

    public void interfaceMethodB()
    {
        System.out.println("interfaceB, interfaceMethodB, implementation B");
    }
}

现在如果你想要你可以写一个像这样的方法:

Now if you wanted you could write a method like this:

public void testInterfaces()
{
    ImplementingClassA u = new ImplementingClassA();
    ImplementingClassB v = new ImplementingClassB();
    InterfaceA w = new ImplementingClassA();
    InterfaceA x = new ImplementingClassB();
    InterfaceB y = new ImplementingClassA();
    InterfaceB z = new ImplementingClassB();

    u.interfaceMethodA();
    // prints "interfaceA, interfaceMethodA, implementation A"
    u.interfaceMethodB();
    // prints "interfaceB, interfaceMethodB, implementation A"
    v.interfaceMethodA();
    // prints "interfaceA, interfaceMethodA, implementation B"
    v.interfaceMethodB();
    // prints "interfaceB, interfaceMethodB, implementation B"
    w.interfaceMethodA();
    // prints "interfaceA, interfaceMethodA, implementation A"
    x.interfaceMethodA();
    // prints "interfaceA, interfaceMethodA, implementation B"
    y.interfaceMethodB();
    // prints "interfaceB, interfaceMethodB, implementation A"
    z.interfaceMethodB();
    // prints "interfaceB, interfaceMethodB, implementation B"
}

但是,你可以从不执行以下操作:

However, you could never do the following:

public void testInterfaces()
{
    InterfaceA y = new ImplementingClassA();
    InterfaceB z = new ImplementingClassB();

    y.interfaceMethodB(); // ERROR!
    z.interfaceMethodA(); // ERROR!
}

你不能这样做的原因是 y 的类型为 interfaceA ,并且<$ c中没有 interfaceMethodB() $ C>了InterfaceA 。同样, z 的类型为 interfaceB ,并且没有 interfaceMethodA() in interfaceB

The reason you can't do this is that y is of type interfaceA, and there is no interfaceMethodB() in interfaceA. Likewise, z is of type interfaceB and there is no interfaceMethodA() in interfaceB.

我之前提到过,接口只是抽象类的一种特殊形式。为了说明这一点,请查看以下代码。

I mentioned earlier that interfaces are just a special form of an abstract class. To illustrate that point, look at the following code.

interface Interface
{
    void abstractMethod();
}

abstract public class AbstractClass
{
    abstract public void abstractMethod();
}

您将从这些类中继承几乎完全相同的方式:

You would inherit from these classes almost exactly the same way:

public class InheritsFromInterface
    implements Interface
{
    public void abstractMethod() { System.out.println("abstractMethod()"); }
}

public class InteritsFromAbstractClass
    extends AbstractClass
{
    public void abstractMethod() { System.out.println("abstractMethod()"); }
}

事实上,你甚至可以像这样更改界面和抽象类:

In fact, you could even change the interface and the abstract class like this:

interface Interface
{
    void abstractMethod();
}

abstract public class AbstractClass
    implements Interface
{
    abstract public void abstractMethod();
}

public class InheritsFromInterfaceAndAbstractClass
    extends AbstractClass implements Interface
{
    public void abstractMethod() { System.out.println("abstractMethod()"); }
}

但是,接口和抽象类之间存在两个区别。

However, there are two differences between interfaces and abstract classes.

第一个区别是接口无法实现方法。

The first difference is that interfaces cannot implement methods.

interface Interface
{
    public void implementedMethod()
    {
        System.out.println("implementedMethod()");
    }
}

上面的接口会产生编译错误,因为它有一个 implementedMethod()的实现。如果你想实现该方法但不能实例化该类,你必须这样做:

The interface above generates a compiler error because it has an implementation for implementedMethod(). If you wanted to implement the method but not be able to instantiate the class, you would have to do it like this:

abstract public class AbstractClass
{
    public void implementedMethod()
    {
        System.out.println("implementedMethod()");
    }
}

这不是一个抽象类,因为它没有一个成员是抽象的,但它是合法的Java。

That's not much of an abstract class because none of its members are abstract, but it is legal Java.

接口和抽象类之间的另一个区别是类可以从多个接口继承,但只能从一个抽象继承class。

The other difference between interfaces and abstract classes is that a class can inherit from multiple interfaces, but can only inherit from one abstract class.

abstract public class AbstractClassA { }
abstract public class AbstractClassB { }
public class InheritsFromTwoAbstractClasses
    extends AbstractClassA, AbstractClassB
{ }

上面的代码生成编译器错误,不是因为类都是空的,而是因为 InheritsFromTwoAbstractClasses 试图从两个抽象类继承,这是非法的。以下是完全合法的。

The code above generates a compiler error, not because the classes are all empty, but because InheritsFromTwoAbstractClasses is trying to inherit from two abstract classes, which is illegal. The following is perfectly legal.

interface InterfaceA { }
interface InterfaceB { }
public class InheritsFromTwoInterfaces
    implements InterfaceA, InterfaceB
{ }    

接口和抽象类之间的第一个区别是第二个差异的原因。看看下面的代码。

The first difference between interfaces and abstract classes is the reason for the second difference. Take a look at the following code.

interface InterfaceA
{
    void method();
}

interface InterfaceB
{
    void method();
}

public class InheritsFromTwoInterfaces
    implements InterfaceA, InterfaceB
{
    void method() { System.out.println("method()"); }
}

上面的代码没有问题,因为 InterfaceA InterfaceB 没有任何要隐藏的内容。很容易判断对方法的调用将打印method()。

There's no problem with the code above because InterfaceA and InterfaceB don't have anything to hide. It's easy to tell that a call to method will print "method()".

现在看看以下内容代码:

Now look at the following code:

abstract public class AbstractClassA
{
    void method() { System.out.println("Hello"); }
}

abstract public class AbstractClassB
{
    void method() { System.out.println("Goodbye"); }
}

public class InheritsFromTwoAbstractClasses
    extends AbstractClassA, AbstractClassB
{ }

这与我们的其他示例完全相同,只是因为我们被允许在抽象类中实现方法,我们这样做了,因为我们不必实现已经实现的方法在继承类中,我们没有。但你可能已经注意到了,这是一个问题。当我们调用 new InheritsFromTwoAbstractClasses()。method()时会发生什么?它会打印Hello还是Goodbye?您可能不知道,Java编译器也不知道。另一种语言,C ++允许这种继承,他们以通常非常复杂的方式解决了这些问题。为了避免这种麻烦,Java决定将这种多重继承视为非法。

This is exactly the same as our other example, except that because we're allowed to implement methods in abstract classes, we did, and because we don't have to implement already-implemented methods in an inheriting class, we didn't. But you may have noticed, there's a problem. What happens when we call new InheritsFromTwoAbstractClasses().method()? Does it print "Hello" or "Goodbye"? You probably don't know, and neither does the Java compiler. Another language, C++ allowed this kind of inheritance and they resolved these issues in ways that were often very complicated. To avoid this kind of trouble, Java decided to make this "multiple inheritance" illegal.

Java解决方案的缺点是无法完成以下任务:

The downside to Java's solution that the following can't be done:

abstract public class AbstractClassA
{
    void hi() { System.out.println("Hello"); }
}

abstract public class AbstractClassB
{
    void bye() { System.out.println("Goodbye"); }
}

public class InheritsFromTwoAbstractClasses
    extends AbstractClassA, AbstractClassB
{ }

AbstractClassA AbstractClassB 是mixins或非意图的类要实例化,但通过继承向它们混入的类添加功能。如果你调用 new InheritsFromTwoAbstractClasses()。hi() new InheritsFromTwoAbstractClasses()。bye(),但是你不能这样做,因为Java不允许它。

AbstractClassA and AbstractClassB are "mixins" or classes that aren't intended to be instantiated but add functionality to the classes that they are "mixed into" through inheritance. There's obviously no problem figuring out what happens if you call new InheritsFromTwoAbstractClasses().hi() or new InheritsFromTwoAbstractClasses().bye(), but you can't do that because Java doesn't allow it.

(我知道这是一个很长的帖子,所以如果有任何错误请告诉我,我会更正。)

(I know this is a long post, so if there are any mistakes in it please let me know and I will correct them.)

这篇关于Java中的接口是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆