标题:[求助]问一个关于C#重载的问题!
只看楼主
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
jacklee
Rank: 7Rank: 7Rank: 7
来 自:XAplus
等 级:贵宾
威 望:32
帖 子:1769
专家分:104
注 册:2006-11-3
得分:0 
不好意思,昨天下班早了点儿,等会,我写段代码实验一下。还没那么用过

XAplus!
讨论群:51090447
删吧删吧,把我的号给删了!
2007-02-02 07:58
liuminghui
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:2882
专家分:0
注 册:2007-1-26
得分:0 
关注!

海鸽 is My Lover!!
2007-02-02 09:37
jacklee
Rank: 7Rank: 7Rank: 7
来 自:XAplus
等 级:贵宾
威 望:32
帖 子:1769
专家分:104
注 册:2006-11-3
得分:0 
实验了一把要将两个类里的函数名一样的合在一起。继承就行了啊,那个函数必须是PUBLIC OR PROTECTED

XAplus!
讨论群:51090447
删吧删吧,把我的号给删了!
2007-02-02 09:51
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
jacklee
Rank: 7Rank: 7Rank: 7
来 自:XAplus
等 级:贵宾
威 望:32
帖 子:1769
专家分:104
注 册:2006-11-3
得分:0 
看形式难道是手工复制了函数?

XAplus!
讨论群:51090447
删吧删吧,把我的号给删了!
2007-02-03 08:00
zone0356224
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2007-2-1
得分:0 

哈哈,对的
我让函数名一样,参数不同,但是里面调用的子函数还是原先的那些函数.
这样也算重载了.有不同的参数就掉用不同的函数.


2007-02-03 10:09



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




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

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