C#的委托如何实现JAVA中的协变?

eRemember 2012-12-27 02:29:31


C#中如何实现JAVA中的协变?

最近需要将JAVA代码改写成C#,但是在处理中遇到了协变的问题,原始的JAVA代码是这样的:
基类:
public abstract class DataElement {
public abstract boolean equals(DataElement otherElement);
public abstract int compareTo(DataElement otherElement);
public abstract void makeCopy(DataElement otherElement);
public abstract DataElement getCopy();
public abstract DataElement properSlot(DataElement searchItem);
}
子类(在重写基类方法getCopy()的时候,其返回值使用了协变,返回的是基类返回值类型的子类型):
public class TaskAttribute extends DataElement{

public TaskAttribute getCopy(){
TaskAttribute temp = new TaskAttribute(index, bLevel);
return temp;
}

}

在C#中如何实现这样的协变呢???使用的是.NET 3.5。

我自己查了资料后写了一个尝试,虽然没有通过编译,但是还是贴出来,勿喷。
基类:
public delegate DataElement delegateDataElement();
public abstract class DataElement
{
public abstract Boolean equals(DataElement otherElement);
public abstract int compareTo(DataElement otherElement);
public abstract void makeCopy(DataElement otherElement);
public abstract delegateDataElement getCopy();
public abstract DataElement properSlot(DataElement searchItem);
}
子类:
public class TaskAttribute : DataElement{

public override delegateDataElement getCopy()
{
TaskAttribute temp = new TaskAttribute(index, bLevel);
return temp;
}

}

诚心请教高手,分不多,谢谢!
...全文
134 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2012-12-27
  • 打赏
  • 举报
回复
C# 4.0开始才支持协变,而且只支持接口和委托的协变。 你可以用泛型代替。
mumuanka 2012-12-27
  • 打赏
  • 举报
回复



class BaseClass 
{ 
    public virtual  BaseClass getCopy1() { return new BaseClass(); }
    public virtual BaseClass getCopy2() { return new BaseClass(); }
}
class NewClass : BaseClass
{
    public override BaseClass getCopy1() { return new NewClass(); }
    public new NewClass getCopy2() { return new NewClass(); }
}


        BaseClass b = new BaseClass();
        Response.Write("b:" + b.GetType().Name + "<br/>");
        var b_copy1 = b.getCopy1();
        Response.Write("b_copy1:" + b_copy1.GetType().Name + "<br/>");
        var b_copy2 = b.getCopy2();
        Response.Write("b_copy2:" + b_copy2.GetType().Name + "<br/>");

        var n = new NewClass();
        Response.Write("n:" +n .GetType().Name + "<br/>");
        var n_copy1 = n.getCopy1();
        Response.Write("n_copy1:" +n_copy1 .GetType().Name + "<br/>");
        var n_copy2 = n.getCopy2();
        Response.Write("n_copy2:" + n_copy2.GetType().Name + "<br/>");

        BaseClass nb = new NewClass();
        Response.Write("nb:" + nb.GetType().Name + "<br/>");
        var nb_copy1 = nb.getCopy1();
        Response.Write("nb_copy1:" + nb_copy1.GetType().Name + "<br/>");
        var nb_copy2 = nb.getCopy2();
        Response.Write("nb_copy2:" + nb_copy2.GetType().Name + "<br/>");

结果: b:BaseClass b_copy1:BaseClass b_copy2:BaseClass n:NewClass n_copy1:NewClass n_copy2:NewClass nb:NewClass nb_copy1:NewClass nb_copy2:BaseClass 不知你是否这个意思?

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧