有没有一种方法使用具有相同名称的属性,但不同类型的派生类的? [英] Is there a way to use a property with same name but is of different type in derived class?

查看:161
本文介绍了有没有一种方法使用具有相同名称的属性,但不同类型的派生类的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的BaseClass和code期待客户ID为int类型的遗产code。然后,我有一个要求,以创建一个新的类,DerivedClass,其行为非常像BaseClass的,但现在客户ID必须是一个字符串。遗留code不能被修改,使得测试是不需要的。

我如何得到我想要使用继承(或任何其他方式)的影响?

下面Linqpad测试code说明了什么,我需要做的。很显然,这不能编译,因为客户ID在DerivedClass需要为int。我需要叫客户ID和一个字符串属性。它需要这个名字,因为其他code将使用这个类而不是BaseClass的,并期望相同名字的属性,是string类型的。

 公共类BaseClass的
{
    公共虚拟INT客户ID {获得;组;}

    公共无效printname()
    {
       客户id = 1;
       customerid.Dump();
    }
}

公共类DerivedClass:BaseClass的
{
    公众覆盖字符串客户ID {获得;组;}

    公共无效PrintCustomerID()
    {
        客户id =jshwedeX;
        customerid.Dump();
    }
}



无效的主要()
{
    DerivedClass DC =新DerivedClass();
    dc.printname();
}
 

解决方案

您可以使用修改是这样的:

 公共类BaseClass的
{
    公共虚拟INT客户ID {获得;组;}

    公共无效printname()
    {
       客户id = 1;
       customerid.Dump();
    }
}

公共类DerivedClass:BaseClass的
{
    大众新的字符串客户ID {获得;组;}

    公共无效PrintCustomerID()
    {
        客户id =jshwedeX;
        customerid.Dump();
    }
}
 

这会给你想要的结果,但它也将隐藏在基类财产。如果你引用的 DerivedClass 的BaseClass 变量的情况下,你只能够引用属性的基础上,类;不是派生类。

在换句话说:

  BaseClass的实例=新DerivedClass();
字符串客户ID = instance.customerid; //<  - 这将不编译
 

另一种方法是使用显式接口实现:

 公共接口IBase的
{
    INT客户ID {获得;组; }
}

公共接口IDerived经
{
    字符串客户ID {获得;组; }
}

公共类派生:IBase的,IDerived经
{
    INT IBase.customerid {获得;组; }
    字符串IDerived.customerid {获得;组; }
}
 

当您的实例派生存储类型的变量 IBase的客户ID 解析到 INT 的版本,当它被保存在类型的变量 IDerived经,它会解析到字符串版本:

 变种衍生=新派生的();
IBase的基准值=而得;
IDerived经IDerived经=而得;
INT ID1 = ibase.customerid; //<  - 编译就好了
字符串ID2 = iderived.customerid; //<  - 编译就好了
 

您也可以使用铸造:

  VAR实例=新派生的();
INT ID1 =((IBase的)实例).customerid;
字符串ID2 =((IDerived经)实例).customerid;
 

请即显式接口实现导致执行成员不可见,除非该变量是接口类型:

  VAR实例=新派生的();
VAR客户id = instance.customerid; //<  - 这将不编译
 

I have legacy code using the BaseClass and the code is expecting customerid to be of type int. Then I have a requirement to create a new class, DerivedClass, which behaves very much like BaseClass but now customerid needs to be a string. The legacy code can't be modified so that testing is not needed.

How do I get the effect I want using inheritance (or any other way)?

Linqpad test code below illustrates what I need to do. Obviously it won't compile because customerid in DerivedClass needs to be int. I need a string property called customerid as well. It needs to be that name because other code will use this class instead of BaseClass and expects the same named property to be of type string.

public  class  BaseClass
{
    public virtual int customerid {get; set;}

    public void printname()
    {
       customerid = 1;
       customerid.Dump();
    }
}

public class DerivedClass : BaseClass
{
    public override string customerid {get; set;}

    public void PrintCustomerID()
    {
        customerid = "jshwedeX"; 
        customerid.Dump();
    }
}



void Main()
{
    DerivedClass dc = new DerivedClass();
    dc.printname();
}

解决方案

You can use the new modifier like this:

public class BaseClass
{
    public virtual int customerid {get; set;}

    public void printname()
    {
       customerid = 1;
       customerid.Dump();
    }
}

public class DerivedClass : BaseClass
{
    public new string customerid {get; set;}

    public void PrintCustomerID()
    {
        customerid = "jshwedeX"; 
        customerid.Dump();
    }
}

This will give you the desired results, but it will also hide the property on the base class. If you referenced an instance of DerivedClass as a BaseClass variable, you would only be able to reference the property on the base class; not the derived class.

In other words:

BaseClass instance = new DerivedClass();
string customerId = instance.customerid; // <- this won't compile

An alternative would be to use explicit interface implementations:

public interface IBase
{
    int customerid { get; set; }
}

public interface IDerived
{
    string customerid { get; set; }
}

public class Derived : IBase, IDerived
{
    int IBase.customerid { get; set; }
    string IDerived.customerid { get; set; }
}

When your instance of Derived is stored in a variable of type IBase, customerid will resolve to the int version, and when it is stored in a variable of type IDerived, it will resolve to the string version:

var derived = new Derived();
IBase ibase = derived;
IDerived iderived = derived;
int id1 = ibase.customerid; // <- compiles just fine
string id2 = iderived.customerid; // <- compiles just fine

You could also use casting:

var instance = new Derived();
int id1 = ((IBase)instance).customerid;
string id2 = ((IDerived)instance).customerid;

Keep in mind that explicit interface implementations cause the implemented members to not be visible unless the variable is of the interface type:

var instance = new Derived();
var customerid = instance.customerid; // <- this won't compile

这篇关于有没有一种方法使用具有相同名称的属性,但不同类型的派生类的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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