拒绝课程/方法 [英] Reffer to a class/method
问题描述
我在我的Calroies Class中有这个方法,其中包含User类。
I have this method in my Calroies Class which inhertis the User class.
class Calories : User
{
public void CalcuateCaloriesMale()
{
var user = new User();
var equation = (10 * user.Weight) + (6.25 * user.Height) - (5 * user.Age) + 5;
Console.WriteLine("Based on the Mifflin – St Jeor Formula You need to eat {0} Kalories a day\nTo Gain Weight ", equation);
}
}
我在Program.cs中有这段代码
And i have this code in Program.cs
if (newGoal.GoalStatusGainWeight == true)
{
var calories = new Calories();
switch (newUser.Gender)
{
case "m":
calories.CalcuateCaloriesMale();
break;
现在我不明白如何让Calories Class知道什么用户在开始时输入。
Now I dont understand how to make the Calories Class know what the user has typed in at the beginning of the Beginning.
newUser.Name = Question.AskString("Name: ");
newUser.Age = Question.AskInt("Age: ");
newUser.Gender = Question.AskString("Gender(m/f): ");
newUser.Height = Question.AskInt("Height(cm): ");
newUser.Weight = Question.AskInt("Weight(kg): ");
卡路里等级没有考虑用户输入的内容。
我认为我可以juse创建一个新用户,但我想我的想法是错误的。
我做错了什么?我怎么能想象这个有用呢?我可以更好地学习它吗?
我尝试了什么:
我不明白如何让代码知道用户在初始输入的内容
The Calories Class doesn't take into consideration waht the user has typed in.
I tought i can juse make a new User, but i guess my thinking was wrong.
What am i doing wrong ? How can i imagine this working so i can learn it better?
What I have tried:
I dont understand how to make the code know what the user has typed in initally
推荐答案
确保卡路里
class获取newUser
,例如作为CalculateCaloriesMale
的参数:
Make sure that theCalories
class getsnewUser
, for example as argument toCalculateCaloriesMale
:
public void CalcuateCaloriesMale(User user) // add "user" parameter
{
// now you don't need this line: var user = new User();
var equation = (10 * user.Weight) + (6.25 * user.Height) - (5 * user.Age) + 5;
Console.WriteLine("Based on the Mifflin – St Jeor Formula You need to eat {0} Kalories a day\nTo Gain Weight ", equation);
}
...
if (newGoal.GoalStatusGainWeight == true)
{
var calories = new Calories();
switch (newUser.Gender)
{
case "m":
calories.CalcuateCaloriesMale(newUser); // Pass newUser to the method
break;
或者,您可以创建构造函数 [ ^ ] 卡路里
并在那里拿一个用户
并存储它,然后在无参数 CalculateCaloriesMale
类中使用它。
附注:制作<$ c几乎没有意义$ c>卡路里继承自用户
。对于人类开发人员来说,卡路里似乎是某种类型的用户,这当然不是真的。并且它看起来没有任何用处。
Alternatively, you could create a constructor[^] for Calories
and take a User
there and store it, then use it in a parameterless CalculateCaloriesMale
class.
Side note: it hardly makes sense to make Calories
inherit from User
. It would appear to a human developer as if Calories is a certain type of User, which is of course not true. And it doesn't look like it has any use.
这篇关于拒绝课程/方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!