如何在 C# 中实现某种程度的多态性?

发布于 2024-10-20 02:59:18 字数 878 浏览 3 评论 0 原文

这是我最近一直试图解决的问题的简化版本。 我有以下两个类:

class Container { }  

class Container<T> : Container  
{  

    T Value  
    {  
        get;  
        private set;  
    }

    public Container(T value)  
    {  
        Value = value;  
    }  

    public T GetValue()
    {
        return Value;
    }
}

现在我想做:

Container<int> c1 = new Container<int>(10);
Container<double> c2 = new Container<double>(5.5);

List<Container> list = new List<Container>();
list.Add(c1);  
list.Add(c2);  

foreach (Container item in list)
{  
    Console.WriteLine(item.Value);
    Console.WriteLine(item.GetValue()); 
} 

实现此功能的最佳方法是什么?有可能吗?我想我可能有解决这个问题的方法,但我认为这是一个解决方法,我正在寻找一些设计模式。

预先感谢您的回复, 米哈尔.

PS

我尝试过接口、虚函数、抽象类、抽象函数;甚至在超类中创建函数,通过名称调用真实类型的属性(使用反射)...我仍然无法实现我想要的...

This is a simplified version of the problem that I've been trying to solve recently.
I have the following two classes:

class Container { }  

class Container<T> : Container  
{  

    T Value  
    {  
        get;  
        private set;  
    }

    public Container(T value)  
    {  
        Value = value;  
    }  

    public T GetValue()
    {
        return Value;
    }
}

Now I want to do:

Container<int> c1 = new Container<int>(10);
Container<double> c2 = new Container<double>(5.5);

List<Container> list = new List<Container>();
list.Add(c1);  
list.Add(c2);  

foreach (Container item in list)
{  
    Console.WriteLine(item.Value);
    Console.WriteLine(item.GetValue()); 
} 

What is the best way to implement this functionality? Is it possible at all? I think I might have on solution to this issue, but I consider it a work-around and I am looking for some design pattern.

Thank you in advance for your responses,
Michal.

P.S.

I tried with interfaces, virtual functions, abstract classes, abstract functions; even creating functions in a superclass that would call the properties of the real type by name (using reflection)...I am still not able to achieve what I want...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

听闻余生 2024-10-27 02:59:18

您可以将基类 Container 转换为接口:

interface IContainer
{
    object GetValue();
}

然后在派生类中显式实现该接口:

class Container<T> : IContainer
{
    public T Value { get; private set; }

    public Container(T value)
    {
        Value = value;
    }

    public T GetValue()
    {
        return Value; 
    }

    object IContainer.GetValue()
    {
        return this.GetValue();
    }
}

更改列表以包含 IContainer 元素:

Container<int> c1 = new Container<int>(10);
Container<double> c2 = new Container<double>(5.5);
List<IContainer> list = new List<IContainer>();
list.Add(c1);
list.Add(c2);

foreach (IContainer item in list)
{
    Console.WriteLine(item.GetValue());
}

Container 上的公共 Value 属性有点令人困惑,但您明白我的意思。

You could the base class Container into an interface:

interface IContainer
{
    object GetValue();
}

Which is then explicitly implemented in the derived classes:

class Container<T> : IContainer
{
    public T Value { get; private set; }

    public Container(T value)
    {
        Value = value;
    }

    public T GetValue()
    {
        return Value; 
    }

    object IContainer.GetValue()
    {
        return this.GetValue();
    }
}

Change the list to contain IContainer elements:

Container<int> c1 = new Container<int>(10);
Container<double> c2 = new Container<double>(5.5);
List<IContainer> list = new List<IContainer>();
list.Add(c1);
list.Add(c2);

foreach (IContainer item in list)
{
    Console.WriteLine(item.GetValue());
}

The public Value property on Container is kind of confusing, but you get my point.

献世佛 2024-10-27 02:59:18

您正在寻找这样的东西吗?这允许您迭代这些值。

abstract class Container
{
    public abstract object RawValue { get; }
}

class Container<T> : Container
{
    public override object RawValue
    {
        get { return this.Value; }
    }

    T Value
    {
        get;
        private set;
    }

    public Container(T value)
    {
        Value = value;
    }
}

编辑:您可以随意调用 Container.RawValue,这是我首先想到的。您可以这样称呼它:

Container<int> c1 = new Container<int>(10);
Container<double> c2 = new Container<double>(5.5);

List<Container> list = new List<Container>();
list.Add(c1);  
list.Add(c2);  

foreach (Container item in list)
{  
    Console.WriteLine(item.RawValue);
    Console.WriteLine(item.RawValue); 
} 

Is something like this what you're looking for? This allows you to iterate through the values.

abstract class Container
{
    public abstract object RawValue { get; }
}

class Container<T> : Container
{
    public override object RawValue
    {
        get { return this.Value; }
    }

    T Value
    {
        get;
        private set;
    }

    public Container(T value)
    {
        Value = value;
    }
}

EDIT: You can call Container.RawValue whatever you want, that was the first thing that came to mind. Here is how you would call it:

Container<int> c1 = new Container<int>(10);
Container<double> c2 = new Container<double>(5.5);

List<Container> list = new List<Container>();
list.Add(c1);  
list.Add(c2);  

foreach (Container item in list)
{  
    Console.WriteLine(item.RawValue);
    Console.WriteLine(item.RawValue); 
} 
牵你手 2024-10-27 02:59:18

只是添加到您已有的答案中,这不是多态性问题,而是类型专业化的问题。就编译器而言,ContainerContainer 不是同一件事,因此 List() 也不是与 List>() 相同。

您可以执行类似的操作

List<Container<int>> list = new List<Container<int>>();

,但这也不适用于 List> 。因此,答案是将 GetValue() 定义移至接口。

Just to add to the answers you already have, this isn't a matter of polymorphism, it's a problem of type specialization. As far as the compiler is concerned, Container and Container<T> are not the same thing, so List<Container>() is not the same thing as List<Container<T>>().

You can do something like

List<Container<int>> list = new List<Container<int>>();

But that won't work with List<Container<double>> either. So the answer is to move the GetValue() definition to an interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文