⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 Visual C#2005程序设计教程
💻 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_7构造函数长方体
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        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();
            }
            else
            {
                double l = double.Parse(txtL.Text);
                Cuboid cuboid = new Cuboid(l);
                lblInfo.Text = "对象创建成功!\n" + "正方体的棱长为:"
                    + cuboid.Length + "\n" + "正方体的体积为:" + cuboid.Cubage();
            }
        }

        private void radCube_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 = "长";
            }
        }
    }

    class Cuboid
    {
        private double length;
        private double width;
        private double high;
        public Cuboid(double l, double w, double h)   // 声明构造函数
        {
            length = l; width = w; high = h;
        }
        public Cuboid(double l)
        { length = width = high = l; }
        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 bool IsCube() { return (length == width && length == high); }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -