本帖最后由 gyt_xtt 于 2014-4-14 12:06 编辑
- class Program
- {
- static void Main(string[] args)
- {
- Person p1 = new Person();
- Person p2 = new Person("tom");
- Person p3 = new Person("jerry",18);
- Console.WriteLine("年龄是{0},名字叫{1}",p1.Age,p1.Name);
- Console.WriteLine("年龄是{0},名字叫{1}", p2.Age, p2.Name);
- Console.WriteLine("年龄是{0},名字叫{1}", p3.Age, p3.Name);
- Console.ReadKey();
- }
- }
- class Person
- {
- public Person()
- {
- Name = "未命名";
- Age = 0;
- }
- public Person(string name)
- {
- this.Name = name;
- }
- public Person(string name, int age)
- {
- this.Name = name;
- this.Age = age;
- }
- private string name;
- private int age;
- public string Name
- {
- get
- {
- return name;
- }
- set
- {
- this.name = value;
- }
- }
- public int Age
- {
- get
- {
- return age;
- }
- set
- {
- this.age = value;
- }
- }
- }
复制代码
这个输出之后,为什么p2的年龄显示为0.
p2明明调用了
public Person(string name)
{
this.Name = name;
}
这个构造函数,这里没有年龄,为什么显示年龄是0? |