类变量引用另一个类的某个方法 [英] class variable referring to some method of another class

查看:57
本文介绍了类变量引用另一个类的某个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当像流类这样的类变量指的是另一个类的某个方法时,这是什么意思

例如。 stream readdata = response.getresponsestream();

上面的示例实际上来自为从ftp服务器下载数据而编写的代码。我真正想要的是什么要知道什么是返回类型,我完全混淆了解这些代码遇到了很多c#程序。有人可以详细解释我的内部工作。

解决方案

如果您不确定方法的返回方式,请打开Goog​​le并输入名称: response.getresponsestream [ ^ ]

通常在顶部附近,您会找到该方法的MSDN文档:System.Net.WebResponse.GetResponseStream [ ^ ]

这将为您提供有关它的完整详细信息,包括返回类型:

返回值
类型:System.IO.Stream
用于从Internet资源读取数据的Stream类的实例。







@ OriginalGriff:好吧我认为ivé设法理解了一点.stream类是一个抽象类和方法getresponsestream( )响应类只是创建一个流类的实例,并将此实例作为返回类型返回到流类,这是当一个类变量引用另一个类的某个方法时通常会发生的事情。我很困惑的是,如何因为它在c#中是非法的,所以抽象类的实例被创建。



好​​的......让我们看看我是否可以解释......熊和我在一起,没有照片或者能够看到你的眼睛何时开始上釉,这并不容易...



你是对的,你可以''创建一个abstrct类的实例 - 但是你可以从一个抽象的cl创建一个类 派生的 的实例屁股:

  public   abstract   class  MyBase 
{
public abstract void MyMethod();
}
public class MyClass:MyBase
{
public 覆盖 void MyMethod(){ }
}

如果你声明这些类,在你的表单类中,你可以高兴地说:

  private   void  TryThis()
{
MyBase reference;
}



  private   void  TryThis()
{
MyClass reference;
}

并且要么编译。

你不能做的是:

  private   void  TryThis()
{
MyBase reference = new MyBase();
}

因为编译器会(正确地)给你一个错误:

错误1无法创建抽象类的实例或接口'OneOffApp.frmMain.MyBase'C:\ Users \ griff \Documents \ Myyject \OneOffApp\OneOffApp\frmMain.cs 167 32 OneOffApp 

因为MyBase是抽象的。

这意味着抽象类变量(即抽象类引用)是没用的,对吧?

嗯,不 - 实际上它们非常有用。

假设您声明了两个派生类:

  public   abstract   class  MyBase 
{
public abstract string MyMethod();
}
public class MyClass1:MyBase
{
public 覆盖 string MyMethod(){ return MyClass1:MyMethod; }
}
public class MyClass2:MyBase
{
public 覆盖 字符串 MyMethod() { return MyClass2:MyMethod; }
}

如果你随后声明了一个基类(抽象)类的变量,你可以为它分配一个类的实例:

 私有  void  TryThis()
{
MyBase参考;
reference = new MyClass1();
reference = new MyClass2();
}

不仅编译器不会抱怨,你可以调用抽象类的方法,系统会根据你想要的实际方法进行排序,并在相应的派生中调用相应的方法。 class:

  private   void  TryThis( )
{
MyBase参考;
reference = new MyClass1();
Console.WriteLine(reference.MyMethod());
reference = new MyClass2();
Console.WriteLine(reference.MyMethod());
}

将打印:

 MyClass1:MyMethod 
MyClass2:MyMethod

这是您的代码发生了什么:GetResponseStream返回从抽象Stream类派生的类的实例。它可以是MemoryStream,FileStream,XMLStream(如果它们存在) - 您的代码并不关心。只要返回的类实例是从Stream类派生的,您就可以使用Stream的所有方法,而无需知道或关心它是什么类型的流。系统会为您排序。



这是好东西!为什么?因为它允许你编写一个适用于文件的方法(通过FileStream) 而不更改它 ,你的代码可以适用于派生自Stream的任何类:

 Microsoft.JScript.COMCharStream 
System.Data.OracleClient.OracleBFile
System.Data.OracleClient.OracleLob
System.Data.SqlTypes.SqlFileStream
System.IO.BufferedStream
System.IO.Compression.DeflateStream
System.IO.Compression.GZipStream
System.IO.FileStream
System.IO.MemoryStream
System.IO.Pipes.PipeStream
System.IO.UnmanagedMemoryStream
System.Net.Security.AuthenticatedStream
System.Net.Sockets.NetworkStream
System.Printing.PrintQueueStream
System.Security.Cryptography.CryptoStream

是标准的.NET。因此,您的文件保存方法可以保存在加密文件或ZIP存档中,而实际上并不了解它。它的工作原理。



这不仅仅适用于抽象类 - 这种行为连接到整个.NET。抽象类只提供一些预定义行为的契约(与仅提供契约的界面不同)

只要符合合同要求,它就会为你整理出来,并且只是工作。



这使得代码更可靠 - 因为你不需要为文件,拉链编写smae代码记忆,加密数据,......



这有什么意义吗? :笑:


what does it mean when a class variable like of a stream class is referring to some method of another class
eg. stream readdata = response.getresponsestream();
example above is actually from a code written for downloading data from an ftp server.what i actually want to know what is the return type, i''m completely confused with understanding such code that come across many c# programs. can someone explain me in details it''s internal working.

解决方案

If you aren''t sure what a method returns, open Google and type in the name: response.getresponsestream[^]
Normally near the top, you will find the MSDN documentaion for the method: System.Net.WebResponse.GetResponseStream[^]
Which will give you full details about it, including the return type:

Return Value
Type: System.IO.Stream
An instance of the Stream class for reading data from the Internet resource.




"@OriginalGriff:ok i think ivé managed to understand a bit of this.stream class is an abstract class and the method getresponsestream() of the response class just create an instance of stream class and returns this instance to the stream class as return type and this is what usually happens when a class variable refers to some method of another class.what i am confused with is, how an instance of abstract class gets created since its illegal in c#."

OK... Let''s see if I can explain...bear with me, this isn''t easy without pictures or being able to see when your eyes start to glaze over...

You are right that you can''t create an instance of an abstrct class - but you can create an instance of a class derived from an abstract class:

public abstract class MyBase
    {
    public abstract void MyMethod();
    }
public class MyClass : MyBase
    {
    public override void MyMethod() { }
    }

If you declare these classes, in your form class you can happily say:

private void TryThis()
    {
    MyBase reference;
    }

Or

private void TryThis()
    {
    MyClass reference;
    }

And either will compile.
What you can''t do is this:

private void TryThis()
    {
    MyBase reference = new MyBase();
    }

because the compiler will (rightly) give you an error:

Error	1	Cannot create an instance of the abstract class or interface 'OneOffApp.frmMain.MyBase'	C:\Users\griff\Documents\My Projects\OneOffApp\OneOffApp\frmMain.cs	167	32	OneOffApp

Because MyBase is abstract.
Which means abstract class variables (i.e. abstract class references) are pretty useless, right?
Well, no - actually they are very useful.
Assume you declare two derived classes:

public abstract class MyBase
    {
    public abstract string MyMethod();
    }
public class MyClass1 : MyBase
    {
    public override string MyMethod() { return "MyClass1:MyMethod"; }
    }
public class MyClass2 : MyBase
    {
    public override string MyMethod() { return "MyClass2:MyMethod"; }
    }

If you then declare an variable of the base (abstract) class, you can assign an instance of either class to it:

private void TryThis()
    {
    MyBase reference;
    reference = new MyClass1();
    reference = new MyClass2();
    }

And not only will the compiler not complain, you can call the methods of the abstract class and the system will sort out which actual method you wanted and call the appropriate one in the appropriate derived class:

private void TryThis()
    {
    MyBase reference;
    reference = new MyClass1();
    Console.WriteLine(reference.MyMethod());
    reference = new MyClass2();
    Console.WriteLine(reference.MyMethod());
    }

Will print:

MyClass1:MyMethod
MyClass2:MyMethod

This is what is happening with your code: GetResponseStream is returning an instance of a class derived from the abstract Stream class. It could be a MemoryStream, a FileStream, an XMLStream (if they existed) - your code does not care. As long as the class instance that is returned is derived from the Stream class you can use all the methods of the Stream without knowing or caring what kind of stream it is. The system sorts that out for you.

This is good stuff! Why? Because it lets you write a method that works to a file (via a FileStream) and without changing it your code can work to any class derived from Stream:

Microsoft.JScript.COMCharStream
System.Data.OracleClient.OracleBFile
System.Data.OracleClient.OracleLob
System.Data.SqlTypes.SqlFileStream
System.IO.BufferedStream
System.IO.Compression.DeflateStream
System.IO.Compression.GZipStream
System.IO.FileStream
System.IO.MemoryStream
System.IO.Pipes.PipeStream
System.IO.UnmanagedMemoryStream
System.Net.Security.AuthenticatedStream
System.Net.Sockets.NetworkStream
System.Printing.PrintQueueStream
System.Security.Cryptography.CryptoStream

Are the standard .NET ones. So your "file save" method can be saving in an encrypted file, or a ZIP archive without actually knowing anything about it. And it works.

This doesn''t just apply to abstract classes either - this behaviour is "wired in" to the whole of .NET. Abstract classes just provide a "contract" with some predefined behaviours (unlike an Interface which only provides the "contract")
As long as the requirements of the contract are met, it gets sorted out for you, and "just works".

This makes code a lot more reliable - becuase you don''t need to write the smae code for a file, a zip, to memory, to encrypted data, to ...

Did that make any sense? :laugh:


这篇关于类变量引用另一个类的某个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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