我有一个关于下课的问题。
Photo类继承自Form并具有图像,因此当我Photo根据其构造函数创建类的对象时,由于虚拟Drawer方法,它会绘制图像。
有一个BorderedPhoto类继承自Photo类,还有一个覆盖方法Drawer(),可以设置照片图像周围的边框。
该程序运行良好,但我的困惑是当我BorderedPhoto通过引用 Photo 类对象创建类对象时,为什么它被称为Drawer()Bordered 类的覆盖,即使它没有显式调用它。
class Photo:Form
{
Image image;
public Photo()
{
image= new Bitmap(@"C:\Jug_7.jpg");
this.Text = "My Water Jug";
this.Paint+= new PaintEventHandler(Drawer);
}
public virtual void Drawer(object source, PaintEventArgs e)
{
e.Graphics.DrawImage(image, 20, 20);
}
}
class BorderedPhoto : Photo
{
Photo photo;
Color color;
public BorderedPhoto(Photo p, Color c)
{
photo = p;
color = c;
}
public override void Drawer(object source, PaintEventArgs e)
{
base.Drawer(source, e);
e.Graphics.DrawRectangle(new Pen(color, 10), 25, 15, 215, 225);
}
}