vb.net 模块和类有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14548991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between a module and a class?
提问by Stephen
When adding a new file to a VB.Net project in Visual Studio, I'm given the option of both a 'Class' and a 'Module'. A class is described as
在 Visual Studio 中向 VB.Net 项目添加新文件时,我可以选择“类”和“模块”。一个类被描述为
An empty class file
While a module is described as
虽然一个模块被描述为
A file for storing groups of functions
This seems to imply that a module is less useful that a class, since a class can store groups of functions and more.
这似乎意味着模块不如类有用,因为类可以存储函数组等等。
Is it the case that a module is simply a group of functions, or is there more to the module than the visual studio documentation suggests?
模块是否只是一组功能,或者模块是否比 Visual Studio 文档建议的更多?
回答by Olivier Jacot-Descombes
A classis a type. You can use this type like any other type (String
, Integer
, Date
, FileInfo
...) to declare variables, parameters, properties and function return types.
甲类是一种类型。您可以像使用任何其他类型 ( String
, Integer
, Date
, FileInfo
...)一样使用此类型来声明变量、参数、属性和函数返回类型。
Let's make a little example:
让我们举一个小例子:
Public Class Person
Public Property FirstName As String
Public Property LastName As String
Public Overridable Sub Print() 'I'll explain Overridable later.
Console.WriteLine("{0} {1}", FirstName, LastName)
End Sub
End Class
Now you can declare variables of type Person
现在您可以声明类型的变量 Person
Dim sue, pete As Person
Dim persons As List(Of Person)
sue = New Person()
sue.FirstName = "Susan"
sue.LastName = "Miller"
pete = New Person()
pete.FirstName = "Peter"
pete.LastName = "Smith"
persons = new List(Of Person)()
persons.Add(sue)
persons.Add(pete)
For Each person As Person In persons
person.Print()
Next
Whereas modulesare static. I.e. Data stored in a module exists exactly once. On the other hand, you don't have to instantiate a module with New
, therefore they are often used to store global data and for methods that are available globally. For instance you could store the persons list in a module.
而模块是静态的。即存储在模块中的数据只存在一次。另一方面,您不必使用 实例化模块New
,因此它们通常用于存储全局数据和全局可用的方法。例如,您可以将人员列表存储在模块中。
But there is much more you can do with classes. You can derive a class from a base class. This new class inherits everything from the base class and can add more stuff to it. For instance you could derive an Employee
class from Person
但是你可以用类做更多的事情。您可以从基类派生一个类。这个新类继承了基类的所有内容,并且可以向其添加更多内容。例如,您可以Employee
从Person
Public Class Employee
Inherits Person
Public Property Salary As Decimal
Public Overrides Sub Print
Console.WriteLine("{0} {1}, Salary = {2}", FirstName, LastName, Salary)
End Sub
End Class
The Overridable
keyword in Person.Print
allows deriving classes to re-define (to override) the Print
method. (Functions and Subs in classes are called methods.)
的Overridable
在关键字Person.Print
允许派生类重新定义(重写)的Print
方法。(类中的函数和子类称为方法。)
Employees are assignment compatible to Persons. You could add an employee to the persons
list. This does not require any change in the For Each loop, i.e., the call of person.Print()
automatically calls the right Print
method (the first one for "normal" persons and the second one for employees).
员工是与人员兼容的分配。您可以将员工添加到persons
列表中。这不需要在 For Each 循环中进行任何更改,即调用person.Print()
自动调用正确的Print
方法(第一个用于“正常”人员,第二个用于员工)。
Dim emp as Employee
emp = New Employee()
emp.FirstName = "Frank"
emp.LastName = "Taggart"
emp.Salary = 3500.00D
persons.Add(emp)
There is much more to say about classes. I hope that you got a certain idea of what you can do with classes.
关于类还有很多要说的。我希望你对你可以用类做什么有一定的了解。
See Objects and classes in Visual Basicand especially the section Differences between classes and modules.
请参阅Visual Basic 中的对象和类,尤其是类和模块之间的差异部分。
回答by Stefan Steiger
A module is nothing more than another name for a static class.
That's all there is to it.
If you don't believe it, compile a module with VB.NET and decompile with ILSpy using C-Sharp.
模块只不过是静态类的另一个名称。
这里的所有都是它的。
不信的话,用VB.NET编译一个模块,用C-Sharp用ILSpy反编译。
And yes, that means you're correct, in terms of functionality, a module is a SUBset of a class.
It follows, that in terms of functionality, a class is a SUPERset of a module, because it can contain static as well as non-static methods & variables, as well as the virtual and protected access modifiers.
是的,这意味着您是对的,就功能而言,模块是类的子集。
因此,就功能而言,类是模块的超级集,因为它可以包含静态和非静态方法和变量,以及虚拟和受保护的访问修饰符。
回答by xpda
A class is more of a unit, and a module is essentially a loose collection of stuff like functions, variables, or even classes.
一个类更像是一个单元,一个模块本质上是一个松散的集合,比如函数、变量甚至类。
In a public module, classes in the project have access to the functions and variables of the module. You don't have to specify the module name to address one. You can also have classes in a module.
在公共模块中,项目中的类可以访问模块的函数和变量。您不必指定模块名称来寻址一个。您还可以在模块中拥有类。
The variables and functions of a class are under tighter "ownership" by the class. Public variables and functions (methods) used by other classes are used with the classname: classname.method
, unlike those in modules.
类的变量和函数由该类拥有更严格的“所有权”。其他类使用的公共变量和函数(方法)与 classname: 一起使用,这classname.method
与模块中的不同。
There is only one instance of a module, but a one or more instances of a class can be used at once.
一个模块只有一个实例,但一个类的一个或多个实例可以一次使用。
回答by Sven Kannenberg
The main difference between classes and modules is that classes can be instantiated as objects while standard modules cannot. Because there is only one copy of a standard module's data, when one part of your program changes a public variable in a standard module, any other part of the program gets the same value if it then reads that variable. In contrast, object data exists separately for each instantiated object. Another difference is that unlike standard modules, classes can implement interfaces.
类和模块之间的主要区别在于,类可以实例化为对象,而标准模块则不能。因为标准模块的数据只有一个副本,所以当程序的一部分更改标准模块中的公共变量时,程序的任何其他部分如果随后读取该变量,就会获得相同的值。相比之下,每个实例化对象的对象数据都单独存在。另一个区别是与标准模块不同,类可以实现接口。
Source: http://msdn.microsoft.com/en-us/library/7825002w(en-US,VS.80).aspx
来源:http: //msdn.microsoft.com/en-us/library/7825002w(en-US,VS.80).aspx
回答by Mahdi Jazini
Actually, you use classes to create objects. For example in the following .NET console application r
is a Rectangle
object:
实际上,您使用类来创建对象。例如在下面的 .NET 控制台应用程序r
是一个Rectangle
对象:
Imports System
Public Class Rectangle
Private length As Double
Private width As Double
'Public methods
Public Sub AcceptDetails()
length = 4.5
width = 3.5
End Sub
Public Function GetArea() As Double
GetArea = length * width
End Function
Public Sub Display()
Console.WriteLine("Length: {0}", length)
Console.WriteLine("Width: {0}", width)
Console.WriteLine("Area: {0}", GetArea())
End Sub
Shared Sub Main()
Dim r As New Rectangle()
r.Acceptdetails()
r.Display()
Console.ReadLine()
End Sub
End Class