替代vb6.0_在VB.NET中的替代

替代vb6.0

This is one of a mini-series that covers the differences in Overloads, Shadows, and Overrides in VB.NET. This article covers Overrides. The articles that cover the others are here:

这是一个迷你系列,其中涵盖VB.NET中Overloads,Shadows和Overrides的差异。 本文介绍了替代。 涵盖其他内容的文章在这里:

-> Overloads-> Shadows

->超载->阴影

These techniques can be hugely confusing; there are a lot of combinations of these keywords and the underlying inheritance options. Microsoft's own documentation doesn't begin to do the topic justice and there is a lot of bad, or out of date information on the web. The best advice to be sure that your program is coded correctly is, "Test, test, and test again." In this series, we'll look at them one at a time with emphasis on the differences.

这些技术可能会造成极大的混乱。 这些关键字和基础继承选项有很多组合。 Microsoft自己的文档并没有开始公平地对待主题,并且网络上有很多不良或过时的信息。 确保程序正确编码的最佳建议是“测试,测试并再次测试”。 在本系列中,我们将一次重点介绍它们之间的差异。

覆写 ( Overrides )

The thing that Shadows, Overloads, and Overrides all have in common is that they reuse the name of elements while changing what happens. Shadows and Overloads can operate both within the same class or when a class inherits another class. Overrides, however, can only be used in a derived class (sometimes called a child class) that inherits from a base class (sometimes called a parent class). And Overrides is the hammer; it lets you entirely replace a method (or a property) from a base class.

阴影,重载和覆盖的共同点是,它们在更改发生的情况时可以重用元素的名称。 Shadows和Overloads可以在同一个类中运行,也可以在一个类继承另一个类时运行。 但是,替代只能在从基类 (有时称为父类)继承的派生类(有时称为子类)中使用。 超越是锤子; 它使您可以完全替换基类中的方法(或属性)。

In the article about classes and the Shadows keyword (See: Shadows in VB.NET), a function was added to show that an inherited procedure could be referenced.

在有关类和Shadows关键字的文章中(请参见:VB.NET中的Shadows),添加了一个函数来显示可以引用继承的过程。

Public Class ProfessionalContact
' ... code not shown ...
Public Function HashTheName(
ByVal nm As String) As String
Return nm.GetHashCode
End Function
End Class

The code that instantiates a class derived from this one (CodedProfessionalContact in the example) can call this method because it's inherited.

实例化从该类派生的类的代码(在示例中为CodedProfessionalContact)可以调用此方法,因为它是继承的。

In the example, I used the VB.NET GetHashCode method to keep the code simple and this returned a fairly useless result, the value -520086483. Suppose I wanted a different result returned instead but,

在该示例中,我使用VB.NET GetHashCode方法保持代码简单,这返回了相当无用的结果,即值-520086483。 假设我想返回一个不同的结果,但是,

-> I can't change the base class. (Maybe all I have is compiled code from a vendor.)

->我不能更改基类。 (也许我仅有的是来自供应商的编译代码。)

... and ...

...和...

-> I can't change the calling code (Maybe there are a thousand copies and I can't update them.)

->我无法更改调用代码(也许有一千个副本,我无法更新它们。)

If I can update the derived class, then I can change the result returned. (For example, the code could be part of an updatable DLL.)

如果可以更新派生类,则可以更改返回的结果。 (例如,该代码可以是可更新DLL的一部分。)

There is one problem. Because it's so comprehensive and powerful, you have to have permission from the base class to use Overrides. But well-designed code libraries provide it. (Your code libraries are all well designed, right?) For example, the Microsoft provided function we just used is overridable. Here's an example of the syntax.

有一个问题。 因为它是如此全面且强大,所以您必须获得基类的许可才能使用Overrides。 但是精心设计的代码库可以提供它。 ( 您的代码库都经过精心设计,对吗?)例如,我们刚刚使用的Microsoft提供的函数是可重写的。 这是语法示例。

Public Overridable Function GetHashCode As Integer

公共可重写函数GetHashCode为整数

So that keyword has to be present in our example base class as well.

因此,该关键字也必须存在于我们的示例基类中。

Public Overridable Function HashTheName(
ByVal nm As String) As String

Overriding the method is now as simple as providing a new one with the Overrides keyword. Visual Studio again gives you a running start by filling in the code for you with AutoComplete. When you enter ...

现在, 覆盖该方法就像提供一个带有Overrides关键字的新方法一样简单。 Visual Studio再次通过使用AutoComplete填写代码为您提供了一个良好的开端。 当您输入...

Public Overrides Function HashTheName(

Visual Studio adds the rest of the code automatically as soon as you type the opening parenthesis, including the return statement which only calls the original function from the base class. (If you're just adding something, this is usually a good thing to do after your new code executes anyway.)

键入打开的括号后,Visual Studio就会自动添加其余代码,包括仅从基类调用原始函数的return语句。 (如果您只是添加一些内容,那么无论如何执行新代码通常都是一件好事。)

Public Overrides Function HashTheName(
nm As String) As String
Return MyBase.HashTheName(nm)
End Function

In this case, however, I'm going to replace the method with something else equally useless just to illustrate how it's done: The VB.NET function that will reverse the string.

但是,在这种情况下,我将用其他同样无用的方法替换该方法,仅用于说明它是如何完成的:将反转字符串的VB.NET函数。

Public Overrides Function HashTheName(
nm As String) As String
Return Microsoft.VisualBasic.StrReverse(nm)
End Function

Now the calling code gets an entirely different result. (Compare with the result in the article about Shadows.)

现在,调用代码得到了完全不同的结果。 (与有关“阴影”的文章中的结果进行比较。)

ContactID: 246
BusinessName: Villain Defeaters, GmbH
Hash of the BusinessName:
HbmG ,sretaefeD nialliV

You can override properties too. Suppose you decided that ContactID values greater than 123 would not be allowed and should default to 111. You can just override the property and change it when the property is saved:

您也可以覆盖属性。 假设您决定不允许使用大于123的ContactID值,并且应将其默认设置为111。您可以覆盖属性并在保存属性时对其进行更改:

Private _ContactID As Integer
Public Overrides Property ContactID As Integer
Get
Return _ContactID
End Get
Set(ByVal value As Integer)
If value > 123 Then
_ContactID = 111
Else
_ContactID = value
End If
End Set
End Property

Then you get this result when a larger value is passed:

然后,当传递更大的值时,您将得到以下结果:

ContactID: 111
BusinessName: Damsel Rescuers, LTD

By the way, in the example code so far, integer values are doubled in the New subroutine (See the article on Shadows), so an integer of 123 is changed to 246 and then changed again to 111.

顺便说一下,到目前为止,在示例代码中,New 子例程中的整数值已加倍(请参见有关Shadows的文章),因此将123的整数更改为246,然后再次更改为111。

VB.NET gives you, even more, control by allowing a base class to specifically require or deny a derived class to override using the MustOverride and NotOverridable keywords in the base class. But both of these are used in fairly specific cases. First, NotOverridable.

VB.NET通过允许基类使用基类中的MustOverride和NotOverridable关键字明确要求或拒绝派生类重写来为您提供更多控制。 但是这两种都在相当特定的情况下使用。 首先,NotOverridable。

Since the default for a public class is NotOverridable, why should you ever need to specify it? If you try it on the HashTheName function in the base class, you get a syntax error, but the text of the error message gives you a clue:

由于公共类的默认值为NotOverridable,所以为什么还要指定它? 如果您在基类的HashTheName函数上尝试使用它,则会收到语法错误,但是错误消息的文本为您提供了一个线索:

'NotOverridable' cannot be specified for methods that do not override another method.

无法为不覆盖其他方法的方法指定“ NotOverridable”。

The default for an overridden method is just the opposite: Overrideable. So if you want overriding to definitely stop there, you have to specify NotOverridable on that method. In our example code:

重写方法的默认设置与之相反:可以重写。 因此,如果您希望重写一定要到此为止,则必须在该方法上指定NotOverridable。 在我们的示例代码中:

Public NotOverridableOverrides Function HashTheName( ...

Then if the class CodedProfessionalContact is, in turn, inherited ...

然后,如果依次继承了CodedProfessionalContact类,则...

Public Class NotOverridableEx
Inherits CodedProfessionalContact

... the function HashTheName cannot be overriden in that class. An element that cannot be overridden is sometimes called a sealed element.

...函数HashTheName不能在该类中重写。 不能覆盖的元素有时称为密封元素。

A fundamental part of the .NET Foundation is to require that the purpose of every class is explicitly defined to remove all uncertainty. A problem in previous OOP languages has been called “the fragile base class.” This happens when a base class adds a new method with the same name as a method name in a subclass that inherits from a base class. The programmer writing the subclass didn't plan on overriding the base class, but this is exactly what happens anyway. This has been known to result in the cry of the wounded programmer, "I didn't change anything, but my program crashed anyway." If there is a possibility that a class will be updated in the future and create this problem, declare it as NotOverridable.

的基本部分 的。 NET Foundation要求明确定义每个类的目的,以消除所有不确定性。 以前的OOP语言中的一个问题被称为“脆弱的基类”。 当基类在继承自基类的子类中添加与该方法名同名的新方法时,就会发生这种情况。 编写子类的程序员没有计划重写基类,但是无论如何,这确实是发生的。 众所周知,这导致受伤的程序员大喊:“我什么都没做,但是程序还是崩溃了。” 如果将来可能会更新一个类并造成此问题,请将其声明为NotOverridable。

MustOverride is most often used in what is called an Abstract Class. (In C#, the same thing uses the keyword Abstract!) This is a class that just provides a template and you're expected to fill it with your own code. Microsoft provides this example of one:

MustOverride最常用于所谓的抽象类中。 (在C#中,同一件事使用关键字Abstract!)这是一个仅提供模板的类,您应该用自己的代码填充它。 Microsoft提供了以下示例:

Public MustInherit Class WashingMachine
Sub New()
' Code to instantiate the class goes here.
End sub
Public MustOverride Sub Wash
Public MustOverride Sub Rinse (loadSize as Integer)
Public MustOverride Function Spin (speed as Integer) as Long
End Class

To continue Microsoft's example, washing machines will do these things (Wash, Rinse and Spin) quite differently, so there's no advantage of defining the function in the base class. But there is an advantage in making sure that any class that inherits this one does define them. The solution: an abstract class.

继续Microsoft的示例,洗衣机在处理这些事情(洗涤,漂洗和旋转)方面将大为不同,因此在基类中定义函数没有优势。 但是,确保任何继承这一类的类可以定义它们是有好处的。 解决方案:抽象类。

If you need even more explanation about the differences between Overloads and Overrides, a completely different example is developed in a Quick Tip: Overloads Versus Overrides

如果您需要更多有关“过载”和“替代”之间差异的解释,请在“快速提示:过载与替代”中开发一个完全不同的示例。

VB.NET gives you even more control by allowing a base class to specifically require or deny a derived class to override using the MustOverride and NotOverridable keywords in the base class. But both of these are used in fairly specific cases. First, NotOverridable.

VB.NET通过允许基类使用基类中的MustOverride和NotOverridable关键字明确要求或拒绝派生类重写来为您提供更多控制。 但是这两种都在相当特定的情况下使用。 首先,NotOverridable。

Since the default for a public class is NotOverridable, why should you ever need to specify it? If you try it on the HashTheName function in the base class, you get a syntax error, but the text of the error message gives you a clue:

由于公共类的默认值为NotOverridable,所以为什么还要指定它? 如果您在基类的HashTheName函数上尝试使用它,则会收到语法错误,但是错误消息的文本为您提供了一个线索:

'NotOverridable' cannot be specified for methods that do not override another method.

无法为不覆盖其他方法的方法指定“ NotOverridable”。

The default for an overridden method is just the opposite: Overrideable. So if you want overriding to definitely stop there, you have to specify NotOverridable on that method. In our example code:

重写方法的默认设置与之相反:可以重写。 因此,如果您希望重写一定要到此为止,则必须在该方法上指定NotOverridable。 在我们的示例代码中:

Public NotOverridableOverrides Function HashTheName( ...

Then if the class CodedProfessionalContact is, in turn, inherited ...

然后,如果依次继承了CodedProfessionalContact类,则...

Public Class NotOverridableEx
Inherits CodedProfessionalContact

... the function HashTheName cannot be overriden in that class. An element that cannot be overridden is sometimes called a sealed element.

...函数HashTheName不能在该类中重写。 不能覆盖的元素有时称为密封元素。

A fundamental part of the .NET Foundation is to require that the purpose of every class is explicitly defined to remove all uncertainty. A problem in previous OOP languages has been called “the fragile base class.” This happens when a base class adds a new method with the same name as a method name in a subclass that inherits from a base class. The programmer writing the subclass didn't plan on overriding the base class, but this is exactly what happens anyway. This has been known to result in the cry of the wounded programmer, "I didn't change anything, but my program crashed anyway." If there is a possibility that a class will be updated in the future and create this problem, declare it as NotOverridable.

.NET Foundation的基本部分是要求明确定义每个类的目的,以消除所有不确定性。 以前的OOP语言中的一个问题被称为“脆弱的基类”。 当基类在继承自基类的子类中添加与该方法名同名的新方法时,就会发生这种情况。 编写子类的程序员没有计划重写基类,但是无论如何,这确实是发生的。 众所周知,这导致受伤的程序员大喊:“我什么都没做,但是程序还是崩溃了。” 如果将来可能会更新一个类并造成此问题,请将其声明为NotOverridable。

MustOverride is most often used in what is called an Abstract Class. (In C#, the same thing uses the keyword Abstract!) This is a class that just provides a template and you're expected to fill it with your own code. Microsoft provides this example of one:

MustOverride最常用于所谓的抽象类中。 (在C#中,同一件事使用关键字Abstract!)这是一个仅提供模板的类,您应该用自己的代码填充它。 Microsoft提供了以下示例:

Public MustInherit Class WashingMachine
Sub New()
' Code to instantiate the class goes here.
End sub
Public MustOverride Sub Wash
Public MustOverride Sub Rinse (loadSize as Integer)
Public MustOverride Function Spin (speed as Integer) as Long
End Class

To continue Microsoft's example, washing machines will do these things (Wash, Rinse and Spin) quite differently, so there's no advantage of defining the function in the base class. But there is an advantage in making sure that any class that inherits this one does define them. The solution: an abstract class.

继续Microsoft的示例,洗衣机在处理这些事情(洗涤,漂洗和旋转)方面将大为不同,因此在基类中定义函数没有优势。 但是,确保任何继承这一类的类可以定义它们是有好处的。 解决方案:抽象类。

If you need even more explanation about the differences between Overloads and Overrides, a completely different example is developed in a Quick Tip: Overloads Versus Overrides

如果您需要更多有关“过载”和“替代”之间差异的解释,请在“快速提示:过载与替代”中开发一个完全不同的示例。

翻译自: https://www.thoughtco.com/overrides-in-vbnet-3424372

替代vb6.0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值