我有一种情况,我认为我需要接口和抽象类提供的功能.
我正在创建模拟中使用的模型.每个模型都包含在一个C#类中,该类实现了一个IModel接口(我的设计是可以修改的).我还创建了一个c ++非托管Win32 DLL,它充当我的C#模型类和模拟环境之间的桥梁.模型类通过COM调用,因此需要接口.
我刚收到一个新的要求,即这些模型类应该可以通过Web服务进行调用,而与模拟环境无关.现在这些模型相当敏感,客户端已请求由客户端配置并在服务器上执行*.我认为如果我使模型类继承自抽象类,然后在服务器上创建一个Web服务方法,这将是可能的:
public AbstractModel executeModel(AbstractModel modelForExecution)
Run Code Online (Sandbox Code Playgroud)
从客户端接收模型对象,执行它并用结果填充它.
但是 - 我不能使用抽象类,因为模型需要COM实现的接口.
我如何协调这两个要求?我被告知可以让一个类实现一个接口并让该类派生自一个抽象类,但我找不到任何这样的例子.如果可能,语法是什么?如果不可能,我该怎么办?
*模型类实际上调用了古老的fortran可执行文件,因此类的模型配置并没有提供有关模型如何工作的太多信息.
可能是最着名的软件开发人员面试问题之一.
你的答案是什么?
编辑:我想知道如何在现实生活中回答这个问题.请尝试像在实际面试时一样制定答案(完整,但不要太长,当然不发布任何链接).
根据OOP,需要抽象类来模拟那些在现实世界中不存在的对象,但它们可以作为几个真实世界对象的基类.
例如:
BankAccount
/\
/ \
/ \
/ \
Current Savings
Account Account
Run Code Online (Sandbox Code Playgroud)
这里BankAccount应该被建模为抽象类.
但是在C#/ Java中使用抽象类的技术原因是什么?链接文字
例如:
使用Interfaces的OOP原因是对行为继承进行建模(继承没有真正的层次关系).
在C#/ Java中使用Interfaces的技术原因是为了解决多重继承的问题(如果我没有错!).
我有一个抽象的基类,其中包含一个私有嵌套实现.当我尝试实例化非抽象嵌套实现时,visual c ++给出了以下错误:
错误C2259:'node :: empty_node':无法实例化抽象类(第32行)
据我所知,我已经覆盖了基类的所有抽象成员
代码如下:
using namespace boost;
template<typename K, typename V>
class node {
protected:
class empty_node : public node<K,V> {
public:
bool is_empty(){ return true; }
const shared_ptr<K> key() const { throw empty_node_exception; }
const shared_ptr<V> value() const { throw empty_node_exception; }
const shared_ptr<node<K,V>> left() const { throw empty_node_exception; }
const shared_ptr<node<K,V>> right() const { throw empty_node_exception; }
const shared_ptr<node<K,V>> add(const shared_ptr<K> &key, const shared_ptr<V> &value) const {
return shared_ptr<node<K,V>>();
}
const shared_ptr<node<K,V>> remove(const shared_ptr<K> …Run Code Online (Sandbox Code Playgroud) 我必须在我的大学进行基础C++讲座,所以要明确一点:如果我被允许,我会使用STL.
问题:我有一个名为"shape3d"的类,我从中派生出类"cube"和"sphere".现在我必须实现"shape3d_stack",这意味着能够保存"cube"和"sphere"类型的对象.我使用了数组,当我尝试使用一堆int时,它运行得很好.我试着这样做:
shape3d_stack.cpp:
15 // more stuff
16
17 shape3d_stack::shape3d_stack (unsigned size) :
18 array_ (NULL),
19 count_ (0),
20 size_ (size)
21 { array_ = new shape3d[size]; }
22
23 // more stuff
Run Code Online (Sandbox Code Playgroud)
但是,不幸的是,编译器告诉我:
g++ -Wall -O2 -pedantic -I../../UnitTest++/src/ -c shape3d_stack.cpp -o shape3d_stack.o
shape3d_stack.cpp: In constructor ‘shape3d_stack::shape3d_stack(unsigned int)’:
shape3d_stack.cpp:21: error: cannot allocate an object of abstract type ‘shape3d’
shape3d.hpp:10: note: because the following virtual functions are pure within ‘shape3d’:
shape3d.hpp:16: note: virtual double shape3d::area() const
shape3d.hpp:17: note: virtual double …Run Code Online (Sandbox Code Playgroud) 在下面的父类SqlStatement中,如何使Initialize() 抽象但保持Execute() 虚拟?
using System;
using System.Collections.Generic;
namespace TestSql28374
{
class Program
{
static void Main(string[] args)
{
object item = new object();
List<string> properties = new List<string>();
SqlCreateStatement sqlCreateStatement = new SqlCreateStatement(properties);
sqlCreateStatement.Execute();
SqlInsertStatement sqlInsertStatement = new SqlInsertStatement(item, properties);
sqlInsertStatement.Execute();
Console.ReadLine();
}
}
public class SqlStatement
{
protected List<string> properties;
protected object item;
protected string sql;
public SqlStatement(List<string> properties)
{
this.properties = properties;
}
protected virtual void Initialize() //should be abstract
{ } …Run Code Online (Sandbox Code Playgroud) 在标题中,我正在定义bool isActive.在从这个派生的类中,我想默认使isActive为false.我尝试通过添加来做到这一点
AbstractClass::isActive = false;
Run Code Online (Sandbox Code Playgroud)
到cpp文件,但这会导致错误"预期的构造函数,析构函数或'='标记之前的类型转换."
我最近重新编写了一个自己的库来尝试将接口与实现分离.我在最后一个问题上遇到了一个用于返回另一个类的实例的类.
在接口定义中,我做了类似的事情
struct IFoo
{
virtual const IBar& getBar() = 0;
}
Run Code Online (Sandbox Code Playgroud)
然后在具体的Foo getBar看起来像
const IBar& Foo::getBar()
{
Bar ret = Bar();
return ret;
}
Run Code Online (Sandbox Code Playgroud)
问题是,一旦getBar完成就会删除ret,当复制构造函数试图像这样使用Bar时会导致严重崩溃
const Bar myBar = myFoo.getBar();
Run Code Online (Sandbox Code Playgroud)
我一直在阅读各种各样的东西,我知道通过引用返回是不受欢迎的,但我没有看到任何其他方式(我不想返回Bar*因为我不想手动删除返回值).
抽象类返回从另一个抽象类派生的具体类的实例的正确方法(如果存在任何方式)是什么?
注意我确实看到了这个解决方案:从函数返回一个抽象类, 但我不想让返回值静态和松散线程安全.
我遇到了一个具有挑战性的问题,这个问题一直无法解决 - 希望到现在为止.我正在开发自己的框架,因此试图为用户提供所有代码复杂性的灵活性.
首先,我有一个用户可以实现的抽象基类,显然简化了:
class IStateTransit
{
public:
bool ConnectionPossible(void) = 0;
}
// A user defines their own class like so
class MyStateTransit : public IStateTransit
{
public:
bool ConnectionPossible(void){ return true; }
}
Run Code Online (Sandbox Code Playgroud)
接下来,我定义一个工厂类.用户可以注册自己的自定义状态传输对象,稍后通过使用他们选择的字符串标识符来引用它们:
class TransitFactory : public Singleton<TransitFactory>
{
public:
template<typename T> void RegisterStateTransit(const string& name)
{
// If the transit type is not already registered, add it.
if(transits.find(name) == transits.end())
{
transits.insert(pair<string, IStateTransit*>(name, new T()));
};
}
IStateTransit* TransitFactory::GetStateTransit(const string& type) const
{
return transits.find(type)->second;
}; …Run Code Online (Sandbox Code Playgroud) 从过去两天开始,我一直在深入研究PHP中的OOPS概念,我发现抽象类是非常有用的概念,我想在我的应用程序中实现它.这就是为什么我要实现它.
我的应用程序由几个非结构化模式组成,它使用几个不同的类而没有任何层次结构,因此我必须使用几个重复的代码.我想切断这一切并正确地构建它,基本上我想做的是
基本上,我的Abstract类应该能够继承可重用的方法和属性,同时它应该充当接口.
为了向您展示,请让我向您展示一些我一直在使用的示例代码.
public $error = array(); //used to handle error mechanism and to hold the errors.
private $data = array(); //used with Accessor Methods (__set and __get)
private $validate; //holds Validate Object
private $dbh; //holds Database Object
public function __set($property, $value) {
if( in_array($property, $this->data)) {
return $this->data[$property] = $value;
} else {
return false;
}
}
public function __get($property) {
return 'Access Denied to Class Property [ '.$property.' ]';
}
Run Code Online (Sandbox Code Playgroud)
几乎每个类都重复上面的代码,这就是我想在父类中定义一次并从那里控制机制的原因.
由于我仍然是许多OOP概念的新手,我无法理解如何使用Abstract Class实现我想要的东西.下面是我尝试使用的示例代码,其中使用哪种方法来声明抽象方法.
abstract …Run Code Online (Sandbox Code Playgroud)