我有3个班,A,B,和C那里B是A和C是A.
这些类旨在保留历史记录,因此A提供了一个虚拟方法,派生类可以创建该方法,以便在存档历史记录时进行通知void archive().
现在突然有一个需要有从信息A,B以及C在一个类中,我不知道最好的方法.
我想到了创建一个新的类D从继承B和C,并改变他们的继承virtual public A,避免钻石的问题,并D::archive()简单地调用B::archive()和C::archive().
这是一个好方法吗?或者我应该重新设计4个类,以便我不使用多重继承?
我遇到一个问题,我想使用 的 mixin 方法collections.abc.MutableSequence,但我还必须从其他方法继承。
class Thing(urwid.Pile, collections.abc.MutableSequence):
...
Run Code Online (Sandbox Code Playgroud)
我最终得到
TypeError: metaclass conflict: the metaclass of a derived class must be
a (non-strict) subclass of the metaclasses of all its bases
Run Code Online (Sandbox Code Playgroud)
我如何确定发生了什么并解决它?metaclass = ABCMeta没有达到目的,无论其价值如何。
class Employee(object):
def __init__(self,ename,salary,dateOfJoining):
self.ename=ename
self.salary=salary
self.dateOfJoining=dateOfJoining
def output(self):
print("Ename: ",self.ename,"\nSalary: ",self.salary,"\nDate Of
Joining: ",self.dateOfJoining)
class Qualification(object):
def __init__(self,university,degree,passingYear):
self.university=university
self.degree=degree
self.passingYear=passingYear
def qoutput(self):
print("Passed out fom University:",self.university,"\nDegree:",self.degree,"\Passout year: ",self.passingYear)
class Scientist(Employee,Qualification):
def __int__(self,ename,salary,dateOfJoining,university,degree,passingYear):
Employee.__init__(self,ename,salary,dateOfJoining)
Qualification.__init__(self,university,degree,passingYear)
def soutput(self):
Employee.output()
Qualification.output()
a=Scientist('Ayush',20000,'21-04-2010','MIT','B.Tech','31-3-2008')
a.soutput()
Run Code Online (Sandbox Code Playgroud)
我无法找到问题的解决方案,也无法理解为什么会发生此 TpyeError。我是python的新手。谢谢
我在在线测试中遇到了以下c ++代码.
#include <iostream>
class A
{
public:
A(int n = 2) : m_n(n) {}
public:
int get_n() const { return m_n; }
void set_n(int n) { m_n = n; }
private:
int m_n;
};
class B
{
public:
B(char c = 'a') : m_c(c) {}
public:
char get_c() const { return m_c; }
void set_c(char c) { m_c = c; }
private:
char m_c;
};
class C
: virtual public A
, public B
{ };
class D
: …Run Code Online (Sandbox Code Playgroud) 我目前对这个问题有点困惑.我注意到ArrayList是一个实现List的类,后者又扩展了Collection.
在Collection<?>我们有一堆方法,一个可识别的方法containsAll().我查看了官方文档,ArrayList但可以看出,Array列表中没有这样的方法.在页面的页脚中写了一些东西,说"继承的方法",containsAll()并在那里提到.
我不明白(从文档中)是,containsAll()即使它在ArrayList类中有空体还是根本没有定义?如果不是这种违反Java规则的行为?
还有其他人以同样的方式"失踪"吗?!
只是一个快速愚蠢的问题:
我想知道C++是否在内部利用了虚拟继承提供的多重继承功能.我确信,对于模板化实现,例如STL提供的数据结构(例如std :: vector <>)不能使用这样的功能,但是如某些其他对象如流如何.
我知道多重继承在某些情况下可能很方便(例如,对于交叉委托)但我觉得如果C++开发人员决定避免在代码中使用这样的功能,我应该避免使用它.
感谢大家!
在阅读有关Java中的多重继承或钻石问题时,我意识到它不受支持。但是我想知道Java实际上如何限制多重继承?
是否有任何类可以检查程序员是否在extends关键字之后传递了一个以上的类名或其他方法来检测此功能?
我读了几篇文章,但没有什么建议暗示Java如何确切地防止多重继承,除了一个常见的错误提示:类名继承了多个类。
我C2259实例化从其他类继承的类时出现编译器错误,这些类具有抽象方法。
继承方案有点怪异和不透明,但由于问题的某些限制,我需要以这种方式进行操作。
继承方案如下:
class A
{
public:
enum Animal { CAT, DOG };
enum Color { RED, GREEN };
enum Food { MEAT, FISH };
protected:
virtual Animal animal() const = 0;
virtual Color color() const = 0;
virtual Food food() const = 0;
};
class B: public A
{
Animal animal() const { return CAT; }
};
class C: public A
{
Color color() const { return GREEN; }
};
class D: public A
{
Food food() …Run Code Online (Sandbox Code Playgroud) c++ inheritance abstract-class virtual-functions multiple-inheritance
我在c#中创建一个表单,所以任何新表单都继承了类中构建的属性From我还创建了一个名为program的类,它启动了与我的数据库的连接我希望我的所有表单都继承它,因为我不想打开它每个表单的新连接我不能使用接口,因为程序包含main(),它创建一个连接我的程序类是
class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
MySqlConnection myConnection = new MySqlConnection("Server=instance2813231.db.xeround.com;Port=18422;Database=shares info;Uid=user;Pwd=password;");
MySqlCommand command = myConnection.CreateCommand();
try
{
myConnection.Open();
MessageBox.Show("connected");
}
catch (Exception a)
{
Console.WriteLine(a.ToString());
MessageBox.Show("Failed");
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么能绕过这个?
这个程序似乎在使用多重继承.但是Java不支持多重继承,那么这个程序是如何工作的呢?
这是什么代码背后的原因在编译的时候两个类Student和Professor继承的基类Person?
class Person
{
private String name;
private int age;
private int val;
private int value(int sal)
{
return sal/100;
}
public Person(String name, int age, int sal)
{
this.name = name;
this.age = age;
val = value(sal);
}
public void valOverride(int val)
{
this.val = val;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public int getVal()
{
return val;
}
}
class Student extends …Run Code Online (Sandbox Code Playgroud)