我有以下课程:
class Polygon
{
protected string name;
protected float width, height;
public Polygon(string theName, float theWidth, float theHeight)
{
name = theName;
width = theWidth;
height = theHeight;
}
public virtual float calArea()
{
return 0;
}
}
class Rectangle : Polygon
{
public Rectangle(String name, float width, float height) : base(name,width,height)
{
}
public override float calArea()
{
return width * height;
}
}
主要功能1:
static void Main(string[] args)
{
Rectangle rect1 = new Rectangle("Rect1", 3.0f, 4.0f);
float Area = rect1.calArea()
}
主要功能2:
static void Main(string[] args)
{
Polygon poly = new Rectangle("Rect1", 3.0f, 4.0f);
float Area = poly.calArea()
}
我了解主要功能 2 使用动态绑定。
如果我在 Rectangle 类的 calArea 方法中将 override 关键字更改为 new,则它是静态绑定。主要功能1呢?它使用静态/动态绑定吗?