抽象类 vs. 接口 vs. mixin [英] Abstract classes vs. interfaces vs. mixins

查看:35
本文介绍了抽象类 vs. 接口 vs. mixin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释一下抽象类接口混合之间的区别吗?我以前在我的代码中使用过每一个,但我不知道技术上的差异.

Could someone please explain to me the differences between abstract classes, interfaces, and mixins? I've used each before in my code but I don't know the technical differences.

推荐答案

抽象类

抽象类是一种不设计为实例化的类.抽象类可以没有实现、某些实现或全部实现.抽象类旨在允许其子类共享公共(默认)实现.抽象类的(伪编码)示例将是这样的

Abstract Class

An abstract class is a class that is not designed to be instantiated. Abstract classes can have no implementation, some implementation, or all implementation. Abstract classes are designed to allow its subclasses share a common (default) implementation. A (pseudocoded) example of an abstract class would be something like this

abstract class Shape {
    def abstract area();  // abstract (unimplemented method)
    def outline_width() = { return 1; }  // default implementation
}

子类可能看起来像

class Rectangle extends Shape {
    int height = width = 5;
    def override area() = { return height * width; }  // implements abstract method
    // no need to override outline_width(), but may do so if needed
}

可能的用途

def main() = {
    Shape[] shapes = { new Rectangle(), new Oval() };
    foreach (s in shapes) {
        print("area: " + s.area() + ", outline width: " + s.outline_width());
    }
}

如果子类不覆盖未实现的方法,它也是一个抽象类.

If a subclass does not override unimplemented methods, it is also an abstract class.

在一般的计算机科学术语中,接口是暴露给客户端的程序部分.公共类和成员是接口的示例.

In general computer science terms, an interface is the parts of a program exposed to a client. Public classes and members are examples of interfaces.

Java 和 C# 有一个特殊的 interface 关键字.这些或多或少是一个没有实现的抽象类.(关于常量、嵌套类、显式实现和访问修饰符的一些技巧,我不打算讨论.)虽然关于无实现"的部分没有涉及.不再适合 Java,他们添加了默认方法.interface 关键字可以看作是接口概念的具体化.

Java and C# have a special interface keyword. These are more or less an abstract class with no implementation. (There's trickiness about constants, nested classes, explicit implementation, and access modifiers that I'm not going to get into.) Though the part about "no implementation" doesn't fit any more in Java, they added default methods. The interface keyword can be seen as a reification of the interface concept.

回到形状示例

interface Shape {
    def area();  // implicitly abstract so no need for abstract keyword
    def outline_width();  // cannot implement any methods
}

class Rectangle implements Shape {
    int height = width = 5;
    def override area() = { return height * width; }
    def override outline_width() = { return 1; }  // every method in interface must be implemented
}

def main() = {
    Shape[] shapes = { new Rectangle(), new Oval() };
    foreach (s in shapes) {
        print("area: " + s.area() + ", outline width: " + s.outline_width());
    }
}

Java 和 C# 不允许具有实现的类的多重继承,但它们允许多个接口实现.Java 和 C# 使用接口作为解决Deadly Diamond of Death Problem 的方法,这些问题在以下语言中发现允许多重继承(如果处理得当,这并不是那么致命).

Java and C# do not allow multiple inheritance of classes with implementation, but they do allow multiple interface implementation. Java and C# use interfaces as a workaround to the Deadly Diamond of Death Problem found in languages that allow multiple inheritance (which isn't really that deadly, if properly handled).

mixin(有时称为特征)允许抽象类的多重继承.Mixin 没有多重继承所具有的可怕关联(由于 C++ 的疯狂),所以人们更愿意使用它们.它们具有完全相同的致命钻石死亡问题,但支持它们的语言比 C++ 具有更优雅的缓解方法,因此它们被认为更好.

A mixin (sometimes called a trait) allows multiple inheritance of abstract classes. Mixins don't have the scary association that multiple inheritance has (due to C++ craziness), so people are more comfortable using them. They have the same exact Deadly Diamond of Death Problem, but languages that support them have more elegant ways of mitigating it than C++ has, so they're perceived as better.

Mixin 被誉为具有行为重用更灵活 接口,以及更强大的接口.您会注意到所有这些都包含术语 interface,指的是 Java 和 C# 关键字.混合不是接口.它们是多重继承.取个好听的名字.

Mixins are hailed as interfaces with behavioral reuse, more flexible interfaces, and more powerful interfaces. You will notice all these have the term interface in them, referring to the Java and C# keyword. Mixins are not interfaces. They are multiple inheritance. With a prettier name.

这并不是说 mixin 不好.多重继承也不错.C++ 解决多重继承的方式是每个人都关心的问题.

关于疲惫的旧形状示例

mixin Shape {
    def abstract area();
    def outline_width() = { return 1; }
}

class Rectangle with Shape {
    int height = width = 5;
    def override area() = { return height * width; }
}

def main() = {
    Shape[] shapes = { new Rectangle(), new Oval() };
    foreach (s in shapes) {
        print("area: " + s.area() + ", outline width: " + s.outline_width());
    }
}

您会注意到这与抽象类示例之间没有区别.

You will notice there is no difference between this and the abstract class example.

一个额外的花絮是 C# 从 3.0 版开始支持 mixin.您可以使用接口上的扩展方法来实现.这是具有真实(!)C# 代码混合样式的 Shape 示例

One extra tidbit is that C# has supported mixins since version 3.0. You can do it with extension methods on interfaces. Here's the Shape example with real(!) C# code mixin style

interface Shape
{
    int Area();
}

static class ShapeExtensions
{
    public static int OutlineWidth(this Shape s)
    {
        return 1;
    }
}

class Rectangle : Shape
{
    int height = 5;
    int width = 5;

    public int Area()
    {
        return height * width;
    }
}

class Program
{
    static void Main()
    {
        Shape[] shapes = new Shape[]{ new Rectangle(), new Oval() };
        foreach (var s in shapes)
        {
            Console.Write("area: " + s.Area() + ", outline width: " + s.OutlineWidth());
        }
    }
}

这篇关于抽象类 vs. 接口 vs. mixin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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