c# visual studio如何将值从子类传递到父类

本文关键字:子类 父类 studio visual | 更新日期: 2023-09-27 18:12:10

我是新来的,也是Visual Studio c#编程的新手。

目前我有一个关于c#重构的任务。

这是原始类

calculator.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)
        {
        }
    }
}

下面的类是我要设置的父类和子类。

父:calculator.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();
        }
    }
}

我们可以看到,这两个子类用于计算卡路里和理想体重。它们计划取代原始类calculator.cs中的private void calorieCalculation()和private void weightCalculation()。

我需要的函数是这样的:

当我执行程序并需要计算理想体重和卡路里时,父类calculator.cs将从子类中包含的公式和文本框中的ToString中获得结果。这就是为什么在calculator.cs中有txtcalorie和txtIdealWeight。

那么问题是如何在子类中获得结果,并填写文本框?

伙计们,请帮帮我,因为这对我来说真的很重要!!

谢谢大家!!

c# visual studio如何将值从子类传递到父类

你是说虚函数吗?如果是,

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;
    }
}

这是祖先,具有虚函数。更多信息:

    虚函数在c#中的实际应用
  • http://msdn.microsoft.com/en-us/library/9fkccyh4 (v = vs.80) . aspx

这种类型的代码也可以通过接口完成。参见http://msdn.microsoft.com/en-us/library/vstudio/ms173156.aspx。