黑马程序员技术交流社区
标题: 对类赋值异常 [打印本页]
作者: 万大述 时间: 2013-6-27 18:11
标题: 对类赋值异常
写一个person类,实例化一个该类的数组chinese,然后分别对数组中的元素赋值,运行时弹出异常,求指导。代码如下:
static void Main(string[] args)
{
person[] chinese = new person[3];
chinese[0].Name = "张?三¨y"; // 错误出在这里,说未将对象引用设置到对象的实例
chinese[0].Sex = "男D";
chinese[0].Age = 18;
chinese[0].SayHello();
}
class person
{
string name;
public string Name
{
get { return name; }
set { name = value; }
}
string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
int age;
public int Age
{
get { return age; }
set { age = value>0?value:0; }
}
public void SayHello()
{
Console.WriteLine("大家好,我叫{0},我是{1}同学,我今年{2}岁了!",name,sex,age);
}
作者: $love 时间: 2013-6-27 18:47
您好,您这样是对对象数组进行初始化! person[] chinese = new person[3]; 里面的对象并未实例化! 你分别对里面的对象进行实例化! 如:chinese[0]=new person();
之后调用对象的属性,或者方法,这样就不会报异常了!
作者: 许庭洲 时间: 2013-6-27 20:14
class Person
{
privatestring m_name = "N/A";
private int m_Age = 0;
//Declare a Name property of type string:
publicstring Name
{
get
{
return m_name;
}
set
{
m_name = value;
}
}
// Declare an Age property of type int:
publicint Age
{
get
{
return m_Age;
}
set
{
m_Age = value;
}
}
publicoverride string ToString()
{
return "Name = " + Name + ", Age = " + Age;
}
}
class TestPerson
{
staticvoid Main()
{
//Create a new Person object:
Person person = new Person();
//Print out the name and the age associated with the person:
System.Console.WriteLine("Person details - {0}", person);
//Set some values on the person object:
person.Name = "Joe";
person.Age = 99;
System.Console.WriteLine("Person details - {0}", person);
//Increment the Age property:
person.Age += 1;
System.Console.WriteLine("Person details - {0}", person);
}
}
Person details - Name = N/A, Age = 0
Person details - Name = Joe, Age = 99
Person details - Name = Joe, Age = 100
作者: 蔓越莓 时间: 2013-6-27 20:46
我是这样理解的,不知道对不对- static void Main(string[] args)
- {
- person[] chinese = new person[3];//这句话是说new了一个person类型的数组实例,但是数组里的元素还是没有实例化
- chinese[0] = new person();//加上这句就不报错了。
- chinese[0].Name = "张?三¨y"; // 错误出在这里,说未将对象引用设置到对象的实例
- chinese[0].Sex = "男D";
- chinese[0].Age = 18;
- chinese[0].SayHello();
- }
复制代码
作者: 万大述 时间: 2013-6-28 08:23
{:soso_e113:}多谢各位的帮助。。。
作者: 关关雎鸠 时间: 2013-6-28 16:31
Person[] chinese = new Person[3];
这样只是定义了一个长度为3的Person数组,但是并没有为每个Person实例化对象,因此当你对Person的属性赋值就会报错(你上面那个错误)。
作者: .netNo1 时间: 2013-7-8 14:04
- static void Main(string[] args)
- {
- //person[] chinese = new person[3];
- person chinese = new person();
- chinese.Name = "张?三¨y"; // 错误出在这里,说未将对象引用设置到对象的实例
- chinese.Sex = "男D";
- chinese.Age = 18;
- chinese.SayHello();
- Console.ReadKey();
- }
- class person
- {
- string name;
- public string Name
- {
- get { return name; }
- set { name = value; }
- }
- string sex;
- public string Sex
- {
- get { return sex; }
- set { sex = value; }
- }
- int age;
- public int Age
- {
- get { return age; }
- set { age = value > 0 ? value : 0; }
- }
- public void SayHello()
- {
- Console.WriteLine("大家好,我叫{0},我是{1}同学,我今年{2}岁了!", name, sex, age);
- }
- }
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |