是否有可能延长2班? [英] Is it possible to extends 2 classes?
问题描述
我有这些类:
public class myClassPage : System.Web.UI.Page
{
public myClassPage ()
{
}
}
public class myClassControl : System.Web.UI.UserControl
{
public myClassControl ()
{
}
}
和我想有一个扩展这些类,像出头另一个类:
and I'd like have another Class that extends these classes, somethings like :
public class myClassData : myClassPage, myClassControl
{
public myClassData ()
{
}
}
这可能吗?或者,我可以做什么?
is it possible? Or what can I do?
推荐答案
在你需要扩展两个类的话,你可能会被提供给了青睐组成继承和其他的答案也提到使用接口。举个例子:
In the case where you need to extend two classes, you might be served to favor composition over inheritance, and to use interfaces as other answers have mentioned. An example:
开始由definining的接口
Start by definining your interfaces
interface IFoo
{
void A();
}
interface IBar
{
void B();
}
然后创建实现每个接口的具体类
Then create concrete classes that implement each interface
class Foo : IFoo
{
public void A()
{
// code
}
}
class Bar : IBar
{
public void B()
{
// code
}
}
最后,在类,你要表现出这两套行为,可以实现。每一个接口,但是撰写的实现与具体类
Finally, in the class you want to exhibit both sets of behaviors, you can implement each interface but compose the implementations with the concrete classes.
public class Baz : IFoo, IBar
{
IFoo foo = new Foo(); // or inject
IBar bar = new Bar(); // or inject
public void A()
{
foo.A();
}
public void B()
{
bar.B();
}
}
这篇关于是否有可能延长2班?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!