مشخصات مقاله
-
4238
-
0.0
-
8610
-
0
-
0
کلاس به عنوان یک گونه داده ای (class as a data type)
همانطور که در آغاز بحث شی گرایی گفتیم، در سی شارپ یک کلاس در واقع یک گونه داده ای است که شی ساخته شده از آن همان متغیری است که آن گونه داده ای را دارد. پس به سادگی می توان گفت که همانند سایر گونه های داده ای پایه، می توان در زبان سی شارپ با کلاس های موجود در بستر دات نت و نیز کلاس های تعریف شده خودمان به عنوان یک گونه داده ای برخورد نمود.
ی توان مقدار یک کلاس را از متد یک کلاس بازگرداند. برای این منظور، ابتدا متد را تعریف کرده، سپس کلاس را به عنوان نوع بازگشتی (return type) مشخص می کنید.
public class Point
{
internal short x;
internal short y;
}
public class CoordinateSystem
{
private Point start;
private Point end;
public Point GetThePoint()
{
}
}
پس از پیاده سازی (implementing) متد، باید مقداری بازگردانید که با کلاس همخوانی داشته باشد، در غیر این صورت حین ترجمه / کامپایل برنامه با error مواجه می شوید. بنابراین، ابتدا متغیر کلاس را در بدنه ی متد تعریف می کنید، در این خلال متغیر را مقداردهی اولیه می کنید، سپس آن را باز می گردانید.
مثال:
public class Point
{
internal short x;
internal short y;
}
public class CoordinateSystem
{
private Point start;
private Point end;
public Point GetThePoint()
{
}
}
پس از این که متد مقدار کلاس را بازگرداند، می توانید آن را طبق نیاز خود مورد استفاده قرار دهید.
ارسال کلاس به عنوان آرگومان
پس از ایجاد کلاس، می توان آن را مثل هر متغیر دیگری به کار برد. برای مثال، می توان متغیر آن را به عنوان آرگومان به متد کلاس دیگری ارسال کرد. زمانی که کلاسی به عنوان آرگومان ارسال می شود، اعضای عمومی (public members) کلاس در اختیار متدی که از آن استفاده می کند قرار می گیرد. همچنین می توان چندین (بیش از یک) کلاس به عنوان آرگومان به یک متد ارسال کرد.
مثال:
using System;
namespace Geometry
{
public class Point
{
internal short x;
internal short y;
}
public class CoordinateSystem
{
public Point start;
public Point end;
public Point GetThePoint()
{
Point pt = new Point();
Console.Write("Enter the x coordinate of the point: ");
pt.x = short.Parse(Console.ReadLine());
Console.Write("Enter the y coordinate of the point: ");
pt.y = short.Parse(Console.ReadLine());
return pt;
}
public double DistanceFromOrigin(Point pt)
{
double sqr1 = Math.Pow(pt.x، 2);
double sqr2 = Math.Pow(pt.y، 2);
double distance = Math.Sqrt(sqr1 + sqr2);
return distance;
}
public double DistanceBetween2Points(Point pt1، Point pt2)
{
double sqr1 = Math.Pow(pt2.x - pt1.x، 2);
double sqr2 = Math.Pow(pt2.y - pt1.y، 2);
double distance = Math.Sqrt(sqr1 + sqr2);
return distance;
}
}
public class Program
{
private static CoordinateSystem IdentifyCoordinates()
{
CoordinateSystem coord = new CoordinateSystem();
Console.WriteLine("Start Point");
coord.start = coord.GetThePoint();
Console.WriteLine("End Point");
coord.end = coord.GetThePoint();
return coord;
}
private static void Show(CoordinateSystem c)
{
Console.WriteLine("Coordinate System");
Console.WriteLine("Starting Point: P({0}، {1})"، c.start.x، c.start.y);
Console.WriteLine("Ending Point: Q({0}، {1})"، c.end.x، c.end.y);
Console.WriteLine("Distance Between Both Points: {0:F}"،
c.DistanceBetween2Points(c.start، c.end));
}
static int Main()
{
CoordinateSystem coord = IdentifyCoordinates();
Console.WriteLine();
Show(coord);
Console.ReadKey();
return 0;
}
}
}
نتیجه
Start Point Enter the x coordinate of the point: -2 Enter the y coordinate of the point: 2 End Point Enter the x coordinate of the point: 3 Enter the y coordinate of the point: -6 Coordinate System Starting Point: P(-2، 2) Ending Point: Q(3، -6) Distance Between Both Points: 9.43 Press any key to continue...
به این خاطر که کلاس ها همیشه به عنوان ارجاع (reference) مورد استفاده قرار می گیرند، هنگام ارسال کلاس به عنوان آرگومان، کلاس نام برده (به صورت ضمنی) با ارجاع فرستاده می شود. برای این منظور می توانید کلیدواژه ی ref را در سمت چپ آرگومان تایپ کنید.
مثال:
using System;
namespace ConsoleApplication1
{
public class Point
{
internal short x;
internal short y;
}
public class CoordinateSystem
{
public Point start;
public Point end;
public Point GetThePoint()
{
Point pt = new Point();
Console.Write("Enter the x coordinate of the point: ");
pt.x = short.Parse(Console.ReadLine());
Console.Write("Enter the y coordinate of the point: ");
pt.y = short.Parse(Console.ReadLine());
return pt;
}
public double DistanceFromOrigin(ref Point pt)
{
double sqr1 = Math.Pow(pt.x, 2);
double sqr2 = Math.Pow(pt.y, 2);
double distance = Math.Sqrt(sqr1 + sqr2);
return distance;
}
public double DistanceBetween2Points(ref Point pt1, ref Point pt2)
{
double sqr1 = Math.Pow(pt2.x - pt1.x, 2);
double sqr2 = Math.Pow(pt2.y - pt1.y, 2);
double distance = Math.Sqrt(sqr1 + sqr2);
return distance;
}
}
public class Program
{
private static CoordinateSystem IdentifyCoordinates()
{
CoordinateSystem coord = new CoordinateSystem();
Console.WriteLine("Start Point");
coord.start = coord.GetThePoint();
Console.WriteLine("End Point");
coord.end = coord.GetThePoint();
return coord;
}
private static void Show(CoordinateSystem c)
{
Console.WriteLine("Coordinate System");
Console.WriteLine("Starting Point: P({0}, {1})", c.start.x, c.start.y);
Console.WriteLine("Ending Point: Q({0}, {1})", c.end.x, c.end.y);
Console.WriteLine("Distance Between Both Points: {0:F}",
c.DistanceBetween2Points(ref c.start, ref c.end));
}
static int Main()
{
CoordinateSystem coord = IdentifyCoordinates();
Console.WriteLine();
Show(coord);
Console.ReadKey();
return 0;
}
}
}