using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;
namespace 习题7.____14题___几何图形面积{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } //定义抽象基类 public abstract class Shape { //声明字段 protected double s1; protected double s2;
public Shape(double a1,double a2) //构造函数 { s1 = a1; s2 = a2; }
public abstract double Cubage(); //定义抽象方法 }
//定义派生类 Cycle public class Cycle : Shape { public Cycle(double radius) : base(radius, 0) { } //调用构造函数 public double Radius { get { return s1; } set { s1 = value; } } //声明属性 public override double Cubage() //重载抽象方法 { //throw new Exception("The method or operation is not implemented."); return 3.14 * s1 * s1; } }
//定义派生类 Cuboid public class Cuboid : Shape { public Cuboid(double l, double w):base(l,w) { } //调用构造函数 public double L { get { return s1; } set { s1 = value; } } public double W { get { return s2; } set { s2 = value; } } //声明属性 public override double Cubage() //重载抽象方法 { //throw new Exception("The method or operation is not implemented."); return s1 * s2; } }
//定义派生类 Triangle public class Triangle : Shape { public Triangle(double bottom, double high) : base(bottom, high) { } //调用构造函数 public double Bottom { get { return s1; } set { s1 = value; } } //声明属性 public double High { get { return s2; } set { s2 = value; } } public override double Cubage() //重载抽象方法 { //throw new Exception("The method or operation is not implemented."); return s1 * s2 / 2; } } private void Form1_Load(object sender, EventArgs e) { label1.Text = " 圆半径:\n 矩形长:\n三角形底:\n"; label2.Text = " 矩形宽:\n三角形高:"; label3.Text = ""; button1.Text = "计算圆面积"; button2.Text = "计算矩形面积"; button3.Text = "计算三角形面积"; button4.Text = "退 出"; }
private void button1_Click(object sender, EventArgs e) { double radius = double.Parse(textBox1.Text);
Cycle cycle = new Cycle(radius); label3.Text = "圆的面积为:" + cycle.Cubage(); }
private void button2_Click(object sender, EventArgs e) { double l = double.Parse(textBox1.Text); double w = double.Parse(textBox2.Text);
Cuboid cuboid = new Cuboid(l,w); label3.Text = "长方形的面积为:" + cuboid.Cubage(); }
private void button3_Click(object sender, EventArgs e) { double bottom = double.Parse(textBox1.Text); double high = double.Parse(textBox2.Text);
Triangle triangle = new Triangle(bottom,high); label3.Text = "三角形的面积为:" + triangle.Cubage(); }
private void button4_Click(object sender, EventArgs e) { Application.Exit(); } }}
namespace 习题7.____14题___几何图形面积{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } //定义抽象基类 public abstract class Shape { //声明字段 protected double s1; protected double s2;
public Shape(double a1,double a2) //构造函数 { s1 = a1; s2 = a2; }
public abstract double Cubage(); //定义抽象方法 }
//定义派生类 Cycle public class Cycle : Shape { public Cycle(double radius) : base(radius, 0) { } //调用构造函数 public double Radius { get { return s1; } set { s1 = value; } } //声明属性 public override double Cubage() //重载抽象方法 { //throw new Exception("The method or operation is not implemented."); return 3.14 * s1 * s1; } }
//定义派生类 Cuboid public class Cuboid : Shape { public Cuboid(double l, double w):base(l,w) { } //调用构造函数 public double L { get { return s1; } set { s1 = value; } } public double W { get { return s2; } set { s2 = value; } } //声明属性 public override double Cubage() //重载抽象方法 { //throw new Exception("The method or operation is not implemented."); return s1 * s2; } }
//定义派生类 Triangle public class Triangle : Shape { public Triangle(double bottom, double high) : base(bottom, high) { } //调用构造函数 public double Bottom { get { return s1; } set { s1 = value; } } //声明属性 public double High { get { return s2; } set { s2 = value; } } public override double Cubage() //重载抽象方法 { //throw new Exception("The method or operation is not implemented."); return s1 * s2 / 2; } } private void Form1_Load(object sender, EventArgs e) { label1.Text = " 圆半径:\n 矩形长:\n三角形底:\n"; label2.Text = " 矩形宽:\n三角形高:"; label3.Text = ""; button1.Text = "计算圆面积"; button2.Text = "计算矩形面积"; button3.Text = "计算三角形面积"; button4.Text = "退 出"; }
private void button1_Click(object sender, EventArgs e) { double radius = double.Parse(textBox1.Text);
Cycle cycle = new Cycle(radius); label3.Text = "圆的面积为:" + cycle.Cubage(); }
private void button2_Click(object sender, EventArgs e) { double l = double.Parse(textBox1.Text); double w = double.Parse(textBox2.Text);
Cuboid cuboid = new Cuboid(l,w); label3.Text = "长方形的面积为:" + cuboid.Cubage(); }
private void button3_Click(object sender, EventArgs e) { double bottom = double.Parse(textBox1.Text); double high = double.Parse(textBox2.Text);
Triangle triangle = new Triangle(bottom,high); label3.Text = "三角形的面积为:" + triangle.Cubage(); }
private void button4_Click(object sender, EventArgs e) { Application.Exit(); } }}