正确的方法来创建类的对象? [英] correct way to create an object of the class?

查看:84
本文介绍了正确的方法来创建类的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建类Sample的对象的正确方法..



示例s = new Sample();









样品s;

s =新样品();






是否存在差异或优势我们正在为班级创造对象......





提前感谢...

correct way to create an object of the class Sample..

Sample s = new Sample();


or

Sample s;
s = new Sample();



is there any difference or advantages in the way we are creating object to the class...


thanks in advance...

推荐答案

AFAIK,两者之间没有区别。

在第一个,你只需在声明对象后立即实例化它。

在第二个,你首先声明你的对象,然后你在下一行实例化它。



我总是使用第二个(我真的不知道)为什么)也许是因为你可以在需要实例化的事件上实例化它。示例:

AFAIK, there is no difference between the two.
On the first one, you just immediately instantiate it after you declare your object.
On the second one, you declare your object first, then you instantiate it on the next line.

I always use the second one ( which I really don't know why) maybe because you can instantiate it on which event you needed it to be instantiated. Example:
Sample s;

private void btnClick_click (object sender, EventArgs e)
{
//you don't need the object here yet..
//add code here
}

private void btnSubmit_Click (object sender, EventArgs e)
{
//in this event you need to instantiate your object.
s = new Sample();
//add code here
}


我尽可能使用第一个;第二次,当我不得不。



我永远不会像你的例子那样在连续的线上做第二次;总会有一些东西(通常是范围界定的)来分隔陈述。
I use the first when I can; the second when I have to.

I would never do the second on consecutive lines like your example; there would always be something (usually scoping) to separate the statements.


这篇关于正确的方法来创建类的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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