作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我会画圆,但不会画矩形或画线。你们能看出我缺少什么吗?
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Shapes
{
public abstract class Shapes
{
// these shapes are defined with four coordinates
protected int x1, y1, x2, y2;
// this method initializes the coordinates
public void setCoordinates(int x1, int y1, int x2, int y2)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
// abstract method to draw the shape
public abstract void drawShape(Graphics g);
public abstract void drawShapes(Graphics g);
} // end of Shapes
// class Circle derives from Shapes
public class Circle : Shapes
{
// no argument constructor
public Circle()
{
setCoordinates(0, 0, 0, 0);
}
// constructor with four arguments
public Circle(int a, int b, int w, int h)
{
setCoordinates(a, b, w, h);
}
public override void drawShape(Graphics g)
{
g.DrawEllipse(new Pen(Color.Green), x1, y1, x2, y2);
}
public override void drawShaper(Graphics q)
{
q.DrawRectangle(new Pen(Color.Green), x1, y1, x2, y2);
}
}
public class Rectangle : Shapes
{
// no argument constructor
public Rectangle()
{
setCoordinates(0, 0, 0, 0);
}
// constructor with four arguments
public Rectangle(int a, int b, int w, int h)
{
setCoordinates(a, b, w, h);
}
public override void drawShape(Graphics g)
{
g.DrawEllipse(new Pen(Color.Green), x1, y1, x2, y2);
}
public override void drawShaper(Graphics q)
{
q.DrawRectangle(new Pen(Color.Green), x1, y1, x2, y2);
}
}
// tests Shapes hierarchy
public class TestShapes : Form
{
private static Circle c;
private static int shape;
private static Rectangle r;
public static void Main()
{
// Here you can ask the user to enter the type and
// the coordinates for a shape
shape = 1;
c = new Circle(100, 100, 50, 50);
r = new Rectangle(100, 100, 50, 50);
// starts the application and makes the form visible
Application.Run(new TestShapes());
}
// this method executes automatically and paints the form
protected override void OnPaint(PaintEventArgs e)
{
// Get Graphics Object
Graphics g = e.Graphics;
// Draw text on the form
g.DrawString("Testing C# inheritance!", new Font("Arial", 18),
new SolidBrush(Color.Blue), 5, 5);
switch (shape)
{
case 1:
// draw a circle
c.drawShape(g);
break;
case 2:
r.drawShape(g);
// draw a rectangle
break;
case 3:
// draw a line
break;
}
}
}
}
最佳答案
像这样改变你的类:
public abstract class Shape
{
// ...
// abstract method to draw the shape
public abstract void DrawShape(Graphics g);
} // end of Shape
// class Circle derives from Shape
public class Circle : Shape
{
// ...
public override void DrawShape(Graphics g)
{
g.DrawEllipse(new Pen(Color.Green), x1, y1, x2, y2);
}
}
public class Rectangle : Shape
{
// ...
public override void DrawShape(Graphics g)
{
g.DrawRectangle(new Pen(Color.Green), x1, y1, x2, y2);
}
}
关于c# - 类继承与多态——绘制简单形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3314154/
我来自 Asp.Net 世界,试图理解 Angular State 的含义。 什么是 Angular 状态?它类似于Asp.Net中的ascx组件吗?是子页面吗?它类似于工作流程状态吗? 我听到很多人
我一直在寻找 3 态拨动开关,但运气不佳。 基本上我需要一个具有以下状态的开关: |开 |不适用 |关 | slider 默认从中间开始,一旦用户向左或向右滑动,就无法回到N/A(未回答)状态。 有人
我是一名优秀的程序员,十分优秀!