【发布时间】:2009-04-25 13:02:13
【问题描述】:
考虑这个类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Game.Items
{
class Item
{
private string name;
public string Name
{
get { return this.name; }
}
private string description;
public string Description
{
get { return this.description; }
}
public Item(string name, string description)
{
this.name = name;
this.description = description;
}
public override string ToString()
{
return this.name;
}
}
}
然后我像这样创建一个新对象:
Item item1 = new Item("Item1", "Description...");
现在的问题是,我无法使用 getter 方法访问对象的属性,即这不起作用:
Console.WriteLine(item1.Name);
Console.WriteLine(item1.Description);
Console.ReadLine();
“不起作用”是指当我单击“开始调试”时,控制台窗口出现但什么也没有,它是空白的。但是,这很好用:
Console.WriteLine(item1); // ToString()
Console.ReadLine();
我做错了什么?
理查德
【问题讨论】:
-
如果您还没有,请尝试安装 Resharper 的 30 天免费试用版 - 它会检测并提供修复许多问题,例如命名空间问题、保护问题等等......以及建议良好的命名约定 - 很好的学习材料。
-
当你说“这行不通”时,你到底是什么意思?编译器错误?运行时错误?结果不正确?崩溃了?
-
换行符是打印还是什么也不打印?
Console.ReadLine怎么样?有用吗? -
请粘贴一个完整的演示问题的程序,换句话说,将所有内容放在一个文件中,删除演示问题不需要的所有内容,验证您仍然有问题,然后将其粘贴到这里.
-
建议关闭“不再相关”的 asn,因为他有一个特定于他的完整问题的答案(他没有发布),遇到同样问题的人不会偶然发现这个问题.此外,主题行不好。我可能不得不将“前 10 名最糟糕的主题行”更改为“前 20 名”。
标签: c# .net visual-studio-2008 console getter