[初学者问题]实例化一个对象 [英] [Beginner question] instantiate an object
问题描述
大家好,
当一个对象被实例化时我们放弃了:
person p = new person( john, doe, 23 );
但有时它会直接写成
p = 新人( john, doe, 23 );
在p之前没有人物前缀,是正常行为吗?
感谢您的帮助:)
person p;
可能已在
p = 的行之前声明新人( john, doe, 23 );或作为班级字段或者属性。
然后只有你可以将新对象分配给p
,否则你会得到编译错误。
更新:
引用:你能告诉我们类级字段或属性是什么意思?
方法级字段示例:
public < span class =code-keyword> void MyMethod()
{
person p;
// 一些代码行..........
p = 新人( john , doe, 23 );
// 一些代码行..........
}
班级字段:
public class Myclass
{
public person p;
public void MyMethod()
{
// 一些代码行..........
p = new person( john, doe, 23 );
// 一些代码行..........
}
}
班级物业
<前lang =c#> public class Myclass
{
public person p {< span class =code-keyword> get ; set ;}
public void MyMethod()
{
// 一些代码行..........
p = 新人( john, doe, 23 );
// 一些代码行..........
}
}
http: //msdn.microsoft.com/en-us/library/bb384054.aspx [ ^ ]
这是正常的,但仅在使用数据类型创建该p变量以存储此人的情况下在里面。像这样,
person p;
// 添加值
p = new person( john, doe, 23 );
Hello everyone,
when an object is instantiated we put for ex:
person p = new person("john","doe",23);
but sometimes it is writen directly like that
p = new person("john","doe",23);
without the person prefix before p, is that normal behavior ?
Thanks for your help :)
person p;
may have declared before the line of
p = new person("john","doe",23);or as class level field or property.
then only you can assign new object top
, otherwise you will get compile error.
update:
Quote:can you please tell what means class level field or property ?
method level field example:
public void MyMethod() { person p; // some code lines .......... p = new person("john","doe",23); // some code lines .......... }
class level field:
public class Myclass { public person p; public void MyMethod() { // some code lines .......... p = new person("john","doe",23); // some code lines .......... } }
class level property
public class Myclass { public person p {get; set;} public void MyMethod() { // some code lines .......... p = new person("john","doe",23); // some code lines .......... } }
Yes that is normal, but only in case if that p variable has been created with the data type to store the person in it. Like this,
person p; // add values p = new person("john","doe",23);
这篇关于[初学者问题]实例化一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!