我有一个带有两个不同类的代码(在主程序类旁边)。 第一类叫做“猫”,看起来像这样:
class cat
{
private string name;
private float weight;
private float speed;
static private int counter;
//-----------------------------------------------
public cat()
{
counter++;
}
public static int getcounter()
{
return counter;
}
//----------------------------------------------
public void setname(string name)
{
this.name = name;
}
public void setweight(float weight)
{
this.weight = weight;
}
public void setspeed(float speed)
{
this.speed = speed;
}
//-----------------------------------------------
public string getname()
{
return name;
}
public float getweight()
{
return weight;
}
public float getspeed()
{
return speed;
}
}
第二个类称为“ zoo”,它继承了“ cat”类,如下所示:
class zoo : cat
{
public string[] arcats;
//--------------------------------------------------
public void addcats(int counter)
{
arcats = new string[counter];
}
public void setcats(int counter, string cat)
{
arcats[counter] = cat;
}
//-----------------------------------------------------
public bool iscatsfull(bool limit)
{
limit = false;
if (getcounter() > 20)
{
return limit;
}
else
return true;
}
//-----------------------------------------------------
public int Getcounter()
{
return getcounter();
}
}
主程序如下:
class Program
{
static void Main(string[] args)
{
cat l = new cat();
l.setweight(50.5f);
cat t = new cat();
t.setweight(67.7f);
cat c = new cat();
c.setweight(54.3f);
}
}
我要在这里做的是在“ zoo”类中创建一个数组,该数组将捕获所有“ cat”对象(布尔函数是确保数组中不超过20个对象)。 在拥有一个有效的数组之后,我想在“ zoo”类中创建两个新函数,一个用于计算数组中所有猫的平均体重,另一个与速度相同。
我尝试使用此功能,但不起作用:
public float Weightavg()
{
return (cat.getweight())/ getcounter();
}
我尝试在函数中添加“ static”关键字,但这只是弄乱了代码的另一部分。谁能告诉我如何使用已经使用的代码使该功能正常工作?当然,如果我不知道代码中有任何问题,我也很乐意听到。 在此先感谢您抽出宝贵时间帮助^^
答案 0 :(得分:1)
很好的问题,我马上注意到了一些事情。一种是您的所有方法名称都应为CarmelCased,即setname
应该为SetName
,而getname
应该为GetName
,等等。
您对iscatsfull
的逻辑也不正确。当true
数组位于arcats
上方时,您似乎应该返回20
。看起来您应该将arcats
中的猫另存为Cat
对象,并且不将其另存为strings
。
counter
中的cat
是无用的,因为它不应该生活在Cats
类中,而应该生活在Zoo
类中。
您的Cat
类可以重构为:
public class Cat
{
private string name;
private float weight;
private float speed;
public Cat()
{
this.name = "";
this.weight = 0.0;
this.speed = 0.0;
}
public void SetName(string name)
{
this.name = name;
}
public void SetWeight(float weight)
{
this.weight = weight;
}
public void SetSpeed(float speed)
{
this.speed = speed;
}
public string GetName()
{
return this.name;
}
public float GetWeight()
{
return this.weight;
}
public float GetSpeed()
{
return this.speed;
}
}
您的动物园课程可以是:
public class Zoo
{
public Cat [] arcats;
public void CreateZoo(int counter)
{
arcats = new Cat[counter];
}
public void AddCat(int counter, Cat cat)
{
arcats[counter] = cat;
}
public bool IsZooFull()
{
if (arcats.Length > 20)
{
return true;
}
else
{
return false;
}
}
public double GetAvgWeight()
{
double totalWeight = 0.0;
foreach(Cat cat in arcats) // Loop through all the cats in arcats and add all their weights
{
totalWeight += cat.GetWeight();
}
return totalWeight / arcats.Length; // Divide total weight by number of cats
}
}
您现在可以像这样在Main
中与他们合作:
class Program
{
static void Main(string[] args)
{
Cat cat1 = new Cat();
cat1.SetWeight(50.5f);
Cat cat2 = new Cat();
cat2.SetWeight(67.7f);
Cat cat3 = new Cat();
cat3.SetWeight(54.3f);
Zoo z = new Zoo();
// Create new Zoo with arcats of length 3
// z.CreateZoo(21);
z.CreateZoo(3);
// Add the three Cats to the Zoo
z.AddCat(0, cat1);
z.AddCat(1, cat2);
z.AddCat(2, cat3);
Console.WriteLine("Checking if Zoo is full: " + z.IsZooFull()); // false
Console.WriteLine("Average weight of Cats in Zoo: " + z.GetAvgWeight());
}
}