Topics on Constructors and Methods
Topics on Constructors and Methods
Techniques of Creating and Using a Constructor
A Constructor with No Body
As mentioned for functions (and methods), if you are creating a small constructor that contains a single statement, you can replace the curly brackets with the => operator after the parentheses of the name of the constructor. This is followed by the necessary one-line statement.
Practical Learning: Introducing Constructors
namespace Geometry1.Models { internal class EquilateralTriangle { private double s; const double NumberOfSides = 3; public EquilateralTriangle() : this(0.00) { } public EquilateralTriangle(double side) { s = side; } public double CalculatePerimeter() { double dResult = 0; dResult = s * NumberOfSides; return dResult; } } }
using Geometry1.Models; using static System.Console; EquilateralTriangle et = new(); WriteLine("========================================"); WriteLine("Enter the value to process the triangle"); Write("Side: "); double side = double.Parse(ReadLine()); et = new EquilateralTriangle(side); WriteLine("========================================"); WriteLine("Geometry - Equilateral Triangle"); WriteLine("----------------------------------------"); WriteLine("Side: {0}", side); WriteLine("Perimeter: {0}", et.CalculatePerimeter()); Write("========================================");
======================================== Enter the value to process the triangle Side: 248.73 ======================================== Geometry - Equilateral Triangle ---------------------------------------- Side: 248.73 Perimeter: 746.1899999999999 ======================================== Press any key to close this window . . .
namespace Geometry1.Models { internal class EquilateralTriangle { private double s; const double NumberOfSides = 3; public EquilateralTriangle() : this(0.00) { } public EquilateralTriangle(double side) => s = side; public double CalculatePerimeter() => s * NumberOfSides; [ }
======================================== Enter the value to process the triangle Side: 8412.97 ======================================== Geometry - Equilateral Triangle ---------------------------------------- Side: 8412.97 Perimeter: 25238.909999999996 ======================================== Press any key to close this window . . .
namespace PlanarCurves3.Models { internal class Circle { public const double PI = 3.14; public double Radius { get; set; } public Circle() { Radius = 0.00; } public double CalculateDiameter() { return Radius * 2.00; } public double CalculateArea() { return Radius * Radius * PI; } } }
using PlanarCurves3.Models; using static System.Console; var round = new Circle(); WriteLine("====================================="); WriteLine("Enter the value to process the circle"); Write("Radius: "); round.Radius = double.Parse(ReadLine()); WriteLine("====================================="); WriteLine("Geometry - Circle Summary"); WriteLine("-------------------------------------"); WriteLine("Radius: {0}", round.Radius); WriteLine("Diameter: {0}", round.CalculateDiameter()); WriteLine("Area: {0}", round.CalculateArea()); Write("=====================================");
===================================== Enter the value to process the circle Radius: 248.73 ===================================== Geometry - Circle Summary ------------------------------------- Radius: 248.73 Diameter: 497.46 Area: 194261.16450599997 ===================================== Press any key to close this window . . .
When creating a field of a class, one of the decisions you must make is how the field would get its value(s). To create a field so that objects outside the class can only view its value but cannot change it while the value can be changed inside the class, create that field as read-only. To do this, when creating the field in the class, precede its data type with the readonly keyword. Here is an example:
public class Circle
{
readonly double PI;
}
You can mark the field with an access level such as private or public, depending on your intentions.
To specify the value of a read-only field, use a constructor, such as the default constructor, to initialize the field. Here is an example:
public class Circle { private double r; private readonly double PI; public Circle() { PI = 3.14; } }
The only difference between a constant variable and a read-only field is that a constant variable must be initialized when creating it. On the other hand, you can create more than one constructor in a class, then assign the same or a different value in each constructor to the read-only field. This means that, because you can initialize a read-only field in a constructor, if you have different constructors, you can also have different values for the same read-only field. Here are examples:
public class Circle { private double r; private readonly double PI; public Circle() { PI = 3.14; } public Circle(double rad) { r = rad; PI = 3.14159; } public double CalculateDiameter() { return r * 2; } public double CalculateArea() { return r * r * PI; } }
Instead of a constant value, the value of a read-only field can come from an expression. If the value held by a read-only field is gotten from an expression, then the field must be initialized in the(a) construction with the desired expression.
We know that a constant variable must be initialized when it is created. Although a read-only variable seems to follow the same rule, it doesn't. Remember that you don't need to initialize a read-only variable when you declare it since you can do this in the(a) constructor of the class. Also, because a constructor can be overloaded, a read-only field can hold different values depending on the particular constructor that is accessed at a particular time, but the value of a constant variable cannot change: it is initialized once, in the class (or in a method) and it keeps that value throughout the class (or method).
Practical Learning: Creating and Using a Read-Only Field
namespace PlanarCurves3.Models { public class Circle { public readonly double PI; public double Radius { get; set; } public Circle() { PI = 3.14; Radius = 0.00; } public double CalculateDiameter() { return Radius * 2.00; } public double CalculateArea() { return Radius * Radius * PI; } } }
using PlanarCurves3.Models;
using static System.Console;
var round = new Circle();
WriteLine("=====================================");
WriteLine("Enter the value to process the circle");
Write("Radius: ");
round.Radius = double.Parse(ReadLine());
WriteLine("=====================================");
WriteLine("Geometry - Circle Summary");
WriteLine("-------------------------------------");
WriteLine("Radius: {0}", round.Radius);
WriteLine("PI: {0}", round.PI);
WriteLine("Diameter: {0}", round.CalculateDiameter());
WriteLine("Area: {0}", round.CalculateArea());
Write("=====================================");
===================================== Enter the value to process the circle Radius: 5377.96 ===================================== Geometry - Circle Summary ------------------------------------- Radius: 5377.96 PI: 3.14 Diameter: 10755.92 Area: 90816504.811424 ===================================== Press any key to close this window . . .
namespace PlanarCurves3.Models
{
public class Circle
{
public readonly double PI;
public double Radius { get; set; }
public Circle()
{
PI = 3.14;
Radius = 0.00;
}
public Circle(double rad)
{
Radius = rad;
PI = 3.14159;
}
public double CalculateDiameter()
{
return Radius * 2.00;
}
public double CalculateArea()
{
return Radius * Radius * PI;
}
}
}
using PlanarCurves3.Models; using static System.Console; WriteLine("====================================="); WriteLine("Enter the value to process the circle"); Write("Radius: "); double rad = double.Parse(ReadLine()); var round = new Circle(rad); WriteLine("====================================="); WriteLine("Geometry - Circle Summary"); WriteLine("-------------------------------------"); WriteLine("Radius: {0}", round.Radius); WriteLine("PI: {0}", round.PI); WriteLine("Diameter: {0}", round.CalculateDiameter()); WriteLine("Area: {0}", round.CalculateArea()); Write("=====================================");
===================================== Enter the value to process the circle Radius: 1327.97 ===================================== Geometry - Circle Summary ------------------------------------- Radius: 1327.97 PI: 3.14159 Diameter: 2655.94 Area: 5540207.539496231 ===================================== Press any key to close this window . . .
namespace PlanarCurves3.Models { internal class Circle { public readonly double PI; public Circle() : this(0.00) { PI = 3.14; } public Circle(double rad) { Radius = rad; PI = 3.14159; } public double Radius { get; set; } public double CalculateDiameter() => Radius * 2.00; public double CalculateArea() => Radius * Radius * PI; } }
===================================== Enter the value to process the circle Radius: 1003.77 ===================================== Geometry - Circle Summary ------------------------------------- Radius: 1003.77 PI: 3.14159 Diameter: 2007.54 Area: 3165322.2397045107 ===================================== Press any key to close this window . . .
Using a Reference of a Class Without its Object
Introduction
So far, to create an object, we had to declare a variable of the desired class. A variable, or object, is necessary only if you are planning to use the object more than once. If you need a reference to the object only once (or just a few times), you don't need a variable. In this case, in the section of code where you want the reference, simply type the new keyword followed by the name of the class. Since you are calling a constructor of the class, you must add parentheses. Here is an example:
public class Triangle
{
public double Width { get; set; }
public double Depth { get; set; }
public double CalculateArea() => Width * Depth / 2.00;
}
. . .
new Triangle();
In the same way, you can call such a reference as many times as you want. Since you are getting an object that is a reference to the class, you can then access any public or internal members of that class. Here are examples that access a field, a property, and methods of the class:
using PlanarCurves3.Models; using static System.Console; WriteLine("====================================="); WriteLine("Enter the value to process the circle"); Write("Radius: "); double rad = double.Parse(ReadLine()); WriteLine("====================================="); WriteLine("Geometry - Circle Summary"); WriteLine("-------------------------------------"); WriteLine("Radius: {0}", new Circle(rad).Radius); WriteLine("PI: {0}", new Circle(rad).PI); WriteLine("Diameter: {0}", new Circle(rad).CalculateDiameter()); WriteLine("Area: {0}", new Circle(rad).CalculateArea()); Write("=====================================");
To make your code easy to read, you should put the new operator and the name of the class in parentheses. Here are examples:
using PlanarCurves3; using static System.Console; WriteLine("====================================="); WriteLine("Enter the value to process the circle"); Write("Radius: "); double rad = double.Parse(ReadLine()); WriteLine("====================================="); WriteLine("Geometry - Circle Summary"); WriteLine("-------------------------------------"); WriteLine("Radius: {0}", (new Circle(rad)).Radius); WriteLine("PI: {0}", (new Circle(rad)).PI); WriteLine("Diameter: {0}", (new Circle(rad)).CalculateDiameter()); WriteLine("Area: {0}", (new Circle(rad)).CalculateArea()); Write("=====================================");
Returning a New Object
As done for a value of a regular type, you can return an object value from a method of a class. To do this, you can first declare the method and specify the class as the return type. Here is an example:
public class Triangle
{
public double Width { get; set; }
public double Depth { get; set; }
public Triangle(double w, double d)
{
Width = w;
Depth = d;
}
public double CalculateArea() => Width * Depth / 2.00;
}
public class Shape
{
private Triangle Create()
{
double width = 2834.208;
double depth = 602.415;
return new Triangle(width, depth);
}
}
A Class as a Type
A Field of a Class Type
Just like a primitive type can be used to describe an object, a class can play that role too. To make this happen, create a member that uses a class as its type. You can use any class, including one you create yourself. Here is an example:
class Rectangle
{
double _width_;
double _height_;
}
class Box
{
Rectangle face;
}
You can mark the field with an access level such as private, public or internal, etc.
Of course, the class can also have members (fields and properties) of any type. Here is an example:
public class Rectangle
{
double _width_;
double _height_;
}
public class Box
{
double _depth_;
Rectangle _face_;
}
If a member of a class A (for example, the above Box class) is of a class type B (for example, the above Rectangle class):
public class Rectangle
{
double _width_;
double _height_;
}
public class Box
{
double _depth_;
Rectangle _face_ = new Rectangle();
}
public class Rectangle
{
double _width_;
double _height_;
}
public class Box
{
double _depth_;
Rectangle _face_;
public void Initialize()
{
_face_ = new Rectangle();
}
}
If you use this technique, make sure you call that method before using the member. This can be done as follows:
public class Exercise
{
private void Describe()
{
Box rectangular = new Box();
rectangular.Initialize();
}
}
public class Rectangle
{
double _width_;
double _height_;
}
public class Box
{
double _depth_;
Rectangle _face_;
public void Initialize()
{
_face_ = new Rectangle();
}
}
public class Exercise
{
private void Describe()
{
Box rectangular = new Box();
rectangular._face_ = new Rectangle();
}
}
public class Rectangle
{
double _width_;
double _height_;
}
public class Box
{
double _depth_;
Rectangle _face_;
}
A Property of a Class Type
The data type of a property can be a class. If you want to create a complete property, you can first create a field as we saw in the previous sections. In the get accessor of the property, you can return the field. In the set accessor, you assign value to the field. Here is an example:
public class Point
{
public int X { get; set; }
public int Y { get; set; }
public Point(int x, int y)
{
X = x;
Y = y;
}
}
public class Equation
{
private Point _pt_;
public Point Operand
{
get
{
return _pt_;
}
set
{
_pt_ = value;
}
}
}
In earlier sections, we saw that you should/must initialize the field, which you can do in a constructor for the class. After that, you can use the property. To access a member of the class of the property, you can use the period operator. Here is an example:
public class Point { public int X { get; set; } public int Y { get; set; } public Point(int x, int y) { X = x; Y = y; } } public class Equation { private Point _pt_; public Equation() { _pt_ = new Point(0, 0); } public Point Operand { get { return _pt_; } set { _pt_ = value; } } }
After that, you can use the property. To access a member of the class of the property, you can use the period operator. Here is an example:
using static System.Console; using CoordinateSystem; Algebra.Equation eq = new Algebra.Equation(2, 3, 18); WriteLine("Equation: {0}", eq.Create()); WriteLine("-----------------------------"); eq.Operand = new Point(3, -5); eq.Result = -10; WriteLine("Equation: {0}", eq.Create()); WriteLine("-----------------------------"); eq.Operand.X = 5; eq.Operand.Y = -3; eq.Result = -15; WriteLine("Equation: {0}", eq.Create()); WriteLine("============================="); namespace CoordinateSystem { public class Point { public int X { get; set; } public int Y { get; set; } public Point(int x, int y) { X = x; Y = y; } } } namespace Algebra { public class Equation { private Point _pt_; public Equation(int factor1, int factor2, int result) { Result = result; _pt_ = new Point(factor1, factor2); } public Point Operand { get { return _pt_; } set { _pt_ = value; } } public int Result { get; set; } public string Create() { if( _pt_.Y is >= 0 ) return _pt_.X + "x + " + _pt_.Y + "y = " + Result; return _pt_.X + "x - " + (_pt_.Y * -1) + "y = " + Result; } } }
This would produce:
Equation: 2x + 3y = 18 ----------------------------- Equation: 3x - 5y = -10 ----------------------------- Equation: 5x - 3y = -15 ============================= Press any key to close this window . . .
A Property or Field with the Same Type and Name
A property or a field of a class type can use the same name as the class. Here is an example for a field:
class Employee
{
}
class TimeSheet
{
private double timeWorked;
private string filingStatus;
public Employee Employee;
}
In this case, the compiler would know that the first name represents a type and the second name represents a member of the class. In the methods of the class, when you use the name as variable, the compiler would know that you are accessing the member.
Practical Learning: Using a Property of a Class Type
namespace Volumetrics1.Models { public class Rectangle { public double Width { get; set; } public double Height { get; set; } } }
namespace Volumetrics1 { public class Box { public double Depth { get; set; } public Rectangle Face { get; set; } } }
using Volumetrics1.Models; using static System.Console; Box rectangular = new Box(); rectangular.Face = new Rectangle(); WriteLine("====================================="); WriteLine("Enter the value to process the box"); Write("Width: "); double width = double.Parse(ReadLine()); Write("Height: "); double height = double.Parse(ReadLine()); Write("Depth: "); double depth = double.Parse(ReadLine()); rectangular.Face.Width = width; rectangular.Face.Height = height; rectangular.Depth = depth; double frontBackPerimeter = (rectangular.Face.Width + rectangular.Face.Height) * 2; double leftRightPerimeter = (rectangular.Face.Height + rectangular.Depth) * 2; double topBottomPerimeter = (rectangular.Face.Width + rectangular.Depth) * 2; WriteLine("====================================="); WriteLine("Geometry - Box Summary"); WriteLine("-------------------------------------"); WriteLine("Width: {0}", rectangular.Face.Width); WriteLine("Height: {0}", rectangular.Face.Height); WriteLine("Depth: {0}", rectangular.Depth); WriteLine("====================================="); WriteLine("Perimeters"); WriteLine("-------------------------------------"); WriteLine("Front Back: {0}", frontBackPerimeter); WriteLine("Left Right: {0}", leftRightPerimeter); WriteLine("Top Bottom: {0}", topBottomPerimeter); WriteLine("=====================================");
===================================== Enter the value to process the box Width: 137.58 Height: 69.33 Depth: 88.97 ===================================== Geometry - Box Summary ------------------------------------- Width: 137.58 Height: 69.33 Depth: 88.97 ===================================== Perimeters ------------------------------------- Front Back: 413.82000000000005 Left Right: 316.6 Top Bottom: 453.1 ===================================== Press any key to close this window . . .
|
|||
Previous | Copyright © 2001-2024, C# Key | Saturday 29 April 2023 | Next |
|