Our task
The Basic idea of this program is as follows: we have an abstract base class (in Microsoft Visual Basic? Is MustInherit), which contains public data (such as borders) and a set of virtual methods. Most of the virtual methods are abstract (MustOverride in Visual Basic), such as Draw. Note that the polymorphism of Draw is very important, because each object type that can be drawn (such as vertex, line, rectangle, circle, etc.) is drawn with completely different code.
Although the methods can be polymorphism, the data cannot. Therefore, we only place the data that is indeed applied to all possible printable objects in the program -- in this example, place a border and color (draw the line of the object in it ).
Data related to a specific type of drawn object (such as the center and radius of the circle, the coordinates of the relative points of the rectangle, or the endpoint of a line) it should be declared in the specific class (derived from the abstract base class) corresponding to the plotting object of this type. Note that secondary derivation can be used to merge similar objects. For example, a circle can be generated from an ellipse, because all circles are elliptical. Similarly, a square can also be generated from a rectangle, because all squares are rectangles (also quadrilateral and polygon ). The selected derived tree reflects the relationships between classes and common expected usage modes, so that you can perform operations quickly and conveniently.
Because Constructor (New in Visual Basic) is mainly used to initialize data, constructor is not (and cannot be) polymorphism. This means that the initial creation operation cannot be polymorphism, because the data requirements vary with the type. However, after an object is created with a good design, the object can be processed as a polymorphism in subsequent use. Here we do this.
Let's take a look at what this class contains, starting with the root abstraction base class:
Abstract (MustInherit) base class
The following is the abstract code of the base class in C. Click here to view all source files in the new window.
C #
Public abstract class DShape {
Public abstract void Draw (Graphics g );
Protected Rectangle bounding;
Protected Color penColor; // attributes
// Methods such as moving and resizing should also be available.
}
The following is equivalent Visual Basic. NET code. Click here to view all source files in the new window.
Visual Basic. NET
Public MustInherit Class DShape
Public MustOverride Sub Draw (ByVal g As Graphics)
Protected bounding As Rectangle
Protected penColor As Color should also have attributes
Methods such as moving and resizing should also be available.
End Class
Although the syntax is different, it is obvious that this is the same class.
Note that the Draw method is implicitly virtual (Overridable) because it is declared as abstract (MustOverride ). Note that we do not provide an implementation in this class. Because we do not know the objects executed in this class, it is impossible to write drawing code.
What data does it contain?
Note that there is not much data here-but we have declared all the data for such an abstract class.
Each drawn object (regardless of its shape) has a border-that is, the smallest possible rectangle that can completely contain the object. The border is used to draw points (as small rectangles), rectangles, and circles-and for other shapes, it can be used as the first quick estimation for click or collision tests.
The other things that apply to all objects do not have much in common. The center is useful to some objects, such as circles and rectangles, and has no significance for other objects (such as triangles. In addition, the rectangle is usually represented by an angle instead of a center. However, you cannot use an angle to specify a circle because the circle has no angle. Dr. GUI is sure that you have seen the difficulty of specifying other data for a common printable object.
Each printable object also has a color associated with the line to draw it. Here we also declare it.
Some Derived classes
As mentioned above, we cannot really create an abstract base class object, although we can process any object derived from an abstract base class (or any base class) as a base class object.
Therefore, to create a drawing object, we must derive a new class from the abstract base class -- and ensure that all abstract/MustOverride methods are overwritten.
In this example, we will use the DHollowCircle class. The DHollowRectangle class is very similar to the DPoint class.
Below is DHollowCircle in C. Click here to view other classes in the new window.
C #
Public class DHollowCircle: DShape
{
Public DHollowCircle (Point p, int radius, Color penColor ){
P. Offset (-radius,-radius); // you need to convert it to the upper left corner.
Int diameter = radius * 2;
Bounding = new Rectangle (p, new Size (diameter, diameter ));
This. penColor = penColor;
}
Public override void Draw (Graphics g ){
Using (Pen p = new Pen (penColor )){
G. DrawEllipse (p, bounding );
}
}
}
The following are equivalent Visual Basic. NET classes. Click here to view other classes in the new window.
Visual Basic. NET
Public Class DHollowCircle
Inherits DShape
Public Sub New (ByVal p As Point, ByVal radius As Integer ,_
ByVal penColor As Color)
P. Offset (-radius,-radius) needs to be converted to the upper left corner
Dim diameter As Integer = radius * 2
Bounding = New Rectangle (p, New Size (diameter, diameter ))
Me. penColor = penColor
End Sub
Public Overrides Sub Draw (ByVal g As Graphics)
Dim p = New Pen (penColor)
Try
G. DrawEllipse (p, bounding)
Finally
P. Dispose ()
End Try
End Sub
End Class
Please note that we have not declared other data for this class-it provides enough borders and pens. (This is true for vertices and rectangles, but not for triangles and other polygon .) Our applications do not need to know the center or radius of the circle after setting the circle, so they are ignored. (If the center and radius are needed, we can store the data or calculate it based on the border .)
But we do need a border because it is a parameter used to draw the Graphics. DrawEllipse method of the circle. Therefore, the border is calculated based on the center point and radius passed in the constructor.
Next we will have a deep understanding of each method.