简体   繁体   中英

return type in C#

I have two classes named ROAD and PATH

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

FETCH() contains following code

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(). 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. (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).

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. 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 . 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:

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 . 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)     
{         

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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