Sim*_*mon 3 c# visual-studio-2013
我正在一个项目中,用户应该向动物输入值(名称,年龄,性别等),并且用户输入的值应显示在列表框中。类相互继承。这是继承的工作方式:
动物类是所有类的父类。
哺乳动物类继承自动物类。
狗类继承自哺乳动物类。
猫类继承自哺乳动物类
爬行动物类继承自动物类
蛇类继承自爬行动物类
蜥蜴类继承自爬行动物类
用户能够选择要创造的动物。有一个显示动物类型(哺乳动物和爬行动物)的列表框,旁边有一个列表框,根据用户选择的动物类型,它会显示其动物。
例如,如果用户在列表框中选择“ 哺乳动物 ”,则其旁边的列表框将显示“ 狗和猫”。
这是完美的工作,这不是问题。
问题在于,当用户在列表框中选择动物类型(例如Mammal,Dog)后输入动物值(名称和年龄)并单击添加按钮时,结果列表框会在尝试添加哺乳动物时显示Assign1_1.Mammal尝试添加爬行动物时,动物和Assign_1.Reptile。
我要执行的操作是在结果列表框中显示用户输入的值(名称和年龄),但取决于选择的动物,它只是显示Assign1.Mammal或Assign1.Reptile。
这是我的MainForm代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Assign_1
{
public partial class MainForm : Form
{
private Dog m_dog = new Dog();
private Cat m_cat = new Cat();
private Snake m_snake = new Snake();
private Lizard m_lizard = new Lizard();
private AnimalManager animalmgr = null;
private Animal m_animal = new Animal();
public MainForm()
{
//Visual Studio initializations
InitializeComponent();
//My initializations
InitializeGUI();
Gendercmb.DataSource = Enum.GetValues(typeof(GenderType));
Categorylst.DataSource = Enum.GetValues(typeof(Categorytype));
animalmgr = new AnimalManager();
}
private void InitializeGUI()
{
ReadInput();
}
private void ReadInput()
{
m_animal.Name = ReadName();
m_animal.Age = ReadAge();
}
private int ReadAge()
{
int age = 0;
int.TryParse(Agetxt.Text, out age);
return age;
}
private string ReadName()
{
string name = "";
name = Nametxt.Text;
return name;
}
private void addMammal(Mammal values)
{
ReadInput();
switch ((MammalType)Animallst.SelectedIndex)
{
case MammalType.Dog:
{
// Use a copy constructor to set a dog with common data
Dog m_dog = new Dog(values);
// If more data in GUI to fill in for this animal, do it here
//Then send it to the manager for adding to the list
animalmgr.add(m_dog);
break;
}
case MammalType.Cat:
{
Cat m_cat = new Cat();
animalmgr.add(m_cat);
break;
}
}
}
private void AddReptile(Reptile values)
{
ReadInput();
switch ((ReptileType)Animallst.SelectedIndex)
{
case ReptileType.Snake:
{
// Use a copy constructor to set a snake with common data
Snake m_snake = new Snake(values);
// If more data in GUI to fill in for this animal, do it here
//Then send it to the manager for adding to the list
animalmgr.add(m_snake);
break;
}
case ReptileType.Lizard:
{
Lizard m_lizard = new Lizard();
animalmgr.add(m_lizard);
break;
}
}
}
//When user clicks "Add to list"
private void button1_Click(object sender, EventArgs e)
{
ReadInput();
switch ((Categorytype)Categorylst.SelectedIndex)
{
case Categorytype.Mammal:
{
Mammal mammal = new Mammal(m_animal);
addMammal(mammal);
break;
}
case Categorytype.Reptile:
{
Reptile m_reptile = new Reptile(m_animal);
AddReptile(m_reptile);
break;
}
}
UpdateResults();
}
private void UpdateResults()
{
Resultlst.Items.Clear(); //Erase current list
//Get one elemnet at a time from manager, and call its
//ToString method for info - send to listbox
for (int index = 0; index < animalmgr.ElementCount; index++)
{
Animal animal = animalmgr.GetElementAtPosition(index);
Resultlst.Items.Add(animal.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的动物管理员课程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assign_1
{
class AnimalManager
{
private List<Animal> m_animal;
private List<Mammal> m_mammal;
public AnimalManager()
{
//In this list objects off diff animals of all species are saved
m_animal = new List<Animal>();
m_mammal = new List<Mammal>();
}
public void add(Animal ObjIn)
{
m_animal.Add(ObjIn);
}
public bool IsIndexValid(int index)
{
return ((index >= 0) && (index < m_animal.Count));
}
public Animal GetElementAtPosition(int index)
{
//We choose to return a copy, why do I need type casting when copying?
if (IsIndexValid(index))
{
if (m_animal[index] is Mammal)
return new Mammal((Mammal)m_animal[index]);
if (m_animal[index] is Reptile)
return new Reptile((Reptile)m_animal[index]);
return null;
}
else
return null;
}
public int ElementCount
{
get { return m_animal.Count; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的哺乳动物班:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assign_1
{
class Mammal : Animal
{
private MammalType theMammal;
private int teeth;
public Mammal() : base()
{
}
public Mammal(Animal other)
{
this.Name = other.Name;
this.Age = other.Age;
this.Gender = other.Gender;
this.Id = other.Id;
}
public Mammal (MammalType type)
{
theMammal = type;
}
#region Props
public MammalType TheMammal
{
get { return theMammal; }
set { theMammal = value; }
}
public int Teeth
{
get { return teeth; }
set { teeth = value; }
}
#endregion
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的狗类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assign_1
{
class Dog : Mammal
{
public Dog()
{
}
public Dog(Mammal other)
{
this.Name = other.Name;
this.Age = other.Age;
this.Teeth = other.Teeth;
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑 为了更简单,我只显示儿童类Mammal和Dog。如果我能够弄清楚如何创建Dog,那么我也可以修复其他狗。
您需要覆盖ToString()-method。注意override签名中的关键字。
public override string ToString()
{
//You can put anything here and it will be returned by the ToString()-method.
return this.Name + ", " + this.Age;
}
Run Code Online (Sandbox Code Playgroud)
将此代码放在您的Dog-class中。它会dog.ToString()回报Name, Age。
同样,您实际上可以将此替代方法放到基类Animal中,并将其继承给所有子类。只要他们不超越ToString()自己,他们也会回来Name, Age。
| 归档时间: |
|
| 查看次数: |
669 次 |
| 最近记录: |