using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplicationTest
{
class Program
{
struct Date
{
public Date(int ccyy, int mm, int dd)
{
this.year = ccyy - 1900;
this.month = mm;
this.day = dd - 1;
}
private int year;
private int month;
private int day;
public override string ToString()
{
return this.month + "" + (this.day + 1) + "" + (this.year + 1900);
}
}
static void Main(string[] args)
{
Date defaulDate = new Date();
Console.WriteLine(defaulDate);
Console.ReadLine();
}
}
}
为什么结果是011990,
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplicationTest
{
class Program
{
struct Date
{
public Date(int ccyy, int mm, int dd)
{
this.year = ccyy - 1900;
this.month = mm;
this.day = dd - 1;
}
private int year;
private int month;
private int day;
public override string ToString()
{
return this.month + "" + (this.day + 1) + "" + (this.year + 1900);
}
}
static void Main(string[] args)
{
Date defaulDate = new Date();
Console.WriteLine(defaulDate);
Console.ReadLine();
}
}
}
为什么结果是011990,
你调用了默认的构造函数,
这个使每个成员初始化为对应类型的默认值。
你的成员都是int,所以都为0
当
WriteLine的时候,这个defaulDate 隐式转换为object,
因为WriteLine方法参数没有跟结构对应的参数,
所以转为object
然后调用你覆盖的方法toString.
{
return this.month + "" + (this.day + 1) + "" + (this.year + 1900);
} 这里的先后顺序不一样
初始化所有成员 int 为0,
但此时
public Date(int ccyy, int mm, int dd)
{
this.year = ccyy - 1900;
this.month = mm;
this.day = dd - 1;
}
ccyy mm dd 是否为0 ?
为什么不是 year=-1900 day= -1?
初始化所有成员 int 为0,
但此时
public Date(int ccyy, int mm, int dd)
{
this.year = ccyy - 1900;
this.month = mm;
this.day = dd - 1;
}
ccyy mm dd 是否为0 ?
为什么不是 year=-1900 day= -1?
[/Quote]
ccyy,mm,dd是另一个构造函数的参数..
和默认构造函数没有任何关系型
既然调用了默认构造函数,你的这个构造函数如何操作就都是纸上谈兵了,用不到了