我已经看到一些讨论为什么c#没有实现多重继承,但很少讨论为什么它不支持在vb中.我知道c#和vb都被编译成中间语言,所以他们都需要共享类似的限制.
VB中缺少多重继承似乎是网点缺乏功能的一个原因.有谁知道为什么VB不支持多重继承?我希望有一些历史课和讨论为什么从未考虑过VB.
为什么这有效:
template <typename T>
struct foo
{
};
struct A
{
typedef foo<A> type;
};
struct B : public A
{
typedef foo<B> type;
};
int main()
{
B::type john;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但不是这个:
template <typename T>
struct foo
{
};
template <typename T>
struct Shared
{
typedef foo<T> type;
};
struct A : public Shared<A>
{
};
struct B : public A, public Shared<B>
{
};
int main()
{
// g++ 4.5 says :
// error: reference to 'type' …Run Code Online (Sandbox Code Playgroud) 我可以从其他类导入方法而不使用它们的'extends'继承吗?
class Foo
{
public function fooMethod() {
return 'foo method';
}
}
class Too
{
public function tooMethod() {
return 'too method';
}
}
class Boo
{
public $foo;
public $too;
public function __construct()
{
$this->foo = new Foo();
$this->too = new Too();
}
}
Run Code Online (Sandbox Code Playgroud)
用法,
$boo = new Boo();
var_dump($boo->foo->fooMethod()); // string 'foo method' (length=10)
var_dump($boo->too->tooMethod()); // string 'too method' (length=10)
var_dump($boo->fooMethod()); // Fatal error: Call to undefined method Boo::fooMethod() in
var_dump($boo->tooMethod()); //Fatal error: Call to undefined method …Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的类结构:
class Question(object):
def answer(self):
return "Base Answer"
class ExclaimMixin(object):
def answer(self):
return "{}!".format(super(ExclaimMixin, self).answer())
class ExpressiveQuestion(Question, ExclaimMixin)
pass
Run Code Online (Sandbox Code Playgroud)
我希望answerin 方法ExclaimMixin能够answer在Question调用 in 时访问in ExpressiveQuestion,以便它返回"Base Answer!"。
显然,在这种情况下,这可以通过将answer方法放入inExclaimMixin中ExpressiveQuestion来解决,但在某些情况下这是不可能的(例如,在类结构中更深入和分支)。
是否可以使用mixin来实现这个结果,或者只能通过修改基类树来实现?
我在多重继承中遇到错误。由于我是Python新手,所以我不明白为什么我不能这样做。
class A(object):
def say_hello(self):
print 'A says hello'
class B(A):
def say_hello(self):
super(B, self).say_hello()
print 'B says hello'
class C(A):
def say_hello(self):
super(C, self).say_hello()
print 'C says hello'
class D(A, B):
def say_hello(self):
super(D, self).say_hello()
print 'D says hello'
DD = D()
DD.say_hello()
Run Code Online (Sandbox Code Playgroud)
我收到错误:- 无法创建一致的方法分辨率。为什么?
我有 B 类扩展 A 类。如何在 C 类中编写一个方法,该方法可以接收包含 B 类或 A 类对象的 ArrayList 而不覆盖?
public class A {
//Some methods here
}
public class B extends A {
//Some methods here
}
public class C {
public Static void main(String[] args){
ArrayList<A> one = new ArrayList<>();
one.add(new A());
ArrayList<B> two = new ArrayList<>();
two.add(new B())
doStuff(one);
doStuff(two);
}
public void doStuff(args){
//go ahead do stuff
}
}
Run Code Online (Sandbox Code Playgroud) class base
{
public:
virtual void display() = 0;
};
class derived : virtual public base
{
public:
void display()
{
cout << "Display of derived : " << std::endl;
}
};
class derived1 : virtual public base
{
public:
void display()
{
cout << "Display of derived : " << std::endl;
}
};
class derived2 : public derived, derived1
{
};
Run Code Online (Sandbox Code Playgroud)
我将一个纯虚函数放入基类中。我在创建从基类继承的派生类和派生类 1 时使用 virtual 关键字,最后创建了从派生类和派生类 1 继承的派生类 2,然后我会收到错误“派生类:base::void(display) 的继承不明确” “如何解决这个错误?
c++ overriding virtual-functions multiple-inheritance virtual-inheritance
我做了以下 3 个类:
struct Parent1
{
virtual void f()
{
cout << "\nParent1::f";
}
};
struct Parent2
{
virtual void g()
{
cout << "\nParent2::g";
}
virtual void z()
{
cout << "\nParent2::z";
}
};
struct Child : public Parent1, public Parent2
{
virtual void h()
{
cout << "\nChild::h";
}
};
Run Code Online (Sandbox Code Playgroud)
在 main 中,当我调用 的函数z时Parent2,它会调用h子类的函数。为什么会这样?以下是main函数:
int main()
{
Child obj;
Parent2 * p2 = (Parent2*)(Parent1*)&obj;
p2->z();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有以下课程:
class ServoPart {
protected:
virtual void doJob(byte* job) = 0;
private:
bool moving;
Servo servo;
};
// the following classes only have a constructor so I can use two ServoParts to inherit from
class Major: public ServoPart {};
class Minor: public ServoPart {};
class Arm: public Major, public Minor {
private:
void move() {
// do stuff
if (target == current) {
moving = false;
}
};
public:
void doJob(byte* job) {/* do stuff */};
};
Run Code Online (Sandbox Code Playgroud)
我不能使用虚拟继承(我认为),因为 Major 和 …
我需要实现几个模块,如下所示,
#include <stdio.h>
class Base{
protected:
int data;
};
class ModeA : public Base{};
class ModeB : public Base{};
class ModeHybrid: public ModeA, public ModeB{
public:
void func(){printf("%d\n", this->data);}
};
int main(){
ModeHybrid mh;
mh.func();
}
Run Code Online (Sandbox Code Playgroud)
在哪里,
但是,成员变量 'data' 将被复制,因为 'ModeHybrid' 继承自 'ModeA' 和 'ModeB'。
有什么办法可以避免这种情况吗?