标题:[求助]问一个关于C#重载的问题!
取消只看楼主
zone0356224
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2007-2-1
 问题点数:0 回复次数:10 
[求助]问一个关于C#重载的问题!

我是初学者,对重载很不了解,但是作业要求又要在函数中实现重载,所以特来请教!!!
希望大家帮帮我!!!谢谢!!!!
这是我写的一个类,里面有函数,算法。(算法是经济预测里面的一次指数平滑和二次指数平滑)
intArrayNumber:数据的组数。
decWeight:加权系数。
strTime:时间。
decQuantity:实际需求量。
decInitial1:一次平滑初始值。
decInitial2:二次平滑初始值。
这个 method_once(int intArrayNumber,decimal decWeight,string strTime,decimal decQuantity,decimal decInitial1)
算是重载么????
我希望能够写出下面有重载要求的函数。
method(int intArrayNumber,decimal decWeight,string strTime,decimal decQuantity,decimal decInitial1)
时函数计算一次指数平滑。
method(int intArrayNumber,decimal decWeight,string strTime,decimal decQuantity,decimal decInitial1,decimal decInitial2)
时函数计算二次指数平滑。


我先写了类,然后在Button事件中调用类中的函数。
下面是一次指数平滑的代码:
using System;

namespace ForecastSystem
{
/// <summary>
/// method_weight 的摘要说明。
/// </summary>
public class method_once
{
//private variables to hold class properties
private string strTime;
private int intArrayNumber;
private decimal decQuantity,decWeight,decInitial1,decSingleTotal,decResult,decMSE;
private static int intCount;
private static decimal []decSingle=new decimal[100];
private static decimal []decTotal_number=new decimal1[100];

public method_once(int intArrayNumber,decimal decWeight,string strTime,decimal decQuantity,decimal decInitial1)
{
//
this.Time=strTime;
this.Quantity=decQuantity;
this.Weight=decWeight;
this.Initial=decInitial1;
this.ArrayNumber=intArrayNumber;
AddTotals();
Add_number();
Forecast();
CalculateMSE();
//
}

public string Time
{
get
{
return strTime;
}
set
{
strTime=value;
}
}

public int ArrayNumber
{
get
{
return intArrayNumber;
}
set
{
intArrayNumber=value;
}
}

public decimal Initial
{
get
{
return decInitial;
}
set
{
decInitial=value;
}
}

public decimal Quantity
{
get
{
return decQuantity;
}
set
{
decQuantity=value;
}
}

public decimal Weight
{
get
{
return decWeight;
}
set
{
decWeight=value;
}
}

public decimal SingleTotal
{
get
{
return decSingleTotal;
}
set
{
decSingleTotal=value;
}
}

public decimal Result
{
get
{
return decResult;
}
set
{
decResult=value;
}
}

public decimal MSE
{
get
{
return decMSE;
}
set
{
decMSE=value;
}
}

public static int Count
{
get
{
return intCount;
}
}

protected void AddTotals()
{
decTotal_number1[0]=decInitial;
decSingle[intCount]=decQuantity;
intCount++;

}

protected void Add_number()
{
if(intArrayNumber<=intCount)
{
for(int a=0;a<intArrayNumber;a++)
{
decTotal_number1[a+1]=decWeight*decSingle[a]+(1-decWeight)*decTotal_number1[a];
}
}
}

protected void Forecast()
{
decResult=decTotal_number1[intArrayNumber];
}

protected void CalculateMSE()
{
if(intArrayNumber<=intCount)
{
for(int a=0;a<intArrayNumber;a++)
{
decSingleTotal+=(decSingle[a]-decTotal_number1[a])*(decSingle[a]-decTotal_number1[a]);
}
decMSE=decSingleTotal/intArrayNumber;
decSingleTotal=0;
}
}
}
}
下面是二次指数平滑的代码
using System;

namespace ForecastSystem
{
/// <summary>
/// method_weight 的摘要说明。
/// </summary>
public class method_twice
{
//private variables to hold class properties
private string strTime;
private int intArrayNumber;
private decimal decQuantity,decWeight,decInitial1,decInitial2,decSingleTotal,decResult,decMSE;
private static int intCount;
private static decimal []decSingle=new decimal[100];
private static decimal []decTotal_number1=new decimal[100];
private static decimal []decTotal_number2=new decimal[100];

public method_twice(int intArrayNumber,decimal decWeight,string strTime,decimal decQuantity,decimal decInitial1,decimal decInitial2)
{
//
this.Time=strTime;
this.Quantity=decQuantity;
this.Weight=decWeight;
this.Initial1=decInitial1;
this.Initial2=decInitial2;
this.ArrayNumber=intArrayNumber;
AddTotals();
Add_number();
Forecast();
CalculateMSE();
//
}

public string Time
{
get
{
return strTime;
}
set
{
strTime=value;
}
}

public int ArrayNumber
{
get
{
return intArrayNumber;
}
set
{
intArrayNumber=value;
}
}

public decimal Initial1
{
get
{
return decInitial1;
}
set
{
decInitial1=value;
}
}

public decimal Initial2
{
get
{
return decInitial2;
}
set
{
decInitial2=value;
}
}

public decimal Quantity
{
get
{
return decQuantity;
}
set
{
decQuantity=value;
}
}

public decimal Weight
{
get
{
return decWeight;
}
set
{
decWeight=value;
}
}

public decimal SingleTotal
{
get
{
return decSingleTotal;
}
set
{
decSingleTotal=value;
}
}

public decimal Result
{
get
{
return decResult;
}
set
{
decResult=value;
}
}

public decimal MSE
{
get
{
return decMSE;
}
set
{
decMSE=value;
}
}

public static int Count
{
get
{
return intCount;
}
}

protected void AddTotals()
{
decTotal_number1[0]=decInitial1;
decTotal_number2[0]=decInitial2;
decSingle[intCount]=decQuantity;
intCount++;

}

protected void Add_number()
{
if(intArrayNumber<=intCount)
{
for(int a=0;a<intArrayNumber;a++)
{
decTotal_number1[a+1]=decWeight*decSingle[a]+(1-decWeight)*decTotal_number1[a];
decTotal_number2[a+1]=decWeight*decTotal_number1[a]+(1-decWeight)*decTotal_number2[a];
}
}
}

protected void Forecast()
{
decResult=decTotal_number2[intArrayNumber];
}

protected void CalculateMSE()
{
if(intArrayNumber<=intCount)
{
for(int a=0;a<intArrayNumber;a++)
{
decSingleTotal+=(decSingle[a]-decTotal_number2[a])*(decSingle[a]-decTotal_number2[a]);
}
decMSE=decSingleTotal/intArrayNumber;
decSingleTotal=0;
}
}
}
}



搜索更多相关主题的帖子: 重载 decimal 需求量 decQuantity decWeight 
2007-02-01 14:55
zone0356224
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2007-2-1
得分:0 
不好意思,急啊!!!其实主要呈现都差不多,主要看二次吧!!!
一有的二基本都有,就是加个参数decInitial2后,改变算法!

2007-02-01 15:06
zone0356224
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2007-2-1
得分:0 
我就是希望能把两个类中的2个函数,弄成一个可以重载的函数。
当调用函数的参数为
method(int intArrayNumber,decimal decWeight,string strTime,decimal decQuantity,decimal decInitial1)时函数计算一次指数平滑。
当调用函数的参数为
method(int intArrayNumber,decimal decWeight,string strTime,decimal decQuantity,decimal decInitial1,decimal decInitial2)时函数计算二次指数平滑。

2007-02-01 15:17
zone0356224
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2007-2-1
得分:0 
但是怎么改呢?
他们两个在两个类里面阿??

2007-02-01 15:18
zone0356224
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2007-2-1
得分:0 
怎么拉阿???完全不明白
我不会阿!能教我下么?

2007-02-01 15:38
zone0356224
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2007-2-1
得分:0 
讲一下怎么操作可以么??
没接触过这个阿!!
完全找不到方向。

2007-02-01 15:46
zone0356224
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2007-2-1
得分:0 
你能先把方法说的仔细点么??
还有就是直接重载是什么??

2007-02-01 15:54
zone0356224
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2007-2-1
得分:0 

难道说版主的意思是这样写?
using System;

namespace ForecastSystem
{
/// <summary>
/// method_weight 的摘要说明。
/// </summary>
public class method_twice
{
//private variables to hold class properties
private string strTime;
private int intArrayNumber;
private decimal decQuantity,decWeight,decInitial1,decInitial2,decSingleTotal,decResult,decMSE;
private static int intCount;
private static decimal []decSingle=new decimal[100];
private static decimal []decTotal_number1=new decimal[100];
private static decimal []decTotal_number2=new decimal[100];

public method(int intArrayNumber,decimal decWeight,string strTime,decimal decQuantity,decimal decInitial1)
{
//
this.Time=strTime;
this.Quantity=decQuantity;
this.Weight=decWeight;
this.Initial1=decInitial1;
this.ArrayNumber=intArrayNumber;
AddTotals1();
Add_number1();
Forecast1();
CalculateMSE1();
//
}
public method(int intArrayNumber,decimal decWeight,string strTime,decimal decQuantity,decimal decInitial1,decimal decInitial2)
{
//
this.Time=strTime;
this.Quantity=decQuantity;
this.Weight=decWeight;
this.Initial1=decInitial1;
this.Initial2=decInitial2;
this.ArrayNumber=intArrayNumber;
AddTotals2();
Add_number2();
Forecast2();
CalculateMSE2();
//
}
......
protected void AddTotals1()
{
decTotal_number1[0]=decInitial;
decSingle[intCount]=decQuantity;
intCount++;

}

protected void Add_number1()
{
if(intArrayNumber<=intCount)
{
for(int a=0;a<intArrayNumber;a++)
{
decTotal_number1[a+1]=decWeight*decSingle[a]+(1-decWeight)*decTotal_number1[a];
}
}
}

protected void Forecast1()
{
decResult=decTotal_number1[intArrayNumber];
}

protected void CalculateMSE1()
{
if(intArrayNumber<=intCount)
{
for(int a=0;a<intArrayNumber;a++)
{
decSingleTotal+=(decSingle[a]-decTotal_number1[a])*(decSingle[a]-decTotal_number1[a]);
}
decMSE=decSingleTotal/intArrayNumber;
decSingleTotal=0;
}
}

protected void AddTotals2()
{
decTotal_number1[0]=decInitial1;
decTotal_number2[0]=decInitial2;
decSingle[intCount]=decQuantity;
intCount++;

}

protected void Add_number2()
{
if(intArrayNumber<=intCount)
{
for(int a=0;a<intArrayNumber;a++)
{
decTotal_number1[a+1]=decWeight*decSingle[a]+(1-decWeight)*decTotal_number1[a];
decTotal_number2[a+1]=decWeight*decTotal_number1[a]+(1-decWeight)*decTotal_number2[a];
}
}
}

protected void Forecast2()
{
decResult=decTotal_number2[intArrayNumber];
}

protected void CalculateMSE2()
{
if(intArrayNumber<=intCount)
{
for(int a=0;a<intArrayNumber;a++)
{
decSingleTotal+=(decSingle[a]-decTotal_number2[a])*(decSingle[a]-decTotal_number2[a]);
}
decMSE=decSingleTotal/intArrayNumber;
decSingleTotal=0;
}
}
}
}


2007-02-01 16:03
zone0356224
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2007-2-1
得分:0 
谢谢版主,我已经会用重载了,虽然还不是很明白。
呵呵!!!
谢谢!!!

2007-02-02 16:40
zone0356224
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2007-2-1
得分:0 

我把两个类合并了,把函数重载了。
爽阿!!!
using System;

namespace ForecastSystem
{
/// <summary>
/// method_index 的摘要说明。
/// </summary>
public class method_index
{
//private variables to hold class properties
private string strTime;
private int intArrayNumber,i;
private decimal decQuantity,decWeight,decInitial1,decInitial2,decSingleTotal,decResult,decMSE;
private static int intCount1,intCount2;
private static decimal []decSingle=new decimal[100];
private static decimal []decTotal_number=new decimal[100];
private static decimal []decTotal_number1=new decimal[100];
private static decimal []decTotal_number2=new decimal[100];

public method_index(int intArrayNumber,decimal decWeight,string strTime,decimal decQuantity,decimal decInitial1)
{
//
this.Time=strTime;
this.Quantity=decQuantity;
this.Weight=decWeight;
this.Initial1=decInitial1;
this.ArrayNumber=intArrayNumber;
Add_number1();
Forecast1();
//
}

public method_index(int intArrayNumber,decimal decWeight,string strTime,decimal decQuantity,decimal decInitial1,decimal decInitial2)
{
//
this.Time=strTime;
this.Quantity=decQuantity;
this.Weight=decWeight;
this.Initial1=decInitial1;
this.Initial2=decInitial2;
this.ArrayNumber=intArrayNumber;
Add_number2();
Forecast2();
//
}

public method_index(int i)
{
this.i=i;
if(i==1)
{
newforecast1();
}
else if(i==2)
{
newforecast2();
}

}

public string Time
{
get
{
return strTime;
}
set
{
strTime=value;
}
}

public int ArrayNumber
{
get
{
return intArrayNumber;
}
set
{
intArrayNumber=value;
}
}

public decimal Initial1
{
get
{
return decInitial1;
}
set
{
decInitial1=value;
}
}

public decimal Initial2
{
get
{
return decInitial2;
}
set
{
decInitial2=value;
}
}

public decimal Quantity
{
get
{
return decQuantity;
}
set
{
decQuantity=value;
}
}

public decimal Weight
{
get
{
return decWeight;
}
set
{
decWeight=value;
}
}

public decimal SingleTotal
{
get
{
return decSingleTotal;
}
set
{
decSingleTotal=value;
}
}

public decimal Result
{
get
{
return decResult;
}
set
{
decResult=value;
}
}

public decimal MSE
{
get
{
return decMSE;
}
set
{
decMSE=value;
}
}

public static int Count1
{
get
{
return intCount1;
}
}

public static int Count2
{
get
{
return intCount2;
}
}

protected void Add_number1()
{
decTotal_number[0]=decInitial1;
decSingle[intCount1]=decQuantity;
intCount1++;

if(intArrayNumber<=intCount1)
{
for(int a=0;a<intArrayNumber;a++)
{
decTotal_number[a+1]=decWeight*decSingle[a]+(1-decWeight)*decTotal_number[a];
}
}
}

protected void Forecast1()
{
decResult=decTotal_number[intArrayNumber];

if(intArrayNumber<=intCount1)
{
for(int a=0;a<intArrayNumber;a++)
{
decSingleTotal+=(decSingle[a]-decTotal_number[a])*(decSingle[a]-decTotal_number[a]);
}
decMSE=decSingleTotal/intArrayNumber;
decSingleTotal=0;
}
}

protected void Add_number2()
{
decTotal_number1[0]=decInitial1;
decTotal_number2[0]=decInitial2;
decSingle[intCount2]=decQuantity;
intCount2++;

if(intArrayNumber<=intCount2)
{
for(int a=0;a<intArrayNumber;a++)
{
decTotal_number1[a+1]=decWeight*decSingle[a]+(1-decWeight)*decTotal_number1[a];
decTotal_number2[a+1]=decWeight*decTotal_number1[a+1]+(1-decWeight)*decTotal_number2[a];
decTotal_number[a]=(1+(1/(1-decWeight)))*decTotal_number1[a+1]-(1/(1-decWeight))*decTotal_number2[a+1];
}
}
}

protected void Forecast2()
{
decResult=decTotal_number[intArrayNumber-1];

if(intArrayNumber<=intCount2)
{
for(int a=1;a<intArrayNumber;a++)
{
decSingleTotal+=(decSingle[a]-decTotal_number[a-1])*(decSingle[a]-decTotal_number[a-1]);
}
decMSE=decSingleTotal/(intArrayNumber-1);
decSingleTotal=0;
}
}

protected void newforecast1()
{
intCount1=0;
}

protected void newforecast2()
{
intCount2=0;
}
}
}


2007-02-03 01:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-119159-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.098267 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved