泛型的使用

泛型的使用

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();
    }
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
泛型(Generics)是一种在编程语言中实现参数化类型的技术,可以让我们编写更加灵活和通用的代码。下面是一个泛型使用案例: 假设我们有一个需求,需要实现一个通用的栈(Stack)数据结构,可以存储任何类型的元素。我们可以使用泛型来实现这个通用的栈数据结构。以下是一个基于Java的示例代码: ```java public class Stack<T> { private ArrayList<T> items; public Stack() { items = new ArrayList<T>(); } public void push(T item) { items.add(item); } public T pop() { if (items.isEmpty()) { throw new RuntimeException("Stack is empty"); } return items.remove(items.size() - 1); } public boolean isEmpty() { return items.isEmpty(); } } ``` 在上面的代码中,我们使用了一个类型参数 `T`,它代表任何类型。我们在类的定义中使用了 `<T>` 来声明这个类是一个泛型类,它可以接受任何类型的元素。在类的内部,我们使用 `T` 来代表元素的类型。我们将元素存储在一个 `ArrayList<T>` 中,这个 `ArrayList` 可以存储任何类型的元素。 我们定义了三个方法:`push()`、`pop()` 和 `isEmpty()`。`push()` 方法用于将元素压入栈中,`pop()` 方法用于弹出栈顶元素,并从栈中移除它,`isEmpty()` 方法用于判断栈是否为空。 使用泛型,我们可以使用这个通用的栈数据结构来存储任何类型的元素,例如: ```java Stack<Integer> intStack = new Stack<Integer>(); intStack.push(1); intStack.push(2); intStack.push(3); intStack.pop(); // 返回 3 intStack.pop(); // 返回 2 Stack<String> strStack = new Stack<String>(); strStack.push("Hello"); strStack.push("World"); strStack.pop(); // 返回 "World" ``` 在上面的示例代码中,我们分别使用了 `Stack<Integer>` 和 `Stack<String>` 来存储整数和字符串类型的元素。由于使用泛型,这个通用的栈数据结构可以存储任何类型的元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值