有没有一个词包含“类”?和“结构”?
C# 中的类和结构体具有以下几个共同特征:
- 它们可以被实例化(没有相反的限制,与抽象类和静态类一样)
- 它们可以包含方法和属性实现
- 类型的作者定义类型的实例字段
我们经常使用“类”和“ struct”来区分“引用类型”和“值类型”,但有时考虑两种类型很有用。此外,“引用类型”还包括接口和委托,它们不是类。所以“class”并不意味着任何引用类型,它的意思是“引用_(填空)_”。
例如,如果引用和值类型声明如下所示:
public sealed class ref String { }
public class val Int32 { }
而不是这样:
public sealed class String { }
public struct Int32 { }
那么单词“类”可用于表示该概念。
我在这里提出的最佳答案是“具体类型”,但这会令人困惑,因为它也可以引用抽象类的非抽象子类。
有什么建议吗?
编辑
澄清一下,我并不是在寻找一个可以共同描述类和结构的实例的词。我试图描述类类型和结构类型。
换句话说,如果“class”表示包含 System.String
、System.FileInfo
等的集合,而“struct”表示包含 的集合System.Int32
、System.Collections.Generic.List
等,然后我正在寻找表示这些集合的并集的单词。
编辑 2
(针对 Jordão 的回答)回答此问题的另一种方法是完成以下句子:“所有 C# 方法实现都必须声明为 _(填空)_”。
Classes and structs in C# share several characteristics:
- they can be instantiated (absent restrictions to the contrary, as with abstract and static classes)
- they can contain method and property implementations
- the type's author defines the type's instance fields
We often use "class" and "struct" to distinguish between "reference type" and "value type", but sometimes it's useful to consider both types of types. Furthermore, "reference type" also includes interfaces and delegates, which are not classes. So "class" doesn't mean any reference type, it means "a reference _(fill in the blank)_".
For example, if reference and value type declarations were like this:
public sealed class ref String { }
public class val Int32 { }
instead of like this:
public sealed class String { }
public struct Int32 { }
then the word "class" could be used to denote the concept.
The best answer I've come up with here is "concrete type", but that would be confusing, since it could also refer to the non-abstract subclass of an abstract class.
Any suggestions?
EDIT
To clarify, I'm not seeking a word that can collectively describe instances of classes and structs. I'm trying to describe class types and struct types.
In other words, if "class" denotes a set that includes System.String
, System.FileInfo
, etc., and "struct" denotes a set that includes System.Int32
, System.Collections.Generic.List<T>.Enumerator
, etc., then I'm looking for the word that denotes the union of those sets.
EDIT 2
(In reaction to Jordão's answer) Another way to answer this question would be to complete the following sentence: "All C# method implementations must be declared as members of a _(fill in the blank)_".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我通常使用术语“类型”来指代任何这些元素:类、结构甚至接口和枚举。
我从来没有真正觉得有必要专门讨论类和结构,我可能只会说“类”,然后根据需要区分它们。
I normally use the term "type" to refer to any of those elements: class, struct and even interfaces and enums.
I never really felt the need to talk about classes and structs exclusively, I would probably just say "class", and then differentiate them as needed.
C# 中的术语
type
可以指以下任意一种:对象
、动态
和字符串
List
)decimal
和bool
)所有这些都是 C# 规范中的术语。
类、接口、委托、结构和枚举类型也称为类型声明(或:用户可定义类型)。
根据您的观点,您可能还会将类型参数和
void
视为类型。但是,“类或结构”没有任何特殊术语。在 C# 规范的语言中,人们会说:
The term
type
in C# can refer to any of:object
,dynamic
andstring
List<string>
)decimal
, andbool
)All of these are terms from the C# specification.
class, interface, delegate, struct and enum types are also called
type declarations
(or:user-definable types
).Depending on your point of view, you might also consider type parameters and
void
to be types.However, there isn't any special term for "classes or structs". In the language of the C# specification, one would say:
我意识到这个问题有一个选定的答案,但我相信我可以提供一个仍然有帮助的新鲜见解:
我认为您正在寻找的词可能是模型。该术语在 CS 中用于表示多种不同的事物,但维基百科文章中的数学模型描述了我的意图。
在这种情况下,模型是某种元语言对系统的描述。一个系统可以用它的三个部分来完整地表达:结构;行为;以及互连性。 .NET 类和 .NET 结构都与此定义兼容。接口不是,因为行为没有定义。您只能指示方法调用和成员声明的结构以及操作的类型契约(互连性)。枚举可能与此定义兼容,也可能不兼容,但最常用的是不兼容,因为它们通常不表达行为。枚举是个例外,按位运算足以表示有意义的集合运算。有了这个前提,我认为将枚举与类和结构一起分类是公平的。
附带说明一下,如果扩展方法被解释为它们扩展的类型的固有方法,那么接口和标准枚举本身都可以被视为系统。但是,编译器和我都不认为扩展方法是第一个操作数类型所固有的。更准确的解释是将枚举/接口和扩展方法视为系统的必要组件。这些扩展的组件类型与类/结构/特殊情况枚举之间的区别在于,类/结构/特殊情况枚举本身就是一个系统,因此是其系统的子系统包含系统,而组件类型是一个组件,但本身不是一个系统。
可能值得澄清的是,根据这种解释,术语模型类似于类型,而术语系统类似于实例。系统也可以应用于更大的复合材料,例如装配体,但这不是问题所在。
“所有 C# 方法实现都必须声明为模型的成员”这一说法似乎有效。从逻辑上讲,它也不意味着“所有模型都可以包含自定义方法实现”,因此在集合论枚举的特殊情况下我们是安全的。它也适用于建模系统由静态扩展方法实现和接口组成的情况。
I realize this question has a selected answer, but I believe I can offer a fresh insight that will still be helpful:
I think the word you are looking for may be Model. This term is used to mean several different things in CS, but the wikipedia article for mathematical model describes my intension.
In this context, a model is a description of a system in some meta language. A system can be fully expressed in terms of its three parts: structure; behavior; and, interconnectivity. Both .NET classes and .NET structs are compatible with this definition. Interfaces are not, because the behavior is not defined. You can only indicate the structure of method calls and member declarations and the type contracts for operations (interconnectivity). Enums may or may not be compatible with this definition, but as most frequently used are not, because they typically do not express behavior. The exceptions are enums for which bitwise operations are sufficient representations of meaningful set operations. With this precondition, I think its fair to classify an enum along with classes and structs.
As a side note, both interfaces and standard enums could be considered as systems by themselves, if extension methods were interpreted as intrinsic to the types they extend. However, neither the compiler nor I would consider extension methods to be intrinsic to the type of the first operand. A more accurate interpretation would be to consider both the enum/interface and the extension method as necessary components of a system. The difference between these component types that are extended and a class/struct/special-case enum is that the class/struct/special-case enum is a system in-itself, and therefore a subsystem of its containing system, whereas the component type is a component but not a system in itself.
It is probably worthwhile to clarify that, under this interpretation, the term model is analogous to a type, whereas the term system is analogous to an instance. A system could also apply to a larger composite, such as an assembly, but that is not what the question was about.
The statement "All C# method implementations must be declared as members of a model" seems to work. It also does not logically entail that "all models can contain custom method implementations", so we are safe in the special-case of set-theoretic enums. It would also work in the case where the modeled system is the composition of static extension method implementations and interfaces.
我认为这是一个伪讨论。第三点也可以说是关于枚举和接口,而关于抽象类的子类不适合混合的观点,我根本不明白。我认为您自己对“具体类型”的建议是可以的,但也许您只想将它们视为类和结构,哦等等,但抽象类的子类和实现接口的类除外。没有针对您要查找的内容的术语的原因可能是它本身并不是一个非常有用的概念。
编辑:
所有 C# 方法实现都必须声明为类或结构的成员。
I find this to be a pseudo discussion. Point #3 can be said about enums and interfaces too, and the point about subclasses of abstract classes not fitting into the mix, I simply don't get. I think your own suggestion of "concrete types" is ok, but maybe you just want to talk about them as classes and structs, oh wait, but with the exception of subclasses of abstract classes and classes that implement interfaces. The reason that there is no term for what you are looking for might be that it is not a very useful concept in its own right.
EDIT:
All C# method implementations must be declared as members of a class or struct.
应该是微软的术语。这些概念的起源是 C++,其中结构只是所有成员都是公共的类。因此,微软在这里做了一些新的判断,混合了一些 C、C++ 和 Java。所以他们还应该发明一个术语。
Microsoft 将它们全部表示为“类型”,可以是“值”、“引用”和“指针”: http://msdn.microsoft.com/en-us/library/3ewxz6et(v=vs.100).aspx
但是这些概念不仅仅聚集结构和类。
因此,如果发明一些自定义术语,我们可以从 Pascal 语言中选取一个术语,例如“记录”。或者可以从这里创造一些其他术语:http://en.wikipedia.org/wiki/Object_composition
It should be Microsoft term. The origin of these notions is C++, where structs are just classes with all members being public. So, Microsoft cooked some new judging here, mixing some of C, C++ and Java. So they should invent also a terms.
Microsoft denotes them all as "types" which can be "value", "reference" and "pointer": http://msdn.microsoft.com/en-us/library/3ewxz6et(v=vs.100).aspx
But these notions do not gather only structs and classes.
So, if invent some custom term, we may take one from Pascal language, for example, where it is "record". Or some other terms can be coined from here: http://en.wikipedia.org/wiki/Object_composition
类和结构都是定义对象的类型。它们是面向对象编程语言中的构建块。您可以使用 UML 或其他一些高级面向对象建模语言对它们进行建模。选择其中之一是实施细节。
Both classes and structs are types which define objects. They are building blocks within an object oriented programming language. You can model both of them using UML or some other high-level object oriented modelling language. The choice between one or the other is an implementation detail.