本压缩包包括了VC++基础例子编程,可以为您提供方便.
源代码在线查看: form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WinApp6_9静态方法
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void radradCube_CheckedChanged(object sender, EventArgs e)
{
if (radCube.Checked)
{ // 隐藏“宽”与“高”文本框和标签,设置“长”标签的标题为“棱长”
txtW.Visible = false;
txtH.Visible = false;
label2.Visible = false;
label3.Visible = false;
label1.Text = "棱长";
}
}
private void radCuboid_CheckedChanged(object sender, EventArgs e)
{
if (radCuboid.Checked)
{
txtW.Visible = true;
txtH.Visible = true;
label2.Visible = true;
label3.Visible = true;
label1.Text = "长";
}
}
private void button1_Click(object sender, EventArgs e)
{
if (radCuboid.Checked) // 如果“长方体”单选按钮被选中,则创建长方体对象
{
double l = double.Parse(txtL.Text);
double w = double.Parse(txtW.Text);
double h = double.Parse(txtH.Text);
Cuboid cuboid = new Cuboid(l, w, h);
lblInfo.Text = "对象创建成功!\n" + "长方体的长、宽、高为:"
+ cuboid.Length + "、" + cuboid.Width + "、" + cuboid.High + "\n"
+ "长方体的体积为:" + cuboid.Cubage() + "\n长方体对象的个数为:"
+ Cuboid.GetCuboidNumber(); // 使用类名调用静态方法,获取长方体对象个数
}
else // 如果“正方体”单选按钮被选中,则创建正方体对象
{
double l = double.Parse(txtL.Text);
Cuboid cuboid = new Cuboid(l);
lblInfo.Text = "对象创建成功!\n" + "正方体的棱长为:" + cuboid.Length
+ "\n" + "正方体的体积为:" + cuboid.Cubage() + "\n正方体对象的个数为:"
+ Cuboid.GetCubeNumber(); // 使用类名调用静态方法,获取正方体对象个数
}
}
}
class Cuboid
{
private static int cubeNumber ; // 静态字段,用于统计正方体对象
private static int cuboidNumber; //静态字段,用于统计长方体对象
private double length;
private double width;
private double high;
public Cuboid(double l, double w, double h) // 声明构造函数
{ length = l; width = w; high = h; cuboidNumber++; }
public Cuboid(double l) // 声明1个参数的正方体构造函数重载
{ length = width = high = l; cubeNumber++; }
public double Length { get { return length; } set { length = value; } }
public double Width { get { return width; } set { width = value; } }
public double High { get { return high; } set { high = value; } }
public double Cubage() { return length * width * high; }
public static int GetCubeNumber( )
{return cubeNumber;}
public static int GetCuboidNumber( )
{return cuboidNumber;}
}
}