مشخصات مقاله
-
1672
-
0.0
-
5747
-
0
-
0
آموزش وراثت در سی شارپ
کلیه حقوق مادی و معنوی این مقاله متعلق به آموزشگاه تحلیل داده می باشد و هر گونه استفاده غیر قانونی از آن پیگرد قانونی دارد.
آموزش وراثت در سی شارپ
یکی از مهم ترین مفاهیم در برنامه نویسی آبجکت محور مربوط می شود به inheritance. در واقع inheritance به ما اجازه می دهد تا یک گروه را در ترم های گروه دیگری تعریف کنیم که ایجاد و اجرای یک برنامه را آسان تر می کند. همچنین این فرصت را فراهم می کند تا از قابلیت برنامه و زمان اجرای سریع دوباره استفاده کنیم.
در هنگام ایجاد یک گروه به جای نوشتن اعضای جدید گروه وعملکردها عضو به طور کامل، برنامه نویس می تواند تعیین کند که گروه جدید باید اعضا را از یک گروه موجود بگیرد. این گروه موجود base class(گروه پایه) نامیده می شود و گروه جدید به عنوان derived class (گروه مشتق) اشاره می شود.
نظریه ی inheritance ارتباط IS-A را اجرا می کند، به عنوان مثال mammal IS A animal , dog IS-A mamal hence dog IS-A animal و به همین شکل.
گروه های base و Derived
یک گروه می تواند از بیشتر ازیک گروه یا اینترفیس استخراج شود که بدین معناست که می تواند داده و عملکردهایی را از چند گروه پایه و اینترفیس بگیرد.
ترکیب استفاده شده برای ایجاد گرو های derived در C# مانند زیر می باشد.
class
{
...
}
class :
{
...
}
به base class Shape و derived class Rectangle توجه داشته باشید.
using System;
namespace InheritanceApplication
{
class Shape
{
public void setWidth(int w)
{
width = w;
}
public void setHeight(int h)
{
height = h;
}
protected int width;
protected int height;
}
// Derived class
class Rectangle: Shape
{
public int getArea()
{
return (width * height);
}
}
class RectangleTester
{
static void Main(string[] args)
{
Rectangle Rect = new Rectangle();
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
Console.WriteLine("Total area: {0}", Rect.getArea());
Console.ReadKey();
}
}
}
وقتی که برنامه ی بالا کامپایل شده و اجرا می شود نتیجه زیر را به دنبال دارد:
Total area: 35
Base Class Initialization:
گروه مشتق شده (derived class) متغیرهای عضو گروه اصلی و متودهای عضو را می گیرد. بنابراین آبجکت گروه ممتاز (super class) باید قبل ایجاد گروه جانشین، ایجاد شود. شما می توانید دستوراتی برای superclass initialization در لیست member initialization بدهید.
برنامه ی زیر این مطلب را توضیح می دهد.
using System;
namespace RectangleApplication
{
class Rectangle
{
//member variables
protected double length;
protected double width;
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}//end class Rectangle
class Tabletop : Rectangle
{
private double cost;
public Tabletop(double l, double w) : base(l, w)
{ }
public double GetCost()
{
double cost;
cost = GetArea() * 70;
return cost;
}
public void Display()
{
base.Display();
Console.WriteLine("Cost: {0}", GetCost());
}
class ExecuteRectangle
{
static void Main(string[] args)
{
Tabletop t = new Tabletop(4.5, 7.5);
t.Display();
Console.ReadLine();
}
}
}
وقتی که برنامه ی بالا کامپایل شده و اجرا می شود، نتایج زیر را به دنبال دارد.
Length: 4.5
Width: 7.5
Area: 33.75
Cost: 2362.5
Multiple Inheritance در C#
C# برنامه multiple inheritance را پشتیبانی نمی کند. به هرحال می توانید از اینترفیس هایی برای اجرای multiple inheritance استفاده کنید.
برنامه ی زیر این مطلب را توضیح می دهد.
using System;
namespace InheritanceApplication
{
class Shape
{
public void setWidth(int w)
{
width = w;
}
public void setHeight(int h)
{
height = h;
}
protected int width;
protected int height;
}
// Base class PaintCost
public interface PaintCost
{
int getCost(int area);
}
// Derived class
class Rectangle : Shape, PaintCost
{
public int getArea()
{
return (width * height);
}
public int getCost(int area)
{
return area * 70;
}
}
class RectangleTester
{
static void Main(string[] args)
{
Rectangle Rect = new Rectangle();
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
// Print the area of the object.
Console.WriteLine("Total area: {0}", Rect.getArea());
Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
Console.ReadKey();
}
}
}
وقتی که برنامه ی بالا کامپایل شده و اجرا میشود، نتایج زیر را به دنبال دارد.
Total area: 35
Total paint cost: $2450