设计一个宠物注册登录信息系统。区分三类宠物:猫、狗、 蛇。界面布局美观合理自创 要求 1)定义抽象类Pet 2)由抽象类Pet派生出猫、狗、蛇类; 3)定义泛型Animal<T>,该类中包含一个List<T>泛型集 合,能够使用整型索引器、字符串索引器 4)初始状态下、包含2个动物信息:niuniu/123456和 wangcai/111111: 5)当输入正确的名称和密码时,系统进入宠物信息系统管 理界面(该界面的设计不做要求),否则提示相应的错误信息
时间: 2024-03-17 08:46:16 浏览: 186
好的,我来为您设计一个宠物注册登录信息系统。
1. 定义抽象类Pet
```
abstract class Pet
{
public string Name { get; set; } // 宠物名
public int Age { get; set; } // 年龄
public abstract string Type { get; } // 宠物类型(猫、狗、蛇)
public abstract void Eat(); // 吃东西
}
```
2. 由抽象类Pet派生出猫、狗、蛇类
```
class Cat : Pet
{
public override string Type => "猫";
public override void Eat()
{
Console.WriteLine("猫在吃鱼。");
}
}
class Dog : Pet
{
public override string Type => "狗";
public override void Eat()
{
Console.WriteLine("狗在啃骨头。");
}
}
class Snake : Pet
{
public override string Type => "蛇";
public override void Eat()
{
Console.WriteLine("蛇在吞食老鼠。");
}
}
```
3. 定义泛型Animal<T>,该类中包含一个List<T>泛型集合,能够使用整型索引器、字符串索引器
```
class Animal<T> where T : Pet
{
private List<T> pets = new List<T>();
public void Add(T pet)
{
pets.Add(pet);
}
public T this[int index]
{
get { return pets[index]; }
set { pets[index] = value; }
}
public T this[string name]
{
get { return pets.FirstOrDefault(p => p.Name == name); }
}
}
```
4. 初始状态下、包含2个动物信息:niuniu/123456和 wangcai/111111
```
Animal<Pet> animal = new Animal<Pet>();
animal.Add(new Cat { Name = "niuniu", Age = 2 });
animal.Add(new Dog { Name = "wangcai", Age = 3 });
```
5. 当输入正确的名称和密码时,系统进入宠物信息系统管理界面
您可以在Winform中创建一个登录界面,包含两个文本框(用于输入名称和密码)和一个登录按钮。当用户单击登录按钮时,触发以下事件处理程序:
```
private void btnLogin_Click(object sender, EventArgs e)
{
string name = txtName.Text;
string password = txtPassword.Text;
Pet pet = animal[name];
if (pet != null && password == "123456")
{
// 进入宠物信息系统管理界面
// TODO: 界面设计和相关操作
}
else
{
MessageBox.Show("名称或密码错误。");
}
}
```
以上是一个简单的宠物注册登录信息系统的设计,您可以根据实际需求进行修改和完善。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)