相关疑难解决方法(0)

C#:覆盖返回类型

有没有办法覆盖C#中的返回类型?如果是这样,如果不是为什么以及推荐的做法是什么?

我的情况是我有一个抽象基类的接口及其后代.我想这样做(确实不是,但作为一个例子!):

public interface Animal
{
   Poo Excrement { get; }
}

public class AnimalBase
{
   public virtual Poo Excrement { get { return new Poo(); } }
}

public class Dog
{
  // No override, just return normal poo like normal animal
}

public class Cat
{
  public override RadioactivePoo Excrement { get { return new RadioActivePoo(); } }
}
Run Code Online (Sandbox Code Playgroud)

RadioactivePoo当然继承自Poo.

我想要这样做的原因是,那些使用Cat对象的人可以使用该Excrement属性而不必Poo投入RadioactivePoo,例如,Cat仍然可能是Animal列表的一部分,用户可能不一定知道或关心他们的放射性便便.希望有意义......

据我所知,编译器至少不允许这样做.所以我想这是不可能的.但是你会推荐什么作为解决方案呢?

c# inheritance types overriding covariance

73
推荐指数
5
解决办法
5万
查看次数

基于返回类型重载方法

全部 - 我的一个课程中有这两种方法:

public static void DeSerializeFromXml(out cDBData db, string pathToXML)
{
    using (XmlTextReader reader = new XmlTextReader(pathToXML))
    {
        XmlSerializer ser = new XmlSerializer(typeof(cDBData));
        db = (cDBData)ser.Deserialize(reader);
    }
}

public static void DeSerializeFromXml(out cDatabases db, string pathToXML)
{
    using (XmlTextReader reader = new XmlTextReader(pathToXML))
    {
        XmlSerializer ser = new XmlSerializer(typeof(cDatabases));
        db = (cDatabases)ser.Deserialize(reader);
    }
}
Run Code Online (Sandbox Code Playgroud)

它们工作正常,但我想知道为什么我不能根据方法的返回类型创建方法重载.我以为我读到这可能在某个地方,但我显然是错的,因为它不起作用:

public static cDBData DeSerializeFromXml(string pathToXML)
{
    cDBData db;

    using (XmlTextReader reader = new XmlTextReader(pathToXML))
    {
        XmlSerializer ser = new XmlSerializer(typeof(cDBData));
        db = (cDBData)ser.Deserialize(reader);
    } …
Run Code Online (Sandbox Code Playgroud)

c# overloading return-value

2
推荐指数
1
解决办法
243
查看次数

为什么这种方法会导致异常超载?

我已经看过很多关于通过更改其返回类型来重载方法的帖子,但是,理想情况下,以下程序应该可以正常工作,因为iinteger类型的变量只能包含整数值.

因此,它理想情况下应该调用函数int print(int a)函数,甚至不看函数,float print(int a)因为它返回一个浮点值,而且main(),我使用了一个整数变量来保存方法返回的值,而整数变量永远不能保持浮点数价值..

以下代码演示了它→

class temp 
{
    public float print(int a) 
    {
        int l=12.55;
        return l;
    }

    public int print(int a) 
    {
        int p=5;
        return p;
    }
}

class Program 
{
    static void Main(string[] args) 
    {
        temp t=new temp();
        int i=t.print(10);
        A.Read();
    }
}
Run Code Online (Sandbox Code Playgroud)

在其他情况下,我做这样的事情→

class temp 
{
    public float print(int a) 
    {
        int l=12.55;
        return l;
    }

    public int print(int a) 
    {
        int p=5;
        return p;
    }
} …
Run Code Online (Sandbox Code Playgroud)

c# oop overloading

2
推荐指数
2
解决办法
175
查看次数

按返回类型重载?

public class test
{
    public void xov() { return; }
    public string xov() { return null; }
    public int xov() { return 0; } 
}

public class p
{
    public p()
    {
        test v = new test();
        v.xov();// Here i cann't use defined overloads :(  
    }   
} 
Run Code Online (Sandbox Code Playgroud)

我可以通过参数重载方法,但为什么我不能通过返回类型重载方法?

.net c# syntax-error

2
推荐指数
1
解决办法
98
查看次数