1

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

当你写:

using System.Web.UI;

这被认为是从一个类继承的吗?

4

5 回答 5

7

使用指令

using X.Y.Z;

仅表示如果标识符不在本地命名空间中,则X.Y.Z还将在命名空间中搜索该标识符。这使您可以拥有一些更简洁的代码,例如,System.IO.Path.Combine()如果您有. 我不相信您可以拥有using System.IO.Path多少个语句。using

C# 不允许多重继承——你只能从一个类继承。但是,您可以实现任意数量的接口。

于 2009-09-06T20:44:24.960 回答
7

不,

using指令允许您在将控件或类添加到代码时不必键入完全限定的命名空间。

例如,如果我添加:

using System.Web.UI;

然后我可以简单地写:

Page.Something

代替:

System.Web.UI.Page.Something

继承
您只能从一个类继承,但您可以实现任意数量的接口

如果我的所有页面都继承自一个基类,那么该基类将需要从Page该类继承才能发挥作用。

于 2009-09-06T20:45:08.873 回答
3

您只能从一个类继承。

编写“使用 System.Web.UI”与继承无关。该语句仅表示您要“使用”命名空间 System.Web.UI 中的类,以便您可以直接编写类名,而不是让它以命名空间名作为前缀。

于 2009-09-06T20:44:28.867 回答
1

不。

  1. using makes externally defined classes visible
  2. class Cat : Animal defines a class Pet that derives (inherits) from Animal

C#, unlike C++, does not support multiple inheritance, therefore you can only derive it from one class (However, you can implement any number of interfaces)

于 2009-09-06T20:48:12.103 回答
1

I'd like to add an addendum to the answer given by Jen. It's correct to say that C# doesn't support multiple inheritance, but it can be useful to know that you can implement multiple inheritance in .NET by ignoring the CLS (the Command Language Subsystem). It's nasty work, and involves you rolling your own vtables, but it is possible.

于 2009-09-06T21:07:56.093 回答