نوشته شده توسط
deldadeh108
شما هر موضوعی که انتخاب کنی میتونی از مفاهیم شی گرایی استفاده کنی. تو این کلاس میتونی چند متد با اسم یکسان و پارامترهای متفاوت داشته باشی که میشه همون پلی مورفیسم.
اين مفهوم پلی مورفیسمه؟! این که شما نوشتین Method Overloading هست نه Polymorphism.
Polymorphism به نقل از کتاب جوزف البهاری:
public class Asset
{
public string Name;
}
public class Stock : Asset // inherits from Asset
{
public long SharesOwned;
}
public class House : Asset // inherits from Asset
{
public decimal Mortgage;
}
References are polymorphic. This means a variable of type x can refer to an object
that subclasses x. For instance, consider the following method:
public static void Display (Asset asset)
{
System.Console.WriteLine (asset.Name);
}
This method can display both a Stock and a House, since they are both Assets:
Stock msft = new Stock ... ;
House mansion = new House ... ;
Display (msft);
Display (mansion);
Polymorphism works on the basis that subclasses (Stock and House) have all the
features of their base class (Asset). The converse, however, is not true. If Display
was modified to accept a House, you could not pass in an Asset:
static void Main() { Display (new Asset()); } // Compile-time error
public static void Display (House house) // Will not accept Asset
static void Main() { Display (new Asset()); } // Compile-time error
public static void Display (House house) // Will not accept Asset
{
{