Henry's VB.NET Tour (6) - overloaded and hidden

zhaozj2021-02-16  62

??????? Henry's VB.NET trip (6) - overloading and hiding

????????????????????????? Han Rui

? Big Li is sitting around the swivel chair, and a coin in his hand rolls in his finger. "Hi, Henry. What is your meaning of rewriting and overload?"

I am staring at the coin in his hand, "Oh, rewrite, it is the example you just mentioned, rewritten in the derived class with Overrides to rewrite the basic class or attribute of the Overridable identity; overload, it is us You can use the same name to create multiple methods and properties in a class in a class, you can adapt to different parameter types when calling. "

"Created in a class?" Da Li's left eyebel is picking up, I know that I may say something wrong. However, it seems that there is nothing wrong with it.

"Well, let's take a look, what is the result of such a writing?"

Public Class CBasehenry

??? public sub Oldy ()

??????? console.writeLine ("Oldy method of the base class")

??? End Sub

END CLASS

Public Class CDeriveDhenry

??? inherits CBasehenry

??? public overloads Sub Oldy (ByVal J AS Integer)

??????? console.writeLine ("Directory Oldy Method")

??? End Sub

END CLASS

Public Sub Main ()

?? Dim obj as cderivedhenry = new cderivedhenry ()

......

?

Then Big Li wrote Obj.old that appeared on the intelligent perceived menu that appeared at this time:?

?

?

"咦, the second oldy () no parameter process signature should be a base class definition. Why will there be in the instance of derived classes!" I couldn't help but curious.

"Yes. I still remember what I last explicitly prepared to write constructive function? I used a mybase.new () used to inherit the nature of the base class. Now no write, actually a default implicit call Da Li said that I will understand that in fact, the current derived class should be included in the two Oldy methods. There is no parametric Oldy () is actually the basic class method, because OverLoads and mybase.new () Dual roles Impact.

"Then look at this change again." Dafu is thinking that I have understood this problem. He added a non-parameter OLDY () that was consistent with the base class form in the derived class.

Public Class CBasehenry

??? public sub Oldy ()

??????? console.writeLine ("Oldy method of the base class")

??? End Sub

END CLASS

Public Class CDeriveDhenry

??? inherits CBasehenry

??? public overloads Sub Oldy (ByVal J AS Integer)

??????? console.writeline ("Derive OLDY Method 1")

End Sub

??? public sub Oldy ()

??????? console.writeline ("" "Oldy Method 2"))

??? End Sub

END CLASS

Public Sub Main ()

?? Dim obj as cderivedhenry = new cderivedhenry ()

?? obj.oldy ()

End Sub

?

"Okay, now you talk about it, what is the result of the Obj.oldy () running now?" Da Li handed me on the F5 run button. "It should be derived, wrong, base class, oh, derived class ..." I suddenly fainted.

"Oh." Da Li did not ban laughter. Then click the F5 key. turn out:

?

Derived Oldy Method 2

?

"This is hidden. We use OverLoads to hide the same name method of the base class. In general, there are two cases, one is to implement, such as you define a global variable x However, in one method, you have defined a local variable x, when using X in the method, the one of the local variables is used, that is, the local variable X hides the global variable X. Another The situation is hidden by inheritance. In addition to the OverLoads just used, you can also be implemented with shadows keywords. "

"Shadows? Shadow? It is very attractive. How is this use?" I am born.

"Shadows is very powerful." Said that Da Li started to modify the code just now.

?

Public Class CBasehenry

??? public sub Oldy ()

??????? console.writeLine ("Oldy method of the base class")

??? End Sub

END CLASS

Public Class CDeriveDhenry

??? inherits CBasehenry

??? Public Shadows Sub Oldy (ByVal J AS Integer)

??????? console.writeLine ("Directory Oldy Method")

??? End Sub

END CLASS

Public Sub Main ()

?? Dim obj as cderivedhenry = new cderivedhenry ()

......

?

When you write Obj.OLDY, there is a method left on the smart sensing menu.

?

?

"Ha, really, the basic class method can not see. But is this and the effect of rewriting is the same?" I couldn't help but think of a question, if it is the same role, what is Shadows to do.

"Still distinguish." Da Li began to take the number of points. "The most obvious difference is that hidden uses of any element type, you can use public shadows oldy as integer in derived class Oldy () Methods; rewriting can only be applied to methods and properties, and the declaration parameters are required to be exactly the same. "

"Pay attention to it, when the derived class is derived, the rewriting and hidden will be inherited. Of course, if you use private to modify Shadows members in the derive class, its subclasses will inherit it. The base class. "

"I feel that VB.NET is now very powerful to the inheritance function, inherit, for the programmer, really a good thing." I can't help it.

"Yes, the inheritance hierarchy system is a lot of benefits, but things always have its own bilateral, inheritance, there are many problems, the most troublesome, is' fragile base". "Da Li shouted and said seriously.

"Oh, what is the 'fragile base"? "I didn't understand.

?

(Endlessly)

-------------------------------------------------- -------------

Disclaimer: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement. QQ: 18349592

E-mail: Henry7685@hotmail.com

Please visit my column: http://www.9cbs.net/develop/author/netauthor/latitude/

转载请注明原文地址:https://www.9cbs.com/read-25530.html

New Post(0)