gpt4 book ai didi

oop - 抽象类、接口(interface)、mixins

转载 作者:行者123 更新时间:2023-12-03 05:29:12 24 4
gpt4 key购买 nike

有人可以向我解释一下抽象类接口(interface)mixins之间的区别吗?我之前在代码中使用过它们,但我不知道技术差异。

最佳答案

抽象类

抽象类是不被设计为实例化的类。抽象类可以没有实现、部分实现或全部实现。抽象类旨在允许其子类共享公共(public)(默认)实现。抽象类的(伪编码)示例如下所示

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());
}
}

如果子类没有重写未实现的方法,那么它也是一个抽象类。

界面

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

Java 和 C# 有一个特殊的interface 关键字。这些或多或少是一个没有实现的抽象类。 (常量、嵌套类、显式实现和访问修饰符有些棘手,我不打算深入讨论。)尽管“无实现”部分不再适合 Java,但他们添加了默认方法。 interface 关键字可以看作是接口(interface)概念的具体化。

回到形状示例

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# 不允许具有实现的类的多重继承,但它们允许多个接口(interface)实现。 Java 和 C# 使用接口(interface)作为 Deadly Diamond of Death Problem 的解决方法在允许多重继承的语言中发现了这种情况(如果处理得当,这并不是那么致命)。

混合

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

Mixin 被誉为 behavioral reuse 的接口(interface), more flexible接口(interface),以及 more powerful接口(interface)。您会注意到所有这些都包含术语interface,指的是Java 和C# 关键字。 Mixin 不是接口(interface)。它们是多重继承。有一个更漂亮的名字。

这并不是说 mixin 不好。多重继承并不坏。 C++ 解决多重继承的方式是每个人都热衷的问题。

关于陈旧的 Shape 示例

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());
}
}

您会发现这与抽象类示例没有区别。

一个额外的消息是,C# 从 3.0 版本开始就支持 mixin。您可以使用接口(interface)上的扩展方法来完成此操作。这是具有真实(!)C# 代码 mixin 风格的 Shape 示例

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());
}
}
}

关于oop - 抽象类、接口(interface)、mixins,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/918380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com