Implementing and Using an Abstract Class Visual C Sharp 2010


Implementing and Using an Abstract Class Visual C Sharp 2010

1 . . Return to the Drawing project in Visual Studio .
Note  A finished working copy of the previous exercise is available in the Drawing   project
located in the \Microsoft Press\Visual CSharp Step By Step\Chapter 13\Drawing Using
Interfaces - Complete folder in your Documents folder .
2 . . On the Project menu, click Add Class .
The Add New Item – Drawing dialog box appears .
3 . . In the Name text box, type DrawingShape.cs, and then click Add .
Visual Studio creates the file and displays it in the Code and Text Editor window .
4 . . In the DrawingShape .cs file, add the following using statements to the list at the top:
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Controls;
5 . . The purpose of this class is to contain the code common to the Circle and Square class-
es . A program should not be able to instantiate a DrawingShape object directly . Modify
the definition of the DrawingShape class, and declare it as abstract, as shown here in
bold:

abstract class DrawingShape
{
}
6 . . Add the private variables shown in bold to the DrawingShape class:
abstract class DrawingShape
{
    protected int size;
    protected int locX = 0, locY = 0;
    protected Shape shape = null;
}
The Square and Circle classes both use the locX and locY fields to specify the location of
the object on the canvas, so you can move these fields to the abstract class . Similarly,
the Square and Circle classes both used a field to indicate the size of the object when it
was rendered; although it has a different name in each class (sideLength and radius), se-
mantically the field performed the same task in both classes . The name “size” is a good
abstraction of the purpose of this field .
Internally, the Square class uses a Rectangle object to render itself on the canvas, and
the Circle class uses an Ellipse object . Both of these classes are part of a hierarchy based
on the abstract Shape class in the  .NET Framework . The DrawingShape class uses a
Shape field to represent both of these types .
7 . . Add the following constructor to the DrawingShape class:
public DrawingShape(int size)
{
    this.size = size;
}
This code initializes the size field in the DrawingShape object .
8 . . Add the SetLocation and SetColor methods to the DrawingShape class, as shown in
bold . These methods provide implementations that are inherited by all classes that
derive from the DrawingShape class . Notice that they are not marked as virtual, and
a derived class is not expected to override them . Also, the DrawingShape class is not
declared as implementing the IDraw or IColor interfaces (interface implementation is
a feature of the Square and Circle classes and not this abstract class), so these methods
are simply declared as public .
abstract class DrawingShape
{
    ...
    public void SetLocation(int xCoord, int yCoord)
    {
        this.locX = xCoord;
        this.locY = yCoord;
    }

    public void SetColor(Color color)
    {
        if (shape != null)
        {
            SolidColorBrush brush = new SolidColorBrush(color);
            shape.Fill = brush;
        }
    }
}
9 . . Add the Draw method to the DrawingShape class . Unlike the previous methods, this
method is declared as virtual, and any derived classes are expected to override it to ex-
tend the functionality . The code in this method verifies that the shape field is not null,
and then draws it on the canvas . The classes that inherit this method must provide thei
own code to instantiate the shape object . (Remember that the Square class creates a
Rectangle object and the Circle class creates an Ellipse object .)
abstract class DrawingShape
{
    ...
    public virtual void Draw(Canvas canvas)
    {
        if (this.shape == null)
        {
            throw new ApplicationException(“Shape is null”);
        }

        this.shape.Height = this.size;
        this.shape.Width = this.size;
        Canvas.SetTop(this.shape, this.locY);
        Canvas.SetLeft(this.shape, this.locX);
        canvas.Children.Add(shape);
    }
}
You have now completed the DrawingShape abstract class . The next step is to change the
Square and Circle classes so that they inherit from this class, and remove the duplicated code
from the Square and Circle classes .


No comments:

Post a Comment