C# 与 C++ 中的类定义和实现

发布于 2024-10-07 23:11:32 字数 181 浏览 1 评论 0原文

使用 C++,我可以在头文件中包含一个类定义,并通过包含该头文件来拥有多个实现文件。

对于 C#,似乎没有这样的头文件,因为一个类应该包含定义/实现。

我想知道行数是否可以很大,因为无法将类分成多个文件。我说得对吗?我的意思是,在某些情况下,人们无法改变班级设计以拥有更小的班级。既然如此,有没有办法解决这个问题呢?

With C++, I can have one class definition in a header file, and have a multiple implementation files by including the header file.

With C#, it seems that there is no such header file, as one class should contain both definition/implementation.

I wonder if the number of lines can be very big, because one can't separate the class into multiple files. Am I correct? I mean, in some cases, one can't change the class design to have smaller classes. In this case, is there a way to solve this problem?

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

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

发布评论

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

评论(4

逆光飞翔i 2024-10-14 23:11:32

您可以使用partial关键字将一个类分成多个文件

public partial class ClassNameHere
{
}

You can separate a class into multiple files using the partial keyword

public partial class ClassNameHere
{
}
吾家有女初长成 2024-10-14 23:11:32

可以使用 Partial 关键字修饰符将类、结构或接口的定义拆分为两个或多个源文件 链接到带有部分类的 msdn

It is possible to split the definition of a class or a struct, or an interface over two or more source files using the Partial keyword modifier Link to msdn with the partial class

自此以后,行同陌路 2024-10-14 23:11:32

部分课程只能给你这么多。据我所知,仍然没有办法将类定义与实现分开,以便每个类定义都存在于单独的文件中。因此,如果您喜欢基于需要了解的范式进行开发,那么您就会陷入困境。基本上,开发人员可以工作在三个级别...

1) 拥有所有代码并有权访问和维护所有代码。

2)希望使用一些有用的基类,这些基类可能构成框架的一部分,或者可能只是具有一些虚拟方法等的有用类,并希望扩展或重新实现一些感兴趣的虚拟基类方法。现在,开发人员不需要为了在功能级别上理解事物而去查看基类中的代码。如果您了解函数的工作,即它的输入和输出参数,则无需去查看源代码。如果您认为存在错误,或者需要优化,请咨询 1) 拥有和维护基本代码的开发人员。当然,没有任何规定 1) 和 2) 不能与同一个开发人员关联,在这种情况下我们没有问题。事实上,我怀疑这种情况经常发生。尽管如此,根据您的工作级别将事物分开仍然是一个很好的做法。

3)开发人员需要使用已经封装/密封的对象/组件dll,它公开了相关接口。

在 c# 上下文中,1) 和 3) 没有问题。对于2),我相信没有办法解决这个问题(除非您从公开虚拟基方法更改为公开可以在拥有潜在基类的组件中重新实现的接口方法)。如果我想查看类定义来浏览方法、脚手架函数等,我还必须查看大量源代码,这会妨碍我想要关注的内容。

当然,如果存在我们通常执行方式之外的类定义文档(在标头和源文件中),那么我必须承认,在 2) 的上下文中,没有理由查看类定义文件来获取功能性知识。

因此,也许聪明的 Tom 提出了 C#,决定将类定义与实现混合在一起,试图鼓励开发人员为其类定义和接口提供外部文档,而这在大多数 IT 公司中都严重缺乏。

Partial classes only give you so much. There is still no way, that i know of, to split your class definition from implementation, such that each exists in a separate file. So if you like to develop based on a need-to-know paradigm then you are sort of stuck. Basically there are three levels a developer can work at...

1) Owns all the code and has access to, and maintains all of it.

2) Wishes to use some useful base class(s) which may form part of a framework, or may just be a useful class with some virtual methods, etc, and wishes to extend, or re-implement some virtual base class methods of interest. Now the developer should not need to go and look at the code in the base class(s) in order to understand things at a functional level. If you understand the job of a function, it's input and output parameters, there is no need to go and scratch inside source code. If you think there's a bug, or an optimization is needed, then refer to the developer from 1) who owns and maintains the base code. Of course there's nothing saying that 1) and 2) cannot be associated with the same developer, in which case we have no problem. In fact, this is more than often the case i suspect. Nevertheless, it is still good practice to keep things well separated according to the level at which you are working.

3) A developer needs to use an already packaged / sealed object / component dll, which exposes the relevant interfaces.

Within the context of c#, 1) and 3) have no problems. With 2) i believe there is no way to get round this (unless you change from exposing virtual base methods to exposing interface methods which can be reimplemented in a component owning the would-be base class). If i want to have a look at a class definition to browse over the methods, scaffolding functions, etc, i have to look at a whole lot of source code as well, which just gets in the way of what i am trying to focus on.

Of course if there is class definition documentation external to how we normally do it ( in headers and source files), then i must admit, that within the context of 2), there is not reason to ever look into a class definition file to gain functional knowledge.

So maybe clever Tom's came up with c#, decided to mix class definition with implementation in an attempt to encourage developers to have external documents for their class definitions, and interfaces, which in most IT companies is severely lacking.

前事休说 2024-10-14 23:11:32

按照@sparks的建议使用部分类,或者分成几个类。这是一个很好的经验法则,如果您无法将一个类放在几页纸上,那么它就足够复杂,需要将其分解。

Use a partial class as @sparks suggests, or, split into several classes. It's a good rule of thumb that, if you can't fit a class onto a couple of pages, it's complicated enough to need breaking apart.

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