. Net study notes on Polymorphism

Source: Internet
Author: User

Assign a derived class object to a base class reference. This is a very basic part for programs to process polymorphism objects.
The so-called reference is to declare the data type.
Then it is instantiated. "New ".
The reference of the derived class is assigned to the Base class reference: Point = circle
In this case, if you call the point method, it is the circle method because the derived class reference is assigned to the Base class reference.
Instead of the point method.
A syntax error occurs when you assign a base class object to a derived class for reference (no forced type conversion is required.
The derived class reference is assigned to the Base class reference. You can use forced type conversion to send
Messages in the base class. (The method and attribute of calling an object)
Because the derived class contains members corresponding to all the members of the base class object, the derived class may also have additional members. So,
If explicit forced type conversion is not performed, the base class reference cannot be assigned to the derived class reference. Option strict
In the on status. Otherwise, the attached Member of the derived class enters the undefined state.
Classes that do not instantiate any objects are called abstract classes. (Mustinherit)
Therefore, the dispatch class must overwrite the abstract methods and attributes to instantiate the objects of those derived classes.
For example, if someone asks you to draw a shape, what is the specific shape? Specific classes provide these details.
Many good object-oriented systems use mustinherit to Build Various Hierarchies. Sometimes occupying classes
Several levels at the top of the hierarchy.
The keyword mustoverride can declare a method and attribute as abstract. Each derived class must overwrite all
Mustoverride methods and attributes. Use keywords (overrides)
The difference between the mustoverride and overridable methods is that the latter has specific implementation methods. And allow the derived class
Overwrite. The former has no specific implementation, and the derived class must overwrite its method and attributes.
The iterator class is often used in object-oriented programming. It facilitates all objects in containers (such as arrays)
For example:
Dim arrayofshapes as cshape () = new shape (2)
Arrayofshapes (0) = point
Arrayofshapes (0) = circle
Arrayofshapes (0) = cylinder (cylindrical)
Dim shape as cshape
For each shape in arrayofshapes
Create and use interfaces:
The interface specifies that the class must be a series of public services (that is, methods and attributes) implemented ).
The interface must start with the keyword interface. To use the interface, the class must indicate that it has implemented its own implments.
The interface can provide an attribute and method for implementing these class objects. The methods or attributes of each object have been returned.
In event communication, the event sender class does not know which object or method will receive (process) the event it raises.
It is necessary to have a media (or pointer-like mechanism) between the source and the receiver ).. Net Framework
Defines a special type (delegate) that provides the function pointer function.
A delegate is a class that can be referenced by a method. Unlike other classes, the delegate class has one signature and can only
Reference the method that matches the signature. In this way, the delegate is equivalent to a type security function pointer or a callback.
A delegate is a reference type that references a shared method or an instance method of an object. In other languages,
The function pointer is the closest to the delegate, but the function pointer can only reference shared functions, and the delegate can reference shared functions.
And instance method. In the latter case, the delegate not only stores the reference to the method entry point, but also stores
Object instance reference.
Self-understanding: the essence of a delegate is the address of a method, through which you can find this method and use it.
This is used in multi-threaded programming.
Delegate sub mysubdelegate (X as integer)
Protected sub test ()
Dim C2 as new class2 ()
'Test the delegate.
C2.delegatetest ()
End sub
Class class1
Sub sub1 (byval X as integer)
MessageBox. Show ("the value of X is:" & CSTR (x ))
End sub
End Class
Class class2
Sub delegatetest ()
Dim C1 as class1
Dim MSD as mysubdelegate
C1 = new class1
'Create an instance of the delegate.
MSD = addressof c1.sub1
MSD (x) 'Call the method.
End sub
End Class
Delegate transfer parameters: the parameters passed by the delegate to class1.sub1 can be seen here.
There is another way, but parameters cannot be passed.
Dim MSD as new methodinvoke (addressof method name)
MSD. Invoke
The delegate is used in combination with the thread.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.