你可以在C#中继承多少个类?

时间:2022-09-24 23:29:59

How many classes can you inherit from in .NET?

你可以在.NET中继承多少个类?

There are several backend C# files that I would like to share separate static methods, but they are all in different classes. Is there a way to inherit multiple classes?

有几个后端C#文件,我想分享不同的静态方法,但它们都在不同的类中。有没有办法继承多个类?

Example code would be much appreciated.

示例代码将非常感激。

8 个解决方案

#1


32  

C# does not support multiple inheritance (meaning a single class inherits from multiple classes). You can, however, implement multiple interfaces in a single class.

C#不支持多重继承(意味着单个类继承自多个类)。但是,您可以在单个类中实现多个接口。

As far as chaining together inherited classes, there isn't a limit per-se. Just keep in mind the complexity you will introduce to your system. When using inheritance, make sure you are using it in a "is a" scenario. A cheeta is an animal. A mazda is a car. Otherwise, your inheritance tightly couples your classes with a design that becomes much more difficult to maintain.

就将继承的类链接在一起而言,本身没有限制。请记住您将为系统引入的复杂性。使用继承时,请确保在“是”方案中使用它。 cheeta是一种动物。马自达是一辆汽车。否则,您的继承会将您的类与设计变得更加难以维护。

If they are static "utility" methods, just invoke them directly without inheriting. Unless those methods belong to the entity you are creating, you should not use inheritance.

如果它们是静态“实用程序”方法,则只需直接调用它们而不继承。除非这些方法属于您正在创建的实体,否则不应使用继承。

#2


15  

You can only inherit from a single class. It is however possible to implement multiple interfaces.

您只能从单个类继承。然而,可以实现多个接口。

#3


9  

Let's say you have Utils.cs:

假设你有Utils.cs:

internal class Utils {
    public static void Log(string str) {
        // log str to a file...
    }
}

and there is a DB.cs that uses Log method and in the same namespace with Utils class:

并且有一个DB.cs使用Log方法,并在与Utils类相同的命名空间中:

public class DB {
    public void SaveToDB() {
        // some db operations
        Utils.Log("DB operation successful");
    }
}

Since Log method is static, you don't need to initialize Utils class to use this method.

由于Log方法是静态的,因此您无需初始化Utils类即可使用此方法。

As a conclusion, you cannot use multiple inheritance but for your case, you don't also need that. You can use static methods without multiple inheritance.

作为结论,您不能使用多重继承,但对于您的情况,您不需要这样做。您可以使用静态方法而无需多重继承。

#4


7  

You can't do multiple inheritance in C#.

你不能在C#中做多重继承。

You could achieve the same ends using interfaces if it weren't for the fact that you'd like to share static methods.

如果不是因为您想要共享静态方法,则可以使用接口实现相同的目的。

#5


3  

You can inherit in "chain" as many classes you want but multiple inheritance is not allowed on .NET languages.

您可以在“链”中继承所需的多个类,但.NET语言不允许多重继承。

Your only option for multiple inheritance in .NET are interfaces.

您在.NET中进行多重继承的唯一选择是接口。

#6


3  

As long as the accessors are set appropriately, static methods are more or less shared anyway (in fact, they use the keyword Shared in VB); it makes no difference what class they are in because they are disconnected from any instantiated object.

只要适当地设置了访问器,静态方法或多或少都是共享的(实际上,它们使用VB中的共享关键字);它们与哪个类没有区别,因为它们与任何实例化对象断开连接。

To call a static method, you preface it with the name of the class on which it is declared:

要调用静态方法,请在其前面加上声明它的类的名称:

MyClass.MyUtilityMethod();

The accessor must be public or it can be internal if the caller and method are in the same project/assembly.

访问者必须是公共的,或者如果调用者和方法在同一个项目/程序集中,它可以是内部的。

If your goal is merely to create a single point of access for the static methods, you create a class that forwards the calls as appropriate:

如果您的目标仅仅是为静态方法创建单一访问点,则可以创建一个根据需要转发调用的类:

public static class SharedMethods
{
    public static void SharedMethod1()
    {
        ClassA.SharedMethod1();
    }

    public static string GetName()
    {
        return NameProvider.GetName();
    }

    public static int GetCount()
    {
        return CountClass.GetCount();
    }
}

This way you can access everything through SharedMethods.GetName() etc.

这样您就可以通过SharedMethods.GetName()等访问所有内容。

#7


2  

In .NET you can only inherit from one base class but implement multiple interfaces.

在.NET中,您只能从一个基类继承,但实现多个接口。

For an ASP.NET specific situation like you're in you can do the following:

对于像您一样的ASP.NET特定情况,您可以执行以下操作:

public class BasePage : Page
{
    public string SomeMethod() { //Magic happens here }
}

and have your webforms inherit from BasePage in codebehind.

并让您的webforms继承自代码隐藏中的BasePage。

Another often followed course is to make a custom static class which has all the static methods on it. Be sure if you're working with ASP.NET specific things to prefix them with HttpContext.Current.

另一个经常遵循的方法是创建一个自定义静态类,其中包含所有静态方法。确保您使用ASP.NET特定的东西为HttpContext.Current添加前缀。

For example:

例如:

public static class HelperClass
{
    public static string GetUsernameFromSession()
    {
        return HttpContext.Current.Session["Username"].ToString();
    }
}

#8


2  

You cannot inherit from multiple classes. However, you can string together classes that inherit off of each other.

您不能从多个类继承。但是,您可以将彼此继承的类串联起来。

Class Employee

班级员工

Class Manager : Employee

班主任:员工

class RegionalDirector : Manager

class RegionalDirector:Manager

So if you have a whole naming/employee ID/contact info scheme going on within your program, you can inherit that from employee. Then, elaborate with information for the manager, and then even more info for regional director.

因此,如果您的程序中有一个完整的命名/员工ID /联系信息方案,您可以从员工继承。然后,详细说明经理的信息,然后为区域主管提供更多信息。

#1


32  

C# does not support multiple inheritance (meaning a single class inherits from multiple classes). You can, however, implement multiple interfaces in a single class.

C#不支持多重继承(意味着单个类继承自多个类)。但是,您可以在单个类中实现多个接口。

As far as chaining together inherited classes, there isn't a limit per-se. Just keep in mind the complexity you will introduce to your system. When using inheritance, make sure you are using it in a "is a" scenario. A cheeta is an animal. A mazda is a car. Otherwise, your inheritance tightly couples your classes with a design that becomes much more difficult to maintain.

就将继承的类链接在一起而言,本身没有限制。请记住您将为系统引入的复杂性。使用继承时,请确保在“是”方案中使用它。 cheeta是一种动物。马自达是一辆汽车。否则,您的继承会将您的类与设计变得更加难以维护。

If they are static "utility" methods, just invoke them directly without inheriting. Unless those methods belong to the entity you are creating, you should not use inheritance.

如果它们是静态“实用程序”方法,则只需直接调用它们而不继承。除非这些方法属于您正在创建的实体,否则不应使用继承。

#2


15  

You can only inherit from a single class. It is however possible to implement multiple interfaces.

您只能从单个类继承。然而,可以实现多个接口。

#3


9  

Let's say you have Utils.cs:

假设你有Utils.cs:

internal class Utils {
    public static void Log(string str) {
        // log str to a file...
    }
}

and there is a DB.cs that uses Log method and in the same namespace with Utils class:

并且有一个DB.cs使用Log方法,并在与Utils类相同的命名空间中:

public class DB {
    public void SaveToDB() {
        // some db operations
        Utils.Log("DB operation successful");
    }
}

Since Log method is static, you don't need to initialize Utils class to use this method.

由于Log方法是静态的,因此您无需初始化Utils类即可使用此方法。

As a conclusion, you cannot use multiple inheritance but for your case, you don't also need that. You can use static methods without multiple inheritance.

作为结论,您不能使用多重继承,但对于您的情况,您不需要这样做。您可以使用静态方法而无需多重继承。

#4


7  

You can't do multiple inheritance in C#.

你不能在C#中做多重继承。

You could achieve the same ends using interfaces if it weren't for the fact that you'd like to share static methods.

如果不是因为您想要共享静态方法,则可以使用接口实现相同的目的。

#5


3  

You can inherit in "chain" as many classes you want but multiple inheritance is not allowed on .NET languages.

您可以在“链”中继承所需的多个类,但.NET语言不允许多重继承。

Your only option for multiple inheritance in .NET are interfaces.

您在.NET中进行多重继承的唯一选择是接口。

#6


3  

As long as the accessors are set appropriately, static methods are more or less shared anyway (in fact, they use the keyword Shared in VB); it makes no difference what class they are in because they are disconnected from any instantiated object.

只要适当地设置了访问器,静态方法或多或少都是共享的(实际上,它们使用VB中的共享关键字);它们与哪个类没有区别,因为它们与任何实例化对象断开连接。

To call a static method, you preface it with the name of the class on which it is declared:

要调用静态方法,请在其前面加上声明它的类的名称:

MyClass.MyUtilityMethod();

The accessor must be public or it can be internal if the caller and method are in the same project/assembly.

访问者必须是公共的,或者如果调用者和方法在同一个项目/程序集中,它可以是内部的。

If your goal is merely to create a single point of access for the static methods, you create a class that forwards the calls as appropriate:

如果您的目标仅仅是为静态方法创建单一访问点,则可以创建一个根据需要转发调用的类:

public static class SharedMethods
{
    public static void SharedMethod1()
    {
        ClassA.SharedMethod1();
    }

    public static string GetName()
    {
        return NameProvider.GetName();
    }

    public static int GetCount()
    {
        return CountClass.GetCount();
    }
}

This way you can access everything through SharedMethods.GetName() etc.

这样您就可以通过SharedMethods.GetName()等访问所有内容。

#7


2  

In .NET you can only inherit from one base class but implement multiple interfaces.

在.NET中,您只能从一个基类继承,但实现多个接口。

For an ASP.NET specific situation like you're in you can do the following:

对于像您一样的ASP.NET特定情况,您可以执行以下操作:

public class BasePage : Page
{
    public string SomeMethod() { //Magic happens here }
}

and have your webforms inherit from BasePage in codebehind.

并让您的webforms继承自代码隐藏中的BasePage。

Another often followed course is to make a custom static class which has all the static methods on it. Be sure if you're working with ASP.NET specific things to prefix them with HttpContext.Current.

另一个经常遵循的方法是创建一个自定义静态类,其中包含所有静态方法。确保您使用ASP.NET特定的东西为HttpContext.Current添加前缀。

For example:

例如:

public static class HelperClass
{
    public static string GetUsernameFromSession()
    {
        return HttpContext.Current.Session["Username"].ToString();
    }
}

#8


2  

You cannot inherit from multiple classes. However, you can string together classes that inherit off of each other.

您不能从多个类继承。但是,您可以将彼此继承的类串联起来。

Class Employee

班级员工

Class Manager : Employee

班主任:员工

class RegionalDirector : Manager

class RegionalDirector:Manager

So if you have a whole naming/employee ID/contact info scheme going on within your program, you can inherit that from employee. Then, elaborate with information for the manager, and then even more info for regional director.

因此,如果您的程序中有一个完整的命名/员工ID /联系信息方案,您可以从员工继承。然后,详细说明经理的信息,然后为区域主管提供更多信息。