Go from Csharp to VB.NET (1): Myclass reserved word

xiaoxiao2021-03-06  17

The first B / S project in 2003 was DOTNET. It is csharp. Now 2 years have passed, and also contact VB.NET project, step by step, see the same CLR platform, 2 kinds The language is still very large in the reserved word. The following is one by one: About MyClass, please build a vbconsoleApplication program first, put this article Sourcecp

Module Module1 Class BaseClass Public Overridable Sub MyMethod () Console.WriteLine ( "Father is 50 years old!") End Sub Public Sub UseMe () Me.MyMethod () 'Use calling class's version, even if an override. End Sub Public Sub UseMyClass () MyClass.MyMethod () 'Use this version and not any override End Sub End Class Class DerivedClass:. ( "! child is just 25 years old") Inherits BaseClass Public Overrides Sub MyMethod () Console.WriteLine End Sub End Class Sub main () Dim Child.Useme () child.usemyclass () console.readline () End Subend Module But can't be implemented in CSHARP, C #, how does it not allow me to call a virtual in the non-virtual function? function. C can access its own version of the function with a class name :: method name, but the C # class name can only be used to access static members. This is really C # a strange limit. Maybe I am rare. In MSDN, this is described in MSDN: MyClass behavior is similar to the object variables that initially implemented the current instance of the class. Myclass is similar to ME, but the call to all methods of it is processed by this method as NOTOVERRIDABLE. Therefore, the method called is not affected by rewriting in the derived class. Although derivedClass rewrites MyMethod, the Myclass keyword in UseMyClass is invalid, and the compiler will resolve the call to the base class version of MyMethod. Myclass cannot be used inside the Shared method, but you can use it to access the class sharing member in the instance method.

But in CSHARP can't be implemented, C # should not allow me to call a virtual function in the way in the non-virtual function. C can access its own version of the function with a class name :: method name, but the C # class name can only be used to access static members. This is really C # a strange limit. Maybe I am rare. In MSDN, this is described in MSDN: MyClass behavior is similar to the object variables that initially implemented the current instance of the class. Myclass is similar to ME, but the call to all methods of it is processed by this method as NOTOVERRIDABLE. Therefore, the method called is not affected by rewriting in the derived class. Although derivedClass rewrites MyMethod, the Myclass keyword in UseMyClass is invalid, and the compiler will resolve the call to the base class version of MyMethod. Myclass cannot be used inside the Shared method, but you can use it to access the class sharing member in the instance method. This is still very nice in business applications, such as the parent class as an EmployeeBase, contains a basic salary data, then in instantian employee's Employee (EMPLOYEEID), when the employee's basic salary is increased, O / R Image Model After the E1.Addsalary method is required to call the base class to modify the basic salary modification method. (Ie, uniformly add a basic salary). Personal opinion, huh, what is not good, please advise you.

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

New Post(0)