C Sharp中的对象实例化 [英] Object instantiation in C Sharp

查看:160
本文介绍了C Sharp中的对象实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我是对象编程的新手,我对该主题有疑问:



我明白当我们实例化一个对象时,我们这样做:



 ClassName objectName =  new  ClassName(); 





以下代码行是什么意思:



 TextReader reader =  new  StreamReader(filename); 





看起来像对象实例化,我错了吗?



感谢您的帮助

解决方案

 TextReader reader =  new  StreamReader(filename); 



StreamReader 类的对象实例化为名为<$的 TextReader 变量C $ C>读取器。



使用StreamReader变量可能会更好。

 StreamReader reader =  new  StreamReader(filename); 





StreamReader Class继承自 TextReader 类。 StreamReader 类具有其他功能,如果 TextReader 类用于变量数据类型,则无法使用这些功能。



参见 StreamReader类 [ ^ ]


你是对的。它实例化一个StreamReader。


大多数情况下,我们使用类名来定义变量的类型,并在实例化对象时使用new关键字构造函数。一个类可以有很多构造函数....你可以根据需要选择任何一个。当你在第一个代码行的左边写constructor1时,我也编辑了这个问题。它应该是班级名称。我理解混淆的原因:构造函数是具有相同名称的类中的方法。在C#....中如果你没有为你的对象提供一个构造函数,C#默认会创建一个实例化对象。

如需了解更多信息,请查看:



http://msdn.microsoft.com/ en-us / library / ms173115.aspx [ ^ ]



C#中的构造函数介绍 [ ^ ]



正如其他人已确认的那样......你是对的...... StreamReader是实例化...在你的情况下,它使用一个特定的构造函数,它采用文件名。在RHS上,你有 TextReader 因为TextReader是StreamReader的父类,所以你要创建SreamReader的实例并将它放入一个TextReader类型的变量中。

公共类StreamReader:TextReader 





您的代码TextReader reader = new StreamReader(filename); .....相当于以下代码:

 TextReader阅读器; 
StreamReader s1;
s1 = new StreamReader(filename);
reader = s1;



http://msdn.microsoft.com/en-us/library/system.io.streamreader(v = vs.110)的.aspx [<一个href =http://msdn.microsoft.com/en-us/library/system.io.streamreader(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ],StremReader类有很多构造函数,并继承自TextReader。

随意讨论更多。感谢。


Hello everyone,

I am new to object programming, and I have a question in that topic:

I understand that when we instantiate an object we do it like so:

ClassName objectName= new ClassName();



What does the following line of code mean:

TextReader reader = new StreamReader(filename);



It looks like an object instantiation, am I wrong ?

Thanks for your help

解决方案

TextReader reader = new StreamReader(filename);


Instantiates an object of the StreamReader Class into a TextReader variable named reader.

It would probably be better to use a StreamReader variable.

StreamReader reader = new StreamReader(filename);



The StreamReader Class is inherited from the TextReader Class. The StreamReader Class has additional features that won't be available if the TextReader Class is used for the variable data type.

See StreamReader Class[^]


You are correct. It instantiates a StreamReader.


Most cases, we use class name to define type of a variable and one of its constructor with new keyword while instantiation of an object. A class can have many constructors....you can pick any based on your need. I have edited the question also as you wrote "constructor1" at left hand side of first code line. It should be class name. I understand the reason of confusion: "The constructor is a method in a class with same name". And in C#...."If you do not provide a constructor for your object, C# will create one by default that instantiates the object".
For more, please have a look on:

http://msdn.microsoft.com/en-us/library/ms173115.aspx[^]
and
An Intro to Constructors in C#[^]

And as already other fellows confirmed...you are right...that StreamReader is getting instantiated...in your case it is using a particular constructor which takes file name. And on RHS, you have TextReader because TextReader is a parent class of StreamReader so you are creating the instance of SreamReader and putting it into a variable which is a type of "TextReader".

public class StreamReader : TextReader



Your code "TextReader reader = new StreamReader(filename);"..... is equivalent to below code:

TextReader reader;
StreamReader s1;
s1 = new StreamReader(filename);
reader = s1;


As given at http://msdn.microsoft.com/en-us/library/system.io.streamreader(v=vs.110).aspx[^], StremReader class is having many constructors and inherited from "TextReader".
Feel free to discuss more. Thanks.


这篇关于C Sharp中的对象实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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