C# 使用泛型逆变性

在 C# 中,使用 in 关键字可以指定逆变性。以下是一个示例,展示如何使用逆变性和泛型接口来处理多个类型的生产者。

逆变性

csharp 复制代码
using System;

// 定义逆变的泛型接口
public interface IProducer<in T>
{
    void Produce(T item);
}

// 实现生产者类
public class FruitProducer : IProducer<Fruit>
{
    public void Produce(Fruit fruit)
    {
        Console.WriteLine($"Produced a fruit: {fruit.Name}");
    }
}

public class AppleProducer : IProducer<Apple>
{
    public void Produce(Apple apple)
    {
        Console.WriteLine($"Produced an apple: {apple.Name}");
    }
}

// 基类和子类
public class Fruit
{
    public string Name { get; }
    
    public Fruit(string name)
    {
        Name = name;
    }
}

public class Apple : Fruit
{
    public Apple(string name) : base(name) { }
}

// 主程序
class Program
{
    static void Main()
    {
        // 创建生产者实例
        IProducer<Fruit> fruitProducer = new FruitProducer();
        IProducer<Apple> appleProducer = new AppleProducer();

        // 创建水果和苹果
        Fruit fruit = new Fruit("Banana");
        Apple apple = new Apple("Granny Smith");

        // 使用生产者
        fruitProducer.Produce(fruit);
        appleProducer.Produce(apple);

        // 使用逆变
        IProducer<Fruit> fruitProducerFromApple = appleProducer;
        fruitProducerFromApple.Produce(new Apple("Honeycrisp"));
    }
}

逆变性+委托

csharp 复制代码
using System;

// 定义逆变的泛型委托
public delegate void ProducerDelegate<in T>(T item);

// 定义逆变的泛型接口
public interface IProducer<in T>
{
    void Produce(T item);
}

// 实现生产者类
public class FruitProducer : IProducer<Fruit>
{
    public void Produce(Fruit fruit)
    {
        Console.WriteLine($"Produced a fruit: {fruit.Name}");
    }
}

public class AppleProducer : IProducer<Apple>
{
    public void Produce(Apple apple)
    {
        Console.WriteLine($"Produced an apple: {apple.Name}");
    }
}

// 基类和子类
public class Fruit
{
    public string Name { get; }
    
    public Fruit(string name)
    {
        Name = name;
    }
}

public class Apple : Fruit
{
    public Apple(string name) : base(name) { }
}

// 主程序
class Program
{
    static void Main()
    {
        // 创建生产者实例
        IProducer<Fruit> fruitProducer = new FruitProducer();
        IProducer<Apple> appleProducer = new AppleProducer();

        // 创建委托实例
        ProducerDelegate<Fruit> fruitDelegate = fruitProducer.Produce;
        ProducerDelegate<Apple> appleDelegate = appleProducer.Produce;

        // 创建水果和苹果
        Fruit fruit = new Fruit("Banana");
        Apple apple = new Apple("Granny Smith");

        // 使用委托生产水果和苹果
        fruitDelegate(fruit);
        appleDelegate(apple);

        // 使用逆变
        ProducerDelegate<Fruit> fruitDelegateFromApple = appleProducer.Produce;
        fruitDelegateFromApple(new Apple("Honeycrisp"));
    }
}
相关推荐
paterWang1 小时前
基于 Python 和 OpenCV 的酒店客房入侵检测系统设计与实现
开发语言·python·opencv
东方佑1 小时前
使用Python和OpenCV实现图像像素压缩与解压
开发语言·python·opencv
我真不会起名字啊2 小时前
“深入浅出”系列之杂谈篇:(3)Qt5和Qt6该学哪个?
开发语言·qt
laimaxgg2 小时前
Qt常用控件之单选按钮QRadioButton
开发语言·c++·qt·ui·qt5
水瓶丫头站住2 小时前
Qt的QStackedWidget样式设置
开发语言·qt
小钊(求职中)3 小时前
Java开发实习面试笔试题(含答案)
java·开发语言·spring boot·spring·面试·tomcat·maven
慕诗客5 小时前
QT基于Gstreamer采集的简单示例
开发语言·qt
Blasit5 小时前
C++ Qt建立一个HTTP服务器
服务器·开发语言·c++·qt·http
黄金小码农5 小时前
c# 2025/2/19 周三
c#
Victoria.a5 小时前
数组和指针常见笔试题(深度剖析)
c语言·开发语言