Introduction to Generic Classes and Inheritance
Consider the following geometric figures:
Square | Rectangle | Trapezoid | Parallelogram |
Notice that these are geometric figures with each having
four sides. From what we know so far, we can create a base class to prepare
it for inheritance. If the class is very general, we can make it a generic
one. We can set a data type as an unknown type, anticipating that the
dimensions of the figure can be considered as integer or double-precision
types. Here is an example:
using System; public class Quadrilateral{ protected T _base; protected T _height; protected string _name; public virtual T Base { get { return _base; } set { _base = value; } } public virtual T Height { get { return _height; } set { _height = value; } } public virtual string Name { get { return _name; } set { _name = value; } } public Quadrilateral(string name = "Quadrilateral") { _name = name; } public Quadrilateral(T bs, T height) { _name = "Quadrilateral"; _base = bs; _height = height; } public Quadrilateral(string name, T bs, T height) { _name = name; _base = bs; _height = height; } public virtual string Describe() { return "A quadrilateral is a geometric figure with four sides"; } public virtual void ShowCharacteristics() { Console.WriteLine("Geometric Figure: {0}", Name); Console.WriteLine("Description: {0}", Describe()); Console.WriteLine("Base: {0}", Base); Console.WriteLine("Height: {0}", Height); } } public class Exercise { static int Main() { // Trapezoid with equal sides var Kite = new Quadrilateral ("Beach Kite", 18.64, 18.64); Kite.ShowCharacteristics(); Console.WriteLine(); // Rectangle, in meters var BasketballStadium = new Quadrilateral (); BasketballStadium.Name = "Basketball Stadium"; BasketballStadium.Base = 15; BasketballStadium.Height = 28; BasketballStadium.ShowCharacteristics(); Console.WriteLine(); return 0; } }
This would produce:
Geometric Figure: Beach Kite Description: A quadrilateral is a geometric figure with four sides Base: 18.64 Height: 18.64 Geometric Figure: Basketball Stadium Description: A quadrilateral is a geometric figure with four sides Base: 15 Height: 28 Press any key to continue . . .
If you have a generic class that can serve as a
foundation for another class, you can derive a class from the generic one.
To do this, use the formula we apply when deriving a class but follow the
name of each class with <>. Inside of the <> operator, enter the same
identifier to indicate that the class is a generic type that is based on
another generic class. Here is an example:
public class Square: Quadrilateral { }
In the body of the new class, you can use the parameter
type as you see fit. For example, you can declare some member variables of
that type. You can create methods that return the parameter type or you can
pass arguments of the parameter type. When implementing the methods of the
new class, use the member variables of the parameter and the argument(s)
based on the parameter type as you see fit. You can then declare a variable
of the class and use it as we done so far for other generic classes. Here is
an example:
using System; public class Quadrilateral{ protected T _base; protected T _height; protected string _name; public virtual T Base { get { return _base; } set { _base = value; } } public virtual T Height { get { return _height; } set { _height = value; } } public virtual string Name { get { return _name; } set { _name = value; } } public Quadrilateral(string name = "Quadrilateral") { _name = name; } public Quadrilateral(T bs, T height) { _name = "Quadrilateral"; _base = bs; _height = height; } public Quadrilateral(string name, T bs, T height) { _name = name; _base = bs; _height = height; } public virtual string Describe() { return "A quadrilateral is a geometric figure with four sides"; } public virtual void ShowCharacteristics() { Console.WriteLine("Geometric Figure: {0}", Name); Console.WriteLine("Description: {0}", Describe()); Console.WriteLine("Base: {0}", Base); Console.WriteLine("Height: {0}", Height); } } public class Square : Quadrilateral { public Square() { _name = "Square"; } public Square(string name) { _name = "Square"; } public Square(T side) { _name = "Square"; _base = side; _height = side; } public Square(string name, T side) { _name = name; _base = side; _height = side; } public override string Describe() { return "A square is a quadrilateral with four equal sides"; } public override void ShowCharacteristics() { Console.WriteLine("Geometric Figure: {0}", Name); Console.WriteLine("Description: {0}", Describe()); Console.WriteLine(" {0}", Describe()); Console.WriteLine("Side: {0}", Base); } } public class Exercise { static int Main() { // Rectangle, in meters var plate = new Square (); plate.Name = "Plate"; plate.Base = 15; plate.Height = 28; plate.ShowCharacteristics(); Console.WriteLine(); return 0; } }
This would produce:
Geometric Figure: Plate Description: A quadrilateral is a geometric figure with four sides A square is a quadrilateral with four equal sides Side: 15 Press any key to continue . . .
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment