问题标签 [abstract-base-class]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
1450 浏览

c++ - C++:如果类没有成员函数,如何创建抽象基类?

我有一个抽象基类,其目的是允许创建指向基类的指针数组。(对“很多事情”有用......)

我的抽象基类不包含任何成员函数。因此没有纯粹的虚方法,因此我猜它在技术上不是抽象的。

但是,我不希望能够创建此类的实例。

是否可以创建无成员抽象基类?如果没有,是否有另一种解决方案来阻止创建我的“抽象基础”的实例?制作构造函数protected就足够了吗?

有人向我指出,实际上,如果抽象基类的目的是允许创建指向基类的向量或指针数组,则不需要抽象基类——一个人可能根本没有基类,并且使用继承层次结构顶部的类作为基类。或者,可以复制并粘贴该顶级类并将已实现的方法交换为没有实现的纯虚函数,但这在逻辑上似乎与抽象基指针的想法不一致,并且会导致更难以维护代码。

0 投票
1 回答
48 浏览

python-2.7 - 如何隐式使用方法的基本定义

我目前正在为 python 2 开发,我正在尝试使用抽象基类来模拟接口。我有一个接口、该接口的基本实现以及许多扩展基本实现的子类。它看起来像这样:

但是 PyCharm 通知我ConcreteAudit应该实现所有抽象方法。但是,BaseAudit(未指定为 abc)已经实现了这些方法,并且ConcreteAuditBaseAudit. 为什么 PyCharm 会警告我?它不应该检测到IAudit的合同已经通过 执行了BaseAudit吗?

0 投票
1 回答
1099 浏览

python - issubclass of abstract base class Sequence

This list shows what methods you need to implement for your class to be "regarded" as Sequence: __getitem__, __len__, __contains__, __iter__, __reversed__, index, and count. So why does this minimal implementation does not work, i.e. why issubclass(S, Sequence) is False?

Is there an additional method I need to implement that I overlooked? Did I misunderstand abstract base classes? Subclassing Sequence makes issubclass return True of course, but that kinda defeats the idea behind abc, doesn't it?

0 投票
3 回答
11310 浏览

python - isinstance 与字典和 abc.Mapping 从集合做什么?

我正在运行的代码是:

我明白是什么isinstance,但我不确定abc.Mappingfrom是什么collections

好像这条线isinstance(mydict, abc.Mapping)被用来检查那mydict是一本字典?

这样做不是更容易 isinstance(mydict, dict)吗?

我进行了一些搜索并在此线程中找到了相关评论:检查 Python 变量类型的最佳(惯用)方法是什么?,但我仍然无法弄清楚为什么abc.Mapping在这里使用比仅使用更可取dict

0 投票
1 回答
1014 浏览

c++ - Do we have a better way of returning abstract classes in C++?

I want to start throwing some interfaces into my C++ code to make it easier for me to unit test using mocks.

The problem with this is returning abstract classes from a method in C++ is a pain. You can't return by value so you need to return a pointer or a reference.

Given all of the developments in C++ in the last six or seven years, I thought I'd ask if maybe we had a better way to return an abstract base class. An interface without the noise would look something like this, but I'm sure this isn't possible.

The way that I remember doing this in the past is to use a pointer (probably a smart pointer now):

The problem with the pointer is that I'm never actually planning to take advantage of nullptr so the overhead and noise of dealing with a pointer rather than a value gains me no value as a reader.

Is there a better way that I don't know to handle this?

0 投票
2 回答
73 浏览

c++ - 指向基类的指针数组:如何调用唯一的派生类方法

谷歌搜索了一段时间,但似乎无法找到明确的答案。

如何在以下示例中调用 unscramble() 方法?

谢谢。:^)

0 投票
1 回答
42 浏览

python - 在传递方法引用的情况下不会生成 AttributeError

我对 python 2.7 有一个简单的疑问:

我创建了一个抽象基类和一个子类:

当创建子类的对象并调用 send_method 时,我收到以下错误,这是预期的行为:

但是,当在基类中传递 send_method 引用并通过创建子类对象调用 send_method 时,我认为预期的行为是接收AttributeError但我很惊讶地发现没有生成错误。请解释。

0 投票
1 回答
1033 浏览

c# - 在基类中使用派生类对象

我有一个包含辅助方法的基类,并且我有一些包含一些虚拟方法的派生类。

所以,我想知道如何在基类虚拟方法中使用派生类对象?

派生类

基类

主要的

0 投票
1 回答
1416 浏览

python - 为什么要实现 __sub__ 和 __rsub__ 并以这种方式实现 numbers.Complex

我正在查看模块中的实现,Complexnumbers注意到__sub____rsub__的实现看起来像这样:

这让我很困惑。

首先,我不确定为什么要实现这些(猜测 can 的所有子类都Complex可以回退到它?),其次,我不明白他们为什么选择使用这样的一元-来实现它。

有任何想法吗?

0 投票
1 回答
781 浏览

c# - 使用 Microsoft.fakes 从派生类公共方法模拟调用基抽象类的受保护方法

我正在尝试使用Microsoft.Fakes为下面显示的代码编写单元测试。

作为 Microsoft.Fakes 的新手,我在模拟对抽象基类保护功能方法的调用时遇到了困难

我的基类:

我的孩子班:

我尝试使用以下单元测试代码进行模拟,但没有奏效:

注意:我的抽象基类受保护方法执行复杂的操作,所以我需要模拟它。上面的代码只是一个示例。

任何指针将不胜感激!