H
hualng
Unregistered / Unconfirmed
GUEST, unregistred user!
我编写了一个单元文件:
unit TDateUnit;
interface
uses sysutils;
type
TDate=class
private
fDate:TDateTime;
procedure SetYear(const y:word);
procedure SetMonth(const m:word);
procedure SetDay(const d:word);
function GetYear:word;
function GetMonth:word;
function GetDay:word;
function GetText:string;
public
constructor Create;overload;
constructor Create(y,m,d:integer);overload;
procedure SetDate(y,m,d:integer);overload;
procedure SetDate(aDate:TDateTime);overload;
function Leapyear:boolean;
function DaysAfter(y,m,d:integer):TDateTime;
property Year:word read GetYear write SetYear;
property Month:word read Getmonth write Setmonth;
property Day:word read GetDay write SetDay;
property Text:string read GetText;
end;
implementation
procedure TDate.SetYear(const y:word);
begin
fDate:=EncodeDate(y,Month,Day);
end;
procedure TDate.SetMonth(const m:word);
begin
if (m<=12) then
fDate:=EncodeDate(Year,m,Day)
//else
showmessage ('月份超出范围,请重新输入!');为什么这句不能用?
end;
procedure TDate.SetDay(const d:word);
begin
fDate:=EncodeDate(Year,Month,d);
end;
function TDate.GetYear:word;
begin
GetYear:=Year;
end;
function TDate.GetDay:word;
begin
GetDay:=Day;
end;
function TDate.GetMonth:word;
var y,m,d:word;
begin
DecodeDate(fDate,y,m,d);
result:=m;
end;
function TDate.GetText:string;
begin
GetText:=FormatDateTime('dddddd',fDate);
end;
function TDate.DaysAfter(y,m,d:integer):TDateTime;
var newmon:integer;
begin
year:=year+y+(month+m-1) div 12;
if m>=0
then
month:=(month+m-1) mod 12+1
else
begin
newmon:=(month+m-1) mod 12+1;
while newmon<=0 do
begin
newmon:=(newmon+12-1) mod 12+1;
year:=year-1;
end;
month:=newmon;
end;
fDate:=fDate+d;
result:=fDate;
end;
constructor TDate.Create;
begin
fDate:=Date;
end;
constructor TDate.Create(y,m,d:integer);
begin
fDate:=EncodeDate(y,m,d);
end;
procedure TDate.SetDate(y,m,d:integer);
begin
Year:=y;
Month:=m;
Day:=d;
fDate:=EncodeDate(y,m,d);
end;
procedure TDate.SetDate(aDate:TDateTime);
begin
fDate:=aDate;
end;
function TDate.Leapyear:boolean;
begin
leapyear:=(year mod 400=0) or not (year mod 100=0) and (year mod 4=0);
end;
end.
程序执行时做如下调用:
MonthCalendar1.Date:=aDay.DaysAfter(y,m,d);
可是程序提示“堆栈移出”,怎么办?
unit TDateUnit;
interface
uses sysutils;
type
TDate=class
private
fDate:TDateTime;
procedure SetYear(const y:word);
procedure SetMonth(const m:word);
procedure SetDay(const d:word);
function GetYear:word;
function GetMonth:word;
function GetDay:word;
function GetText:string;
public
constructor Create;overload;
constructor Create(y,m,d:integer);overload;
procedure SetDate(y,m,d:integer);overload;
procedure SetDate(aDate:TDateTime);overload;
function Leapyear:boolean;
function DaysAfter(y,m,d:integer):TDateTime;
property Year:word read GetYear write SetYear;
property Month:word read Getmonth write Setmonth;
property Day:word read GetDay write SetDay;
property Text:string read GetText;
end;
implementation
procedure TDate.SetYear(const y:word);
begin
fDate:=EncodeDate(y,Month,Day);
end;
procedure TDate.SetMonth(const m:word);
begin
if (m<=12) then
fDate:=EncodeDate(Year,m,Day)
//else
showmessage ('月份超出范围,请重新输入!');为什么这句不能用?
end;
procedure TDate.SetDay(const d:word);
begin
fDate:=EncodeDate(Year,Month,d);
end;
function TDate.GetYear:word;
begin
GetYear:=Year;
end;
function TDate.GetDay:word;
begin
GetDay:=Day;
end;
function TDate.GetMonth:word;
var y,m,d:word;
begin
DecodeDate(fDate,y,m,d);
result:=m;
end;
function TDate.GetText:string;
begin
GetText:=FormatDateTime('dddddd',fDate);
end;
function TDate.DaysAfter(y,m,d:integer):TDateTime;
var newmon:integer;
begin
year:=year+y+(month+m-1) div 12;
if m>=0
then
month:=(month+m-1) mod 12+1
else
begin
newmon:=(month+m-1) mod 12+1;
while newmon<=0 do
begin
newmon:=(newmon+12-1) mod 12+1;
year:=year-1;
end;
month:=newmon;
end;
fDate:=fDate+d;
result:=fDate;
end;
constructor TDate.Create;
begin
fDate:=Date;
end;
constructor TDate.Create(y,m,d:integer);
begin
fDate:=EncodeDate(y,m,d);
end;
procedure TDate.SetDate(y,m,d:integer);
begin
Year:=y;
Month:=m;
Day:=d;
fDate:=EncodeDate(y,m,d);
end;
procedure TDate.SetDate(aDate:TDateTime);
begin
fDate:=aDate;
end;
function TDate.Leapyear:boolean;
begin
leapyear:=(year mod 400=0) or not (year mod 100=0) and (year mod 4=0);
end;
end.
程序执行时做如下调用:
MonthCalendar1.Date:=aDay.DaysAfter(y,m,d);
可是程序提示“堆栈移出”,怎么办?