泛型的使用
pet.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Practice
{
///
///动物类
///
abstract class Pet
{
public string name;
public int health;
public int love;
public Pet(string name) : this(name, 100, 100) { }
public Pet() { }
public Pet(string name,int health,int love) {
this.name = name;
this.health = health;
this.love = love;
}
public abstract void Sleep();
public abstract void Play();
public void ShowInfo()
{
Console.WriteLine("宠物姓名:{0},健康值:{1},亲密度:{2}",name,health,love);
}
}
}
Cat.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Practice
{
///
///猫类
///
class Cat : Pet
{
public Cat(string name) : base(name) { }
public Cat(string name, int health, int love) : base(name, health, love) { }
public override void Sleep()
{
Console.WriteLine("猫 " + name + " 睡觉");
}
public override void Play()
{
Console.WriteLine("猫 " + name + " 玩耍");
}
}
}
Dog.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Practice
{
///
///狗类
///
class Dog:Pet
{
public Dog(string name) : base(name) { }
public Dog(string name, int health, int love) : base(name, health, love) { }
public override void Sleep() {
Console.WriteLine("狗狗 "+name+" 睡觉");
}
public override void Play() {
Console.WriteLine("狗狗 " + name + " 玩耍");
}
}
}
Penguin.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Practice
{
///
///企鹅类
///
class Dog:Pet
{
public Dog(string name) : base(name) { }
public Dog(string name, int health, int love) : base(name, health, love) { }
public override void Sleep() {
Console.WriteLine("狗狗 "+name+" 睡觉");
}
public override void Play() {
Console.WriteLine("狗狗 " + name + " 玩耍");
}
}
}
PetStore.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Practice
{
/// <summary>
/// 综合宠物店类
/// </summary>
class PetStore
{
List<Pet> petList = new List<Pet>();
public List<Pet> PetList
{
get { return petList; }
set { value = petList; }
}
/// <summary>
/// 查看某种类型的宠物的睡眠情况
/// </summary>
/// <typeparam name="T"></typeparam>
public void ShowSleep<T>()where T:Pet
{
//遍历宠物清单列表
for (int i = 0; i < petList.Count; i++)
{
//从列表中查找满足类型条件的对象
if (petList[i].GetType()==typeof(T))
{
//展示睡眠情况
petList[i].Sleep();
}
}
}
/// <summary>
/// 查看某种类型宠物的玩耍情况
/// </summary>
/// <typeparam name="T"></typeparam>
public void ShowPlay<T>() where T : Pet
{
//遍历宠物清单列表
for (int i = 0; i < petList.Count; i++)
{
//从列表中查找满足类型条件的对象
if (petList[i].GetType() == typeof(T))
{
//展示玩耍情况
petList[i].Play();
}
}
}
}
}
TypedPetStore.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Practice
{
/// <summary>
/// 特种宠物店类
/// </summary>
/// <typeparam name="T"></typeparam>
class TypedPetStore<T>where T:Pet
{
List<T> petList = new List<T>();
public List<T> PetList
{
set { value = petList; }
get { return petList; }
}
/// <summary>
/// 查看特定类型的宠物的睡眠情况
/// </summary>
public void ShowSleep() {
for (int i = 0; i < petList.Count; i++)
{
petList[i].Sleep();
}
}
/// <summary>
/// 查看特定类型的宠物的玩耍情况
/// </summary>
public void ShowPlay()
{
for (int i = 0; i < petList.Count; i++)
{
petList[i].Play();
}
}
}
}
DictionaryPetStore.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Practice
{
class DictionaryPetStore
{
/// <summary>
/// 字典的综合动物商店类
/// </summary>
Dictionary<int, Pet> petList = new Dictionary<int, Pet>();
public Dictionary<int, Pet> PetList {
get { return petList; }
set { value = PetList; }
}
public void SleepShow<T>() {
foreach (var item in petList.Keys)
{
if (petList[item].GetType()==typeof(T))
{
petList[item].Sleep();
}
}
}
public void PlayShow<T>()
{
foreach (var item in petList.Keys)
{
if (petList[item].GetType() == typeof(T))
{
petList[item].Play();
}
}
}
}
}
DictionaryTypePetStore.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Practice
{
/// <summary>
/// 字典的特种宠物店类
/// </summary>
/// <typeparam name="T"></typeparam>
class DictionaryTypePetStore<T>where T:Pet
{
Dictionary<int, T> petList = new Dictionary<int, T>();
public Dictionary<int,T> PetList {
get { return petList; }
set { value = petList; }
}
public void ShowSleep() {
foreach (var item in petList.Keys)
{
petList[item].Sleep();
}
}
public void ShowPlay() {
foreach (var item in petList.Keys)
{
petList[item].Play();
}
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Practice
{
class Program
{
static void Main(string[] args)
{
Dog dog1 = new Dog("小狗1", 80, 90);
Cat cat1 = new Cat("小猫1", 80, 80);
Penguin penguin1 = new Penguin("企鹅1", 70, 89);
dog1.ShowInfo();
dog1.Sleep();
dog1.Play();
cat1.ShowInfo();
cat1.Sleep();
cat1.Play();
penguin1.ShowInfo();
penguin1.Sleep();
penguin1.Play();
Console.WriteLine("------查看综合宠物商店的宠物睡眠情况----------------");
//综合宠物店添加宠物
PetStore petStore = new PetStore();
petStore.PetList.Add(dog1);
petStore.PetList.Add(cat1);
petStore.PetList.Add(penguin1);
//查看综合宠物商店的宠物睡眠情况
petStore.ShowSleep<Dog>();
petStore.ShowSleep<Cat>();
petStore.ShowSleep<Penguin>();
Console.WriteLine("--------查看特种宠物店中宠物的睡眠情况---------------");
//特种宠物店中添加宠物
TypedPetStore<Dog> dogPetStore = new TypedPetStore<Dog>();
dogPetStore.PetList.Add(dog1);
dogPetStore.ShowSleep();
Console.WriteLine("----------用字典实现查看综合宠物商店的宠物睡眠情况-----------");
//用字典实现综合宠物商店添加宠物
DictionaryPetStore dicPetStore = new DictionaryPetStore();
dicPetStore.PetList.Add(1,dog1);
dicPetStore.PetList.Add(2, cat1);
dicPetStore.PetList.Add(3, penguin1);
//用字典实现查看综合宠物商店的宠物睡眠情况
dicPetStore.SleepShow<Dog>();
dicPetStore.SleepShow<Cat>();
dicPetStore.SleepShow<Penguin>();
Console.WriteLine("-------查看用字典实现特种宠物店中添加宠物之后的睡眠情况-------------");
//用字典实现特种宠物店中添加宠物
DictionaryTypePetStore<Dog> dicDogPetStore = new DictionaryTypePetStore<Dog>();
dicDogPetStore.PetList.Add(1, dog1);
dicDogPetStore.ShowSleep();
Console.ReadKey();
}
}
}