简体   繁体   English

C#中的返回类型

[英]return type in C#

I have two classes named ROAD and PATH 我有两个名为ROADPATH的

public class ROAD
{
    public string getData()
    {
      return "Marlton Road";
    }
}

public class PATH
{
    public string getData()
    {
        return "Tagore Path";
    }
 }

Also i have a function named FETCH() in my Static Void Main 我在Static Void Main中有一个名为FETCH()的函数

FETCH() contains following code FETCH()包含以下代码

public returnType FETCH(bool flag)
{
    if(flag)
    {
       ROAD obj=new ROAD();
       return obj;
    }
    else
    {
       PATH obj=new PATH();
       return obj;
    }
}

Now my question is what should be the return type of function FETCH(). 现在我的问题是应该是函数FETCH()的返回类型。 Or is there any other way to implement this logic. 或者是否有任何其他方式来实现此逻辑。

It would have to be object in this case, as PATH and ROAD have no common base type. 在这种情况下,它必须是object ,因为PATHROAD没有共同的基类型。 (They also don't follow .NET naming conventions - something which should be fixed, along with your getData method, and your FETCH method. Even in sample code, it's worth trying to make your names follow the normal conventions). (它们也不遵循.NET命名约定 - 应该修复的东西,以及你的getData方法和你的FETCH方法。即使在示例代码中,也值得尝试使你的名字遵循常规约定)。

Consider making the two classes implement an interface or give them a common base class. 考虑使这两个类实现一个接口或为它们提供一个公共基类。 That common type could then be the return type of the method. 那个普通类型可以是该方法的返回类型。 It looks like you could probably have an interface with your getData method in, for example. 例如,看起来你可能在你的getData方法中有一个接口。 Hopefully in your real classes it could have a more meaningful name - something to do with both paths and roads. 希望在你真正的课堂上它可以有一个更有意义的名字 - 与道路和道路有关。

我建议你创建一个PATH和ROAD都实现的接口(例如IGetData)然后让两个类实现它,并让FETCH返回一个IGetData。

Object , and then you cast to ROAD or PATH . Object ,然后你转换为ROADPATH Or, if they share a common base class, return that. 或者,如果他们共享一个共同的基类,则返回该类。 See also . 另见

But you probably don't want to do this. 但你可能不想这样做。 ROAD and PATH should each have their own static factory method: ROAD和PATH应该都有自己的静态工厂方法:

class ROAD {
  static ROAD newRoad() { return new ROAD(); }
}

class PATH {
  static PATH newPath() { return new PATH(); }
}

Or some other, better pattern, depending on why you're doing it this way. 或者其他一些更好的模式,取决于你为什么这样做。

using interface would be the best way: 使用界面将是最好的方法:

public interface IData
{
    string getData();
}

public class ROAD : IData
{
    public string getData()
    {
        return "Marlton Road";
    }
}

public class PATH : IData
{
    public string getData()
    {
        return "Tagore Path";
    }
}

public IData FETCH(bool flag)
{
    if (flag)
    {
        ROAD obj = new ROAD();
        return obj;
    }
    else
    {
        PATH obj = new PATH();
        return obj;
    }
}

How about this: 这个怎么样:

interface IWithData
{
    string GetData();
}

class Path: IWithData
{
    public string GetData()
    {
        return "Tagore Path";
    }
}

class Road: IWithData
{
    public string GetData()
    {
        return "Marlton Road";
    }
}

class SomeOtherClass
{
    // ...
    public IWithData FETCH(bool flag)
    {
        if(flag)
        {
            Road obj=new Road();
            return obj;
        }
        else
        {
            Path obj=new Path();
            return obj;
        }
    }
    // ...
}

It's object . 这是object If you want it to be something else, you just have to have those classes share a common base or implement a common interface. 如果您希望它是其他东西,您只需要让这些类共享一个公共基础或实现一个通用接口。 For example: 例如:

public class Road : IPavedSurface
{     
    // members
}
public class Path : IPavedSurface
{     
    // members
}

// then 
public IPavedSurface Fetch(bool flag)     
{         

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM