当我们可以在项目中使用OverLoading和Overriding方法时

问题描述:

伙计们,我知道那些蔑视和一些例子。但是我们可以使用重载和重写方法。在我的应用程序中我不习惯那些东西。我的意图,我将使用那些东西,但在使用这些东西时,我不知道。

你能不能请任何人帮助我。

Hi guys, I know definations and little bit examples on those. But which purpose we can use the overloading and overriding methods . in my application i am not used of those things. My intension, i will use of those things, but when use of those things, i don't know.
Could you please any one help me .

重载是定义具有相同名称和返回类型但具有不同参数的多个方法的过程。例如,您可能需要一种基于字符串路径返回文件内容的方法:

Overloading is the process of defining a number of methods with the same name and return type, but which has different parameters. For example, you might want a method that returns the file content based on the string path:
public string GetFileContent(string path)
   {
   return File.ReadAllText(path);
   }

但是你也可能会发现定义一个使用FileInfo的方法很有帮助:

But you might also find it helpful to define a method which uses a FileInfo:

public string GetFileContent(FileInfo fi)
   {
   return GetFileContent(fi.FullName);
   }

GetFileContent 是一个重载方法 - 它有不同的版本,调用取决于你提供的paramatyers。在Bad Old Days中,我们必须定义两个不同命名的方法:

GetFileContent is an overloaded method - it has different versions and which is called depends on teh paramatyers you supply. In the Bad Old Days we would have had to define two differently named methods:

GetFileContentFromPath
GetFileContentFromFileInformation

然后尝试记住哪些是可用的,并采取了什么参数!



覆盖更复杂:它是创建一个位于基类中现有方法之上的方法的过程,当它调用时,将调用它而不是基类方法。这允许您有两个不同的派生类,它们都覆盖相同的基类方法来执行特定于类的操作。当您在从基类派生的对象上调用该方法时,该框架将确定它应该调用哪个方法,并对列表中的最高覆盖进行分类。



你可能在没有注意到的情况下使用了这个:ToString经常被覆盖以提供类的人类可读版本。 ToString的默认对象定义打印了类名,但是当它被覆盖时会打印出内容。

And then try to remember which were available, and took what parameters!

Overriding is more complex: it is the process of creating a method that sits on top of an existing method in a base class, and which will be called instead of the base class method when it it called. This allows you to have two different derived classes, which both override the same base class method to do class specific actions. When you call the method on an object derived from the base class, the framework works out which method it should call, and class the "highest" override in the list.

You have probably used this quite a bit without noticing it: ToString is very often overridden to provide a human readable version of the class. The default object definition of ToString prints the class name, but when it it overridden it prints the content.


我们如何使用reverse数组中的方法
how we nwill use revers method in array


我认为方法重载也会减少内存空间。
i think method overloading also reduce memory space.