C Visual Studio如何将值从子类传递到父类?

c# visual studio how to pass value from subclass to parent class?

我在这里是新的,在Visual Studio的C编程中也是新的。

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

这是原始班

计算器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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将从子类中包含的公式中得到结果,并在文本框中进行字符串处理。这就是为什么在calculator.cs中有txtcolories和txtealweight。

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

伙计们,请帮帮我,因为这对我很重要!!

谢谢大家!!


你的意思是虚拟函数吗?如果是这样,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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。