-1

私はここでは初めてで、Visual Studio での C# プログラミングも初めてです。

現在、C# リファクタリングに関する課題があります。

オリジナルクラスです

電卓.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HUANG_Kai_30077528_Assign1
{
    public partial class calculator : Form
    {


        public calculator()
        {
            InitializeComponent();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void calorieCalculation()
        {
            if (rbtnMale.Checked)
            {
                txtCalories.Text = (66
            + (6.3 * Convert.ToDouble(txtWeight.Text))
            + (12.9 * ((Convert.ToDouble(txtFeet.Text) * 12)
            + Convert.ToDouble(txtInches.Text)))
            - (6.8 * Convert.ToDouble(txtAge.Text))).ToString();
        }
        else
        {
            txtCalories.Text = (655
            + (4.3 * Convert.ToDouble(txtWeight.Text))
            + (4.7 * ((Convert.ToDouble(txtFeet.Text) * 12)
            + Convert.ToDouble(txtInches.Text)))
            - (4.7 * Convert.ToDouble(txtAge.Text))).ToString();
        }
    }

    private void weightCalculation()
    {
        double maleVariable = 50;
        double femaleVariable = 45.5;
        double Formula = (2.3 * (((Convert.ToDouble(txtFeet.Text) - 5) * 12) + Convert.ToDouble(txtInches.Text)));

        if (rbtnMale.Checked)
        {
            txtIdealWeight.Text = ((maleVariable + Formula) * 2.2046).ToString();
        }
        else
        {
            txtIdealWeight.Text = ((femaleVariable + Formula) * 2.2046).ToString();
        }
    }

    private void txtFeetValidation()
    {
        double feet;
        if (!double.TryParse(txtFeet.Text, out feet))
        {
            MessageBox.Show("Feet must be a numeric value.");
            txtFeet.Select();
            if (!(Convert.ToDouble(txtFeet.Text) >= 5))
            {
                MessageBox.Show("Height has to be equal to or greater than 5 feet!");
                txtFeet.Select();
                return;
            }
        }
    }

    private void txtInchesValidation()
    {
        double inches;
        if (!double.TryParse(txtInches.Text, out inches))
        {
            MessageBox.Show("Inches must be a numeric value.");
            txtInches.Select();
            return;
        }
    }

    private void txtWeightValidation()
    {
        double weight;
        if (!double.TryParse(txtWeight.Text, out weight))
        {
            MessageBox.Show("Weight must be a numeric value.");
            txtWeight.Select();
            return;
        }
    }

    private void txtAgeValication()
    {
        double age;
        if (!double.TryParse(txtAge.Text, out age))
        {
            MessageBox.Show("Age must be a numeric value.");
            txtAge.Select();
            return;
        }
    }

    private void btnCalculate_Click(object sender, EventArgs e)
    {
        txtFeetValidation();
        txtInchesValidation();
        txtWeightValidation();
        txtAgeValication();

        //Clear old results           
        txtIdealWeight.Text = "";
        txtCalories.Text = "";
        calorieCalculation();
        weightCalculation();
    }

    private void label8_Click(object sender, EventArgs e)
    {

    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void btnAddPatient_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(txtIdealWeight.Text))
        {
            Form secondForm = new patientInfo(this);
            secondForm.Show();

        }
        else
        {
            MessageBox.Show("Please enter all datas and click on 'Add Patient' to add patient's records");
        }


    }

    private void btnPatientList_Click(object sender, EventArgs e)
    {
        Form secondForm = new patientList(this);
            secondForm.Show();
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        rbtnMale.Checked = false;
        rbtnFemale.Checked = false;
        txtFeet.Text = "";
        txtInches.Text = "";
        txtWeight.Text = "";
        txtAge.Text = "";
        txtCalories.Text = "";
            txtIdealWeight.Text = "";

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}

次のクラスは、セットアップしたい親クラスとサブクラスです。

親: 計算機.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HUANG_Kai_30077528_Assign1
{
    public partial class calculator : Form
    {
        //private string maleCaloriesCalculation();
        //private string maleWeightCalculation();
        //private string femaleCaloriesCalculation();
        //private string femaleWeightCalculation();

        public calculator()
        {
            InitializeComponent();
        }

        private void rbtnMale_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            //Clear old results           
            txtIdealWeight.Text = "";
            txtCalories.Text = "";


            /* Validate User Input: */
            //Validate height (feet) is numeric value
            double result;
            if (!double.TryParse(txtFeet.Text, out result))
            {
                MessageBox.Show("Feet must be a numeric value.");
                txtFeet.Select();
                return;
            }
            //Validate height (inches) is numeric value
            if (!double.TryParse(txtInches.Text, out result))
            {
                MessageBox.Show("Inches must be a numeric value.");
                txtInches.Select();
                return;
            }
            //Validate weight is numeric value
            if (!double.TryParse(txtWeight.Text, out result))
            {
                MessageBox.Show("Weight must be a numeric value.");
                txtWeight.Select();
                return;
            }
            //Validate age is numeric value
            if (!double.TryParse(txtAge.Text, out result))
            {
                MessageBox.Show("Age must be a numeric value.");
                txtAge.Select();
                return;
            }

            if (!(Convert.ToDouble(txtFeet.Text) >= 5))
            {
                MessageBox.Show("Height has to be equal to or greater than 5 feet!");
                txtFeet.Select();
                return;
            }

            /*End validation*/
            calculation();
        }

        private void calculation()
        {
            if (rbtnMale.Checked)
            {
                txtCalories.Text = maleCalculator.maleCalories().ToString();
                //txtCalories.Text = maleCalculator.maleCaloriesCalculation();
                //txtIdealWeight.Text = maleCalculator.maleWeightCalculation();
            }
            else
            {
                txtCalories.Text = femaleCalculator.femaleCaloriesCalculation();
                txtIdealWeight.Text = femaleCalculator.femaleWeightCalculation();
            }
        }

       private void btnClear_Click(object sender, EventArgs e)
        {
            rbtnMale.Checked = false;
            rbtnFemale.Checked = false;
            txtFeet.Text = "";
            txtInches.Text = "";
            txtWeight.Text = "";
            txtAge.Text = "";
            txtCalories.Text = "";
            txtIdealWeight.Text = "";

        }
    }
}

サブクラス: maleCalculation.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HUANG_Kai_30077528_Assign1
    {
    class maleCalculator: calculator
    {
        //private string maleCaloriesCalculation;
        //private string maleWeightCalculation;

        public maleCalculator maleCalories()
        {
            (66 + (6.3 * Convert.ToDouble(txtWeight.Text))
            + (12.9 * ((Convert.ToDouble(txtFeet.Text) * 12)
            + Convert.ToDouble(txtInches.Text)))
            - (6.8 * Convert.ToDouble(txtAge.Text))).ToString();

            return maleCalories();
            //this.txtCalories.Text = new maleCalculator.maleCalories;
        }

        public maleCalculator maleWeight()
        {
            ((50 + (2.3 * (((Convert.ToDouble(txtFeet.Text) - 5) * 12)
            + Convert.ToDouble(txtInches.Text)))) * 2.2046).ToString();

            return maleWeight();
        }
    }
}

サブクラス: femaleCalculation.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HUANG_Kai_30077528_Assign1
{
    class femaleCalculator : calculator
    {
        public double femaleCaloriesCalculation()
        {
            txtCalories.Text = (655 + (4.3 * Convert.ToDouble(txtWeight.Text))
            + (4.7 * ((Convert.ToDouble(txtFeet.Text) * 12)
            + Convert.ToDouble(txtInches.Text)))
            - (4.7 * Convert.ToDouble(txtAge.Text))).ToString();

            return femaleCaloriesCalculation();
        }

        public double femaleWeightCalculation()
        {
            txtIdealWeight.Text = ((45.5 + (2.3 * (((Convert.ToDouble(txtFeet.Text) - 5) * 12)
            + Convert.ToDouble(txtInches.Text)))) * 2.2046).ToString();

            return femaleWeightCalculation();
        }
    }
}

ご覧のとおり、これら 2 つのサブクラスは、カロリーと理想体重の計算に使用されます。これらは、元のクラスcalculator.csのprivate void calorieCalculation()およびprivate void weightCalculation()に取って代わる予定です。

私が必要とする機能は次のようなものです:

プログラムを実行し、idealWeight とカロリーを計算する必要がある場合、親クラスのcalculator.csは、サブクラスに含まれる式とテキスト ボックスのToStringから結果を取得します。そのため、calculator.cs 内に txtCalories と txtIdealWeight があります。

問題は、サブクラスで結果を取得し、テキスト ボックスに入力する方法です。

みんな、これは私にとって本当に重要なので、私を助けてください!!

皆さん、ありがとうございました!!

4

1 に答える 1

0

仮想機能のことですか?もしそうなら、

class Ancestor
{
    public virtual int DoSomething()
    {
        // execute commands here.
        // for now just throw exception.
        throw new NotImplementedException();
    }
}

class Derived_A : Ancestor
{
    public override int DoSomething()
    {
        return 1 + 1;
    }
}

class Derived_B : Ancestor
{
    public override int DoSomething()
    {
        return 1 + 2;
    }
}

これは祖先であり、仮想機能を備えています。詳細については、次を参照してください。

このタイプのコードは、インターフェイスでも実行できます。http://msdn.microsoft.com/en-us/library/vstudio/ms173156.aspxを参照してください。

于 2013-05-04T10:27:24.920 に答える