在C#中返回一个类 [英] return a class in C#

查看:225
本文介绍了在C#中返回一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个名为 ROAD PATH
的类

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";
    }
}


我的静态虚空主目录中也有一个名为FETCH()的函数

FETCH()包含以下代码


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;
    }
}



现在我的问题是函数FETCH()的返回类型应该是什么.
还是有其他方法可以实现此逻辑.



Now my question is what should be the return type of function FETCH().
Or is there any other way to implement this logic.

推荐答案

您可以通过某些方式进行处理.

一种方法是返回一个对象.
然后在调用方中,只需检查返回类型是PATH还是ROAD.

如果两种方法之间存在某些通用功能,则另一个选择是使用接口.在这两个类(ROAD和PATH)中都实现此接口.在FETCH方法中返回接口类型.
You can handle this in some ways.

One way to do this is to return an object.
Then in the caller, simply check if the return type is PATH or ROAD.

The other option could be to use an interface if there is some common functionality between the two methods. Implement this interface in both these classes (ROAD and PATH). Return the interface type in the FETCH method.


通常,您正在执行的操作不是一个好主意,并且不是面向对象的.

为了进一步迭代Abhinav S所说的话:

使用对象的三种方法.
1)您不在乎对象类型是(return object).
2)您在乎类型是什么(return a base class).
3)您关心对象(return a specific type)的详细信息.

例如,假设您是动物园的服务员,而老板说
1)去看看笼子里有19个东西,所以你去那里照相,看看那里的斑块,然后交给老板. (您返回object并且老板知道该怎么做)
2)看看笼子19中的动物是否还活着,您呼叫Animal.isAlive()并检查返回的不是null.
3)去在19号笼子里喂动物,所以必须知道动物的种类,如果是猴子,就喂香蕉,如果是狮子等,喂肉.
Generally what you are doing is a bad idea and is not object orientated.

To further iterate what Abhinav S has said:

There are three ways you use objects.
1) You don''t care what the object type is (return object).
2) You care what the type is to a degree (return a base class).
3) You care about the details of the object (return a specific type).

For example, say you are a handler in a zoo, and your boss says
1) Go and see what we have in cage 19, so you go there and take a picture of the plaque there and give it to your boss. (you return an object and your boss know what to do with it)
2) See if the animal in cage 19 is alive, You call a Animal.isAlive() and check the return is not null.
3) Go and feed the animal in cage 19, so you must know what the animal type is and feed it bananas if it is a monkey, meat if it is a lion etc.


以上答案全面涵盖了您的问题.这种类型的要求为程序员提供了提高他们的面向对象技能的机会.

每当您遇到面向对象编程"时,请考虑以下设计原则".

" 编程到接口,而不是实现. "
-摘自精彩的书-" Head First设计模式"-精彩的作家Eric Freeman和Elisabeth Freeman.

让我们定义一个接口IWay.
Above answers covers your Question comprehensively. This type of requirements provides an opportunity to Programmer to improve their Object Oriented skills.

Please consider below "Design Principle" whenever you come across "Object Oriented Programming".

"Program to an interface, not an implementation."
- From the wonderful Book - "Head First Design Patterns" - By wonderful writers Eric Freeman and Elisabeth Freeman.

Lets define an Interface IWay.
public interface IWay
{
  string getData();

}


现在定义一个类-Road,它继承了接口-IWay.


Now define a Class - Road, which inherits the Interface - IWay.

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


让我们定义一个类-PATH,它也继承接口-IWay.


Lets define a Class- PATH, which too inherits the Interface - IWay.

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


是时候声明和定义FETCH方法了.如下图所示.


It''s time to declare and define FETCH Method. It will look like below.

public IWay FETCH(bool flag)
{
  IWay objectToReturn = null;
  if (flag)
  {
    objectToReturn = new ROAD();
  }
  else
  {
    objectToReturn = new PATH();
  }

  return objectToReturn;
}


如果您观察到Fetch方法的返回类型为接口IWay的类型.方法Fetch将根据您的条件通过任何一个类实例化接口IWay的对象.

从头开始的设计模式》一书中的另一个设计原理如下.

无论您在哪里工作,正在构建什么或正在使用哪种语言编程,一个永远与您在一起的真正常数是什么?"

答案是-更改

假设将来您会再获得一个称为" STREET "的实体.

在这种情况下,您所要做的如下.


if you observe return Type of Fetch method is Type of Interface IWay. And method Fetch will instantiate object of Interface IWay by either of your Class based on your condition.

One more Design Principle from "Head First Design Patterns" Book is as below.

"No matter where you work, what you''re building, or what language you are programming in, what''s the one true constant that will be with you always?"

Answer is - CHANGE

Lets assume in future you get one more Entity called as "STREET".

In that case all you have to do is as below.

public class STREET : IWay
{
  public string getData()
  {
    return "Canal Street";
  }
}



希望这些信息对您有所帮助. :).感谢您发布如此好的问题.

更新-再次提及书名-" Head First设计模式"在此要对优秀作家Eric Freeman和Elisabeth Freeman的精彩作品"设计原则".



Hope this information helps you. :). Thank you for posting such a good Question.

Updated - Reeason to mention name of Book - "Head First Design Patterns" here is to give a sincere credit to wonderful writers Eric Freeman and Elisabeth Freeman for their wonderful "Design Principles".


这篇关于在C#中返回一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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