In the aesthetic class a few days ago, when the teacher talked about the beauty of form, let's take an example of Han Hong,
She hasn't provided long hair for so many years, because she understands that short hair is the most appropriate form of beauty.
The boys in the first row shouted: "She may be afraid that long hair will be recognized as Liu Huan !"
Let's take a look at the use of me, myclass, and mybase in base classes, classes, and diagonal interactions.
I. Me
The ME keyword provides a way to reference a specific instance of a running class or structure of the Code.
The behavior of me is similar to referencing the object variable or structure variable of the current instance.
It is particularly useful to use me when passing information about the current execution instance of a class or structure to a process in another class, structure, or module.
Conclusion: me can be omitted. It does not destroy existing rules, such as heavy loading, rewriting, and hiding. It is a wall grass, and every rule will adapt to it.
It will follow its virtual method and explain me according to the actual object type;
Hidden, it will follow its non-virtual method and interpret me according to the variable type;
It only writes a few letters less.
First, add a tostring Method to the person class.
Public overrides function tostring () as string 'override tostring return name end function in object
Note: 1. Why is overides used in basic scenarios?
Because all classes in. NET Framework are ultimately derived from system. object. This is true even if inherits is not used.
Therefore, the person class is inherited from the object class and can be overrides.
2. By default, tostring returns the class name (even if it is not defined ). This is rewritten to return the name attribute.
Then, we rewrite the display in the main program:
txtName.Text = .ToString
We can also add me to the tostring method:
Public overrides function tostring () as string 'overwrites the tostring return me. name' polymorphism in the object. The end function is changed.
But this is not necessary. For all method calls in the class, me is the default. This is not only true for rewriting or hiding, so the explicit me above does not affect
Generally, me should be omitted to avoid additional input.
Let's take a look at the me situation during Rewriting:
Add the officeemployee class, add the officenumber attribute, and override the name attribute.
Public class officeemployee inherits employee private moffice as string public property officenumber () as string get return moffice end get set (value as string) moffice = value end set end property public overloads overrides property name () as string get return mybase. name (nametype. informal) 'returns the end get set (value as string) mybase in the informal structure. name = value 'base class storage value end set end propertyend class
Note: In the above example, mybase refers to the base class, namely, employee. (Rather than person)
The code in the main program is as follows: run it and you can see that it displays Freddy.
Why?
A virtual method occurs because of rewriting. When you call a method directly or call a method through the me key:
The method on the current object will be called, so these method calls will follow the same rule as external method calls, that is,
If the tostring method is inherited, the class (employee or officeemployee) under it will be overwritten. This me is no longer a person,
The actual object type (in this example, officemployee ).
So the above example is as follows:
The object temp in officeemployee calls tostring (this is inherited from person), so the tostring method of person is used.
"Me" is no longer "person", but "officemployee", that is, "Temp. Name" is called, which is transferred to the name attribute of the code of this class.
The returned result is the name (nametype) attribute of the basic class (employee). Go to the employee code to extract the name (nametype) attribute, which is not
The official name is Freddy.
The procedure is as follows:
Let's take a look at the hidden performance of me:
In the officeemployee class, change the rewrite code to hide, as shown below:
Public Shadows Property Name() As String Get Return MyBase.Name(NameType.informal) End Get Set(value As String) MyBase.Name = value End Set End Property
In the main program, the type of variables and referenced objects is changed as follows:
As you can see, here it follows the hidden rules.
The variable type is employee. When tostring is called, it will enter the tostring of the person version, which uses me. name. This me is the current employ,
Therefore, the value of name is employee. Name (note that it is neither person. name nor officeemployee. Name), so the value is Fred.
Conclusion: Me does not affect the rewrite or hide functions. It also shows that me can be omitted.
Ii. mybase
The behavior of the mybase keyword is similar to the object variable that references the base class of the current class instance.
Mybase is usually used to access base class members whose classes are overwritten or hidden.
Mybase. New is used to explicitly call the base class constructor from the constructor of the derived class.
Mybase can only reference the direct parent class and cannot continue to locate along the inheritance chain.
Mybase can be called from the parent class or use any pulbic, friend, and protected elements.
Whether it is rewriting or hiding, it can break through this restriction and access the parent class method.
If me is a wall grass, mybase is a dingtalk. It can directly reference base-class elements without being influenced by the outside world.
In the above example, the officeemployee class hides the Code:
Public Shadows Property Name() As String Get Return MyBase.Name(NameType.informal) End Get Set(value As String) MyBase.Name = value End Set End Property
When mybase is used, no matter how the world changes, it is always positioned in the parent class employee. The method in employee is used.
Conclusion: The function of the base class is merged into the subclass with mybase.
Iii. myclass
The behavior of the myclass keyword is similar to the object variable of the current instance of the class that was referenced during initial implementation.
Myclass is similar to me, but all the methods that call the former are considered as notoverridable.
Myclass stubbornly sticks to itself and will not be changed due to external changes.
It can be said that both myclass and mybase are class-one roles and will not be changed due to external changes. myclass only points to this class, And mybase only points to the parent class.
Let's take a look at the changes:
Change the tostring method in person by using myclass, so it always uses the person version name.
Public overrides function tostring () as string 'override tostring return myclass. name' in the object to force end function of this class version
Run:
Why is the running value empty?
In the above main program, no matter how the value is assigned, the name value in the person version has never been assigned a value, and it has always been an empty string.
Therefore, although temp. tostring is used, it will call the tostring method in person, and this method calls myclass. Name, that is
Person. Name.
Iv. My
My function provides an easy and intuitive way to access a large number of. NET Framework classes,
This allows Visual Basic users to interact with computers, applications, settings, and resources.
It can be viewed as a call to the "System" function.
Conclusion: In the class, myclass and mybase play an irreplaceable role.