2013-05-04 53 views
-1

我是新来的,也是Visual Studio中C#编程的新手。c#visual studio如何将值从子类传递给父类?

目前我有一个关于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()。

我需要的功能是这样的:

当我执行程序和需要计算idealWeight和热量,父类calculator.cs将获得从公式计算出的结果包含在子类中,和文本框中的ToString。这就是为什么calculator.cs中有txtCalories和txtIdealWeight的原因。

所以问题是如何获得子类的结果,并填写文本框?

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

谢谢大家!

回答

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

+0

谢谢你的帮助。但我尝试一次,失败。正如你所看到的,在父类calculator.cs中有另一个if和else的私有void计算()。而这个calculation()方法将被用来根据不同的性别自动填充特定的文本框。所以我需要的是我可以调用包含在子类中的计算公式的结果,当我运行该程序时,在calculator.cs的文本框中设置ToString。 – hkmax721 2013-05-04 10:54:08

+0

我不理解你的评论,或者我不明白这个问题。你能更清楚一点吗? – WhyMe 2013-05-16 21:39:50

+0

嗯,没关系。这项工作是在上周完成的。别管它吧。感谢您的帮助! – hkmax721 2013-05-17 07:20:56

相关问题