从类继承

发布于 2024-08-04 10:47:16 字数 124 浏览 8 评论 0原文

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

当你写:

使用 System.Web.UI;

这是否被认为是从类继承?

In C#, how many classes can you inherit from?

When you write:

using System.Web.UI;

is that considered inheriting from a class?

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

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

发布评论

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

评论(5

唠甜嗑 2024-08-11 10:47:16

using 指令

using X.Y.Z;

仅意味着如果标识符不在本地命名空间中,则还将在命名空间 XYZ 中搜索该标识符。这允许您拥有更简洁的代码,因此如果您using System.IO.Path,则不必执行System.IO.Path.Combine(),例如。我认为您可以拥有的 using 语句数量没有限制。

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

A using directive:

using X.Y.Z;

only means that if an identifier isn't in the local namespace, the namespace X.Y.Z will be searched for that identifier as well. This allows you to have somewhat terser code, so you don't have to do System.IO.Path.Combine() if you have using System.IO.Path, for instance. I don't believe there is a limit on how many using statements you can have.

C# does not allow multiple inheritance -- you can only inherit from one class. However, you can implement as many interfaces as you'd like.

摇划花蜜的午后 2024-08-11 10:47:16

不,

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

例如,如果我添加:

using System.Web.UI;

那么我可以简单地写:

Page.Something

而不是:

System.Web.UI.Page.Something

继承
您只能从一个类继承,但您可以根据需要实现任意多个接口

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

No,

A using directive allows you to not have to type the fully qualified namespace when you add a control or a class to your code.

For instance, if I add:

using System.Web.UI;

Then I can simply write:

Page.Something

Instead of:

System.Web.UI.Page.Something

Inheritance
You can only inherit from one class, but you can implement as many Interfaces as you want.

If all of my pages inherit from a base class, then that base class will need to inherit from the Page class for it to do any good.

那片花海 2024-08-11 10:47:16

您只能从一个类继承。

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

You can inherit from only one class.

Writing 'using System.Web.UI' has nothing to do with inheritance.
This statement just means that you want to 'use' the classes that are in the namespace System.Web.UI, so that you can directly write the class-name, instead of having it to prefix with the namespace-name.

往事随风而去 2024-08-11 10:47:16

否。

  1. using 使外部定义的类可见
  2. class Cat : Animal 定义了一个派生(继承)自 Animal 的类 Pet >

C# 与 C++ 不同,不支持多重继承,因此只能从一个类派生(但是,可以实现任意数量的接口)

No.

  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)

旧时光的容颜 2024-08-11 10:47:16

我想对 Jen 给出的答案添加一个附录。确实,C# 不支持多重继承,但了解可以通过忽略 CLS(命令语言子系统)在 .NET 中实现多重继承会很有用。这是一项令人讨厌的工作,并且需要你滚动自己的虚拟表,但这是可能的。

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.

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