仅就你的说明发表评论。
两种多形态。
时间多变:
This is where your polymorphic behavior is defined prior to compilation.
This happens with method overloading e.g.
public bool DoSomeThing(int myVal)
{
// some code here
}
public bool DoSomeThing(double myVal)
{
// some code here
}
移动式多变:
This is where the polymorphic behavior happens at runtime.
An example of this would be a method that accepts a type that implements a specific interface (or common base class). This interface could be of a different type depending on the code that is executing.
e.g.
public interface IWeapon
{
void Strike();
}
public class Knife : IWeapon
{
public void Strike()
{
Console.WriteLine("Stab");
}
}
public class Gun : IWeapon
{
public void Strike()
{
Console.WriteLine("Shoot");
}
}
public class Warrior
{
public void Attack(IWeapon weapon)
{
weapon.Strike();
}
}
如今,如果向Warrior提供枪枝,攻击的产物将是舒特,而Knife的产物是Stab。
我没有测试过这一法典,因此可能出现同学错误,但你应当能够了解这一想法。