用不同的兼容类型覆盖属性 [英] Override Property with different compatible Type

查看:135
本文介绍了用不同的兼容类型覆盖属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个具有属性的基类,我可以派生具有相同属性但不同(兼容)类型的类。基类可以是抽象的。

  public class Base 
{
public virtual object prop {get;组; }
}

public class StrBase:Base
{
public override string prop {get;组; } //编译器错误
}
$ b $ public class UseIt
{
public void use()
{
List< Base> l =新列表< Base>();
// ...
}
}






我用泛型试了一下,但是这给了我一个使用这个类的问题,因为我想在List中存储不同类型的基类。

  public class BaseG< T> 
{
public T prop {get;组; }
}
$ b $ public class UseIt
{
public void use()
{
List< BaseG> l =新列表< BaseG>(); //需要类型参数
// ...
}
}




  public abstract class Base 
{
public abstract void Use();
public abstract object GetProp();
}

公共抽象类GenericBase< T> :Base
{
public T Prop {get;组; }

public override object GetProp()
{
return Prop;
}
}

公共类StrBase:GenericBase< string>
{
public override void Use()
{
Console.WriteLine(Using string:{0},Prop);
}
}

公共类IntBase:GenericBase< int>
{
public override void使用()
{
Console.WriteLine(使用int:{0},Prop);






$ b基本上我在中间添加了一个泛型类存储您的正确类型的属性。这将工作,假设您从不需要访问 List< Base>
成员的代码中的 Prop 。 (如果需要的话,您总是可以向 Base 称为 GetProp >将通用对象转换为对象)添加抽象方法。



示例用法:

  class Program 
{
static void Main(string [] args)
{
List< Base> l =新列表< Base>();

l.Add(new StrBase {Prop =foo});
l.Add(new IntBase {Prop = 42});

Console.WriteLine(使用每个项目);
foreach(var o in l)
{
o.Use();
}
Console.WriteLine(Done);
Console.ReadKey();


编辑:新增GetProp()方法来说明如何直接从基类访问属性。


I need a base class with a property where I can derive classes with the same property but different (compatible) types. The base Class can be abstract.

public class Base
{
    public virtual object prop { get; set; }
}

public class StrBase : Base
{
    public override string prop { get; set; } // compiler error
}

public class UseIt
{
    public void use()
    {
        List<Base> l = new List<Base>();
        //...
    }
}


I tried it with Generics but that gives me a problem when using the class, because I want to store differently typed base classes in the List.

public class BaseG<T>
{
    public T prop { get; set; }
}

public class UseIt
{
    public void use()
    {
        List<BaseG> l = new List<BaseG>(); // requires type argument
        //...
    }
}

解决方案

Here's an alternative approach to proposed solution:

public abstract class Base
{
    public abstract void Use();
    public abstract object GetProp();
}

public abstract class GenericBase<T> : Base
{
    public T Prop { get; set; }

    public override object GetProp()
    {
        return Prop;
    }
}

public class StrBase : GenericBase<string>
{
    public override void Use()
    {
        Console.WriteLine("Using string: {0}", Prop);
    }
}

public class IntBase : GenericBase<int>
{
    public override void Use()
    {
        Console.WriteLine("Using int: {0}", Prop);
    }
}

Basically I've added a generic class in the middle that stores your properly-typed property. this will work assuming that you never need to access Prop from the code that iterates the members of the List<Base>. (You could always add an abstract method to Base called GetProp that casts the generic to an object if that's required.)

Sample usage:

class Program
{
    static void Main(string[] args)
    {
        List<Base> l = new List<Base>();

        l.Add(new StrBase {Prop = "foo"});
        l.Add(new IntBase {Prop = 42});

        Console.WriteLine("Using each item");
        foreach (var o in l)
        {
            o.Use();
        }
        Console.WriteLine("Done");
        Console.ReadKey();
    }
}

Edit: Added the GetProp() method to illustrate how the property can be directly accessed from the base class.

这篇关于用不同的兼容类型覆盖属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆