接口和抽象类有什么区别?

接口和抽象类有什么区别?

问题描述:

接口和抽象类到底有什么区别?

What exactly is the difference between an interface and an abstract class?

接口

界面是合同:界面的编写者说,嘿,我接受看起来那样的东西",使用界面的人说好吧,我写的课看起来就是这样".

Interfaces

An interface is a contract: The person writing the interface says, "hey, I accept things looking that way", and the person using the interface says "OK, the class I write looks that way".

接口是一个空壳.只有方法的签名,这意味着方法没有主体.界面什么也做不了.这只是一种模式.

An interface is an empty shell. There are only the signatures of the methods, which implies that the methods do not have a body. The interface can't do anything. It's just a pattern.

例如(伪代码):

// I say all motor vehicles should look like this:
interface MotorVehicle
{
    void run();

    int getFuel();
}

// My team mate complies and writes vehicle looking that way
class Car implements MotorVehicle
{

    int fuel;

    void run()
    {
        print("Wrroooooooom");
    }


    int getFuel()
    {
        return this.fuel;
    }
}

实现一个接口消耗很少的 CPU,因为它不是一个类,只是一堆名称,因此没有任何昂贵的查找要做.它在重要的时候非常棒,例如在嵌入式设备中.

Implementing an interface consumes very little CPU, because it's not a class, just a bunch of names, and therefore there isn't any expensive look-up to do. It's great when it matters, such as in embedded devices.

与接口不同,抽象类是类.它们的使用成本更高,因为从它们继承时需要进行查找.

Abstract classes, unlike interfaces, are classes. They are more expensive to use, because there is a look-up to do when you inherit from them.

抽象类看起来很像接口,但它们还有更多的东西:您可以为它们定义行为.更像是一个人说,这些类应该看起来像那样,而且它们有共同点,所以填空!".

Abstract classes look a lot like interfaces, but they have something more: You can define a behavior for them. It's more about a person saying, "these classes should look like that, and they have that in common, so fill in the blanks!".

例如:

// I say all motor vehicles should look like this:
abstract class MotorVehicle
{

    int fuel;

    // They ALL have fuel, so lets implement this for everybody.
    int getFuel()
    {
         return this.fuel;
    }

    // That can be very different, force them to provide their
    // own implementation.
    abstract void run();
}

// My teammate complies and writes vehicle looking that way
class Car extends MotorVehicle
{
    void run()
    {
        print("Wrroooooooom");
    }
}

实施

虽然抽象类和接口应该是不同的概念,但实现使该声明有时不正确.有时,它们甚至不是您认为的那样.


Implementation

While abstract classes and interfaces are supposed to be different concepts, the implementations make that statement sometimes untrue. Sometimes, they are not even what you think they are.

在 Java 中,此规则被强制执行,而在 PHP 中,接口是没有声明方法的抽象类.

In Java, this rule is strongly enforced, while in PHP, interfaces are abstract classes with no method declared.

在 Python 中,抽象类更像是一种编程技巧,您可以从 ABC 模块中获得,并且实际上使用元类,因此也使用类.接口与这种语言中的鸭子类型更相关,它是约定和调用描述符的特殊方法(__method__ 方法)之间的混合.

In Python, abstract classes are more a programming trick you can get from the ABC module and is actually using metaclasses, and therefore classes. And interfaces are more related to duck typing in this language and it's a mix between conventions and special methods that call descriptors (the __method__ methods).

和编程一样,有另一种语言的理论、实践和实践:-)

As usual with programming, there is theory, practice, and practice in another language :-)