C#支持多重继承吗?

发布于 2024-08-25 04:23:12 字数 57 浏览 6 评论 0原文

我和一位同事就多重继承发生了一些争论。我说不支持,他说支持。所以我想还是去问问网上那些有头脑的人吧。

A colleague and I are having a bit of an argument over multiple inheritance. I'm saying it's not supported and he's saying it is. So, I thought that I'd ask the brainy bunch on the net.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

放血 2024-09-01 04:23:12

抱歉,您不能从多个类继承。您可以使用接口或一个类和接口的组合,其中接口应遵循签名中的类名称。

interface A { }
interface B { }
class Base { }
class AnotherClass { }

可能的继承方式:

class SomeClass : A, B { } // from multiple Interface(s)
class SomeClass : Base, B { } // from one Class and Interface(s)

这是不合法的:

class SomeClass : Base, AnotherClass { }

Sorry, you cannot inherit from multiple classes. You may use interfaces or a combination of one class and interface(s), where interface(s) should follow the class name in the signature.

interface A { }
interface B { }
class Base { }
class AnotherClass { }

Possible ways to inherit:

class SomeClass : A, B { } // from multiple Interface(s)
class SomeClass : Base, B { } // from one Class and Interface(s)

This is not legal:

class SomeClass : Base, AnotherClass { }
过期情话 2024-09-01 04:23:12

不,请改用接口! ^.^

Nope, use interfaces instead! ^.^

小嗷兮 2024-09-01 04:23:12

C# 不支持多重继承。

但是,如果您想从两个来源“继承”行为,为什么不使用以下组合:

  • 组合
  • 依赖注入

有一个基本但重要的 OOP 原则:“优先考虑组合而不是继承”。

您可以创建这样的类:

public class MySuperClass
{
    private IDependencyClass1 mDependency1;
    private IDependencyClass2 mDependency2;

    public MySuperClass(IDependencyClass1 dep1, IDependencyClass2 dep2)
    {
        mDependency1 = dep1;
        mDependency2 = dep2;
    }

    private void MySuperMethodThatDoesSomethingComplex()
    {
        string s = mDependency1.GetMessage();
        mDependency2.PrintMessage(s);
    }
}

正如您所看到的,依赖项(接口的实际实现)是通过构造函数注入的。您的类不知道每个类是如何实现的,但它知道如何使用它们。因此,这里涉及的类之间是松散耦合的,但使用能力相同。

如今的趋势表明,继承已经“过时”了。

Multiple inheritance is not supported in C#.

But if you want to "inherit" behavior from two sources why not use the combination of:

  • Composition
  • Dependency Injection

There is a basic but important OOP principle that says: "Favor composition over inheritance".

You can create a class like this:

public class MySuperClass
{
    private IDependencyClass1 mDependency1;
    private IDependencyClass2 mDependency2;

    public MySuperClass(IDependencyClass1 dep1, IDependencyClass2 dep2)
    {
        mDependency1 = dep1;
        mDependency2 = dep2;
    }

    private void MySuperMethodThatDoesSomethingComplex()
    {
        string s = mDependency1.GetMessage();
        mDependency2.PrintMessage(s);
    }
}

As you can see the dependecies (actual implementations of the interfaces) are injected via the constructor. You class does not know how each class is implemented but it knows how to use them. Hence, a loose coupling between the classes involved here but the same power of usage.

Today's trends show that inheritance is kind of "out of fashion".

等风来 2024-09-01 04:23:12

C# 3.5 或更低版本不支持多重继承,但 C# 4.0 可以通过使用 Dynamics 来做到这一点。

C# 3.5 or below does not support the multiple inheritance, but C# 4.0 could do this by using, as I remembered, Dynamics.

乖不如嘢 2024-09-01 04:23:12

在 C# 3.5 之前,您无法执行多重继承。我不知道它在 4.0 上的表现如何,因为我还没有看过它,但 @tbischel 发布了一个我需要阅读的链接。

C# 允许您通过接口进行“多重实现”,这与“多重继承”完全不同

所以,您不能这样做:

class A{}

class B{}

class C : A, B{}

但是,您可以这样做:

interface IA{}
interface IB{}

class C : IA, IB{}

HTH

You cannot do multiple inheritance in C# till 3.5. I dont know how it works out on 4.0 since I have not looked at it, but @tbischel has posted a link which I need to read.

C# allows you to do "multiple-implementations" via interfaces which is quite different to "multiple-inheritance"

So, you cannot do:

class A{}

class B{}

class C : A, B{}

But, you can do:

interface IA{}
interface IB{}

class C : IA, IB{}

HTH

回首观望 2024-09-01 04:23:12

C# 不支持类的多重继承,但允许您继承/实现任意数量的接口。

这是非法的
(B、C、D 和 E 都是类别)

class A : B, C, D, E
{
}

这是合法的
(IB、IC、ID 和 IE 都是接口)

class A : IB, IC, ID, IE
{
}

这是合法的
(B 是类,IC、ID 和 IE 是接口)

class A : B, IC, ID, IE
{
}

组合优于继承 是一种设计模式,即使在支持多重继承的语言中似乎也是受欢迎的。

C# does not support multiple inheritance of classes, but you are permitted to inherit/implement any number of interfaces.

This is illegal
(B, C, D & E are all classes)

class A : B, C, D, E
{
}

This is legal
(IB, IC, ID & IE are all interfaces)

class A : IB, IC, ID, IE
{
}

This is legal
(B is a class, IC, ID & IE are interfaces)

class A : B, IC, ID, IE
{
}

Composition over inheritance is a design pattern that seems to be favorable even in languages that support multiple inheritance.

呆萌少年 2024-09-01 04:23:12

实际上,这取决于您对继承的定义:

  • 您可以从单个类继承实现(成员,即数据和行为),但
  • 您可以继承接口< /em> 来自多个,嗯,接口。

这不是“继承”一词通常的含义,但这样定义也并非完全没有道理。

Actually, it depends on your definition of inheritance:

  • you can inherit implementation (members, i.e. data and behavior) from a single class, but
  • you can inherit interfaces from multiple, well, interfaces.

This is not what is usually meant by the term "inheritance", but it is also not entirely unreasonable to define it this way.

云雾 2024-09-01 04:23:12

与 Java(C# 间接派生自 Java)一样,C# 不支持多重继承。

也就是说,类data(成员变量和属性)只能从单个父基继承。另一方面,类行为(成员方法)可以从多个父基接口继承。

一些专家,尤其是 Bertrand Meyer(被一些人认为是面向对象的鼻祖之一)编程),认为这使 C#(和 Java 以及所有其他语言)失去了成为“真正的”面向对象语言的资格。

Like Java (which is what C# was indirectly derived from), C# does not support multiple inhertance.

Which is to say that class data (member variables and properties) can only be inherited from a single parent base class. Class behavior (member methods), on the other hand, can be inherited from multiple parent base interfaces.

Some experts, notably Bertrand Meyer (considered by some to be one of the fathers of object-oreiented programming), think that this disqualifies C# (and Java, and all the rest) from being a "true" object-oriented language.

苹果你个爱泡泡 2024-09-01 04:23:12

您可能想更进一步讨论设计模式 - 您可以找出为什么他愿意费心尝试从 C# 中的多个类继承(如果他可以的话)

You may want to take your argument a step further and talk about design patterns - and you can find out why he'd want to bother trying to inherit from multiple classes in c# if he even could

对岸观火 2024-09-01 04:23:12

多重继承允许程序员创建结合多个类及其相应层次结构的各个方面的类。对于前。 C++ 允许从多个类继承

在 C# 中,类只允许从单个父类继承,这称为单继承。但是您可以使用接口或一个类和接口的组合,其中接口应该在签名中跟上类名。

例如:

Class FirstClass { }
Class SecondClass { }

interface X { }
interface Y { }

您可以像下面这样继承:

class NewClass : X, Y { }
在上面的代码中,类“NewClass”是从多个接口创建的。

类 NewClass : FirstClass, X { }
在上面的代码中,类“NewClass”是从接口 X 和类“FirstClass”创建的。

Multiple inheritance allows programmers to create classes that combine aspects of multiple classes and their corresponding hierarchies. For ex. the C++ allows you to inherit from more than one class

In C#, the classes are only allowed to inherit from a single parent class, which is called single inheritance. But you can use interfaces or a combination of one class and interface(s), where interface(s) should be followed by class name in the signature.

Ex:

Class FirstClass { }
Class SecondClass { }

interface X { }
interface Y { }

You can inherit like the following:

class NewClass : X, Y { }
In the above code, the class "NewClass" is created from multiple interfaces.

class NewClass : FirstClass, X { }
In the above code, the class "NewClass" is created from interface X and class "FirstClass".

伊面 2024-09-01 04:23:12

一般来说,您做不到。

考虑这些接口和类:

public class A { }    
public class B { }
public class C { }

public interface IA { }
public interface IB { }

您可以继承多个接口:

class A : B, IA, IB {
  // Inherits any single base class, plus multiple interfaces.
}

但是您不能继承多个

class A : B, C, IA, IB {
    // Inherits multiple base classes, plus multiple interfaces.
}

In generally, you can’t do it.

Consider these interfaces and classes:

public class A { }    
public class B { }
public class C { }

public interface IA { }
public interface IB { }

You can inherit multiple interfaces:

class A : B, IA, IB {
  // Inherits any single base class, plus multiple interfaces.
}

But you can’t inherit multiple classes:

class A : B, C, IA, IB {
    // Inherits multiple base classes, plus multiple interfaces.
}
一抹淡然 2024-09-01 04:23:12

您不能一次继承多个类。但有一个选项可以通过界面的帮助来做到这一点。请参阅下面的代码,

interface IA
{
    void PrintIA();
}

class  A:IA
{
    public void PrintIA()
    {
        Console.WriteLine("PrintA method in Base Class A");
    }
}

interface IB
{
    void PrintIB();
}

class B : IB
{
    public void PrintIB()
    {
        Console.WriteLine("PrintB method in Base Class B");
    }
}

public class AB: IA, IB
{
    A a = new A();
    B b = new B();

    public void PrintIA()
    {
       a.PrintIA();
    }

    public void PrintIB()
    {
        b.PrintIB();
    }
}

您可以按如下方式调用它们

AB ab = new AB();
ab.PrintIA();
ab.PrintIB();

You can't inherit multiple classes at a time. But there is an options to do that by the help of interface. See below code

interface IA
{
    void PrintIA();
}

class  A:IA
{
    public void PrintIA()
    {
        Console.WriteLine("PrintA method in Base Class A");
    }
}

interface IB
{
    void PrintIB();
}

class B : IB
{
    public void PrintIB()
    {
        Console.WriteLine("PrintB method in Base Class B");
    }
}

public class AB: IA, IB
{
    A a = new A();
    B b = new B();

    public void PrintIA()
    {
       a.PrintIA();
    }

    public void PrintIB()
    {
        b.PrintIB();
    }
}

you can call them as below

AB ab = new AB();
ab.PrintIA();
ab.PrintIB();
北座城市 2024-09-01 04:23:12

不允许的话,就用接口来实现。

为什么会这样?

答案如下:它允许编译器做出非常合理且合理的决定,该决定始终与继承的内容和继承的位置保持一致,以便当您执行此操作时铸造,你总是确切地知道你正在处理哪个实现。

It does not allow it, use interface to achieve it.

Why it is so?

Here is the answer: It's allowed the compiler to make a very reasoned and rational decision that was always going to consistent about what was inherited and where it was inherited from so that when you did casting and you always know exactly which implementation you were dealing with.

独夜无伴 2024-09-01 04:23:12

C# 不支持内置的多重继承。

要为不支持的语言添加多重继承,可以使用 孪生设计模式

C# does not support multiple inheritance built in.

For adding multiple inheritance to languages that does not support it, You can use twin design pattern

無處可尋 2024-09-01 04:23:12

作为对所建议内容的附加建议,提供类似于多重继承的功能的另一种巧妙方法是实现多个接口,但然后在这些接口上提供扩展方法。这称为 混入。这不是一个真正的解决方案,但它有时可以处理提示您想要执行多重继承的问题。

As an additional suggestion to what has been suggested, another clever way to provide functionality similar to multiple inheritance is implement multiple interfaces BUT then to provide extension methods on these interfaces. This is called mixins. It's not a real solution but it sometimes handles the issues that would prompt you to want to perform multiple inheritance.

树深时见影 2024-09-01 04:23:12

我最近以某种方式达到了相同的心态,将两个类继承到一个类中,并最终出现在这个页面上(即使我知道得更好),并且希望保留对我在这种情况下发现的完美解决方案的引用,而不强制执行接口

我的这个问题的解决方案是将您的数据分成有意义的类:

public class PersonAddressSuper
{
    public PersonBase Person { get; set; }
    public PersonAddress Address { get; set; }

    public class PersonBase
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
    public class PersonAddress
    {
        public string StreetAddress { get; set; }
        public string City { get; set; }
    }
}

稍后在您的代码中,您可以像这样使用它:

包括两个部分,基础和基础部分。 仅地址

PersonAddressSuper PersonSuper = new PersonAddressSuper();
PersonSuper.PersonAddress.StreetAddress = "PigBenis Road 16";

基址:

PersonAddressSuper.PersonBase PersonBase = new PersonAddressSuper.PersonBase();
PersonBase.Name = "Joe Shmoe";

仅地址:

PersonAddressSuper.PersonAddress PersonAddress = new PersonAddressSuper.PersonAddress();
PersonAddress.StreetAddress = "PigBenis Road 16";

I recently somehow got to the same mindset, inheriting two classes into a class and ended up on this page (even though i know better) and would like to keep reference to the solution i found perfect in this scenario, without enforcing implementation of interfaces

My solution to this problem would be splitting up your data into classes that make sense:

public class PersonAddressSuper
{
    public PersonBase Person { get; set; }
    public PersonAddress Address { get; set; }

    public class PersonBase
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
    public class PersonAddress
    {
        public string StreetAddress { get; set; }
        public string City { get; set; }
    }
}

Later on in your code, you could use it like this:

Include Both Parts, Base & Address

PersonAddressSuper PersonSuper = new PersonAddressSuper();
PersonSuper.PersonAddress.StreetAddress = "PigBenis Road 16";

Base Only:

PersonAddressSuper.PersonBase PersonBase = new PersonAddressSuper.PersonBase();
PersonBase.Name = "Joe Shmoe";

Address Only:

PersonAddressSuper.PersonAddress PersonAddress = new PersonAddressSuper.PersonAddress();
PersonAddress.StreetAddress = "PigBenis Road 16";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文