1.题目要求如下:
用C#设计一个 Windows应用程序,在该程序定义平面图形抽象类和其派生类圆、矩形和三角形。该程序实现的功能包括:输入相应图形的参数,如矩形的长和宽,单击相应的按钮,枓据输入參数创建图形类并输出该图形的面积。程序运行结果如图所示
2.来吧展示,代码如下:
using System;
using System.Windows.Forms;
namespace Different_Graphic_Areas_4._2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Circle c = new Circle(Convert.ToDouble(textBox1.Text));
richTextBox1.Text = "圆的面积为:" + c.Area();
}
private void button2_Click(object sender, EventArgs e)
{
Trangle t = new Trangle(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text));
richTextBox1.Text = "矩形的面积为:" + t.Area();
}
private void button3_Click(object sender, EventArgs e)
{
S s = new S(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text));
richTextBox1.Text = "三角形的面积为:" + s.Area();
}
public abstract class Figure
{
public abstract double Area();
}
public class Circle:Figure
{
double r;
public Circle(double r)
{
this.r = r;
}
public override double Area()
{
return r * r * 3.14;
}
}
public class Trangle:Figure
{
double h;
double w;
public Trangle(double h,double w)
{
this.h = h;
this.w = w;
}
public override double Area()
{
return h*w;
}
}
public class S:Figure
{
double h;
double w;
public S(double h,double w)
{
this.h = h;
this.w = w;
}
public override double Area()
{
return w * h/2;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
3.运行结果如下:
我是小关,关注我,带你从初级入门编程
希望能帮到大家,问你们要一个赞,你们会给吗,谢谢大家
版权声明:本文版权归作者(@攻城狮小关)和CSDN共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
大家写文都不容易,请尊重劳动成果~
交流加Q:1909561302
博客园地址https://www.cnblogs.com/guanguan-com/