這是我的物件
public class Totals {
public int Total1 { get; set; }
public int Total2 { get; set; }
public int Total3 { get; set; }
public int Total4 { get; set; }
}
使用 calculateTotals 方法增加 Total1 和 Total2 的值
private Totals calculateTotals(Totals t) {
if (//condition) {
t.Total1 = 1;
} else {
t.Total2 = 1;
}
return t;
}
**使用不同的方法calculateOtherTotals在不同位置相同條件下相同物件的Total3和Total4的遞增值,此時我只需要更新Total3和Total4 **
private Totals calculateOtherTotals(Totals t) {
if (//condition) {
t.Total3 = 1;
} else {
t.Total4 = 1;
}
return t;
}
我是 c# 新手,我需要分別增加值 Total1,Total2 和 Total3,Total4 并且我擁有的代碼作業正常
有沒有辦法改進我的代碼?,我怎樣才能避免創建兩種不同的方法,它們在不同的屬性上幾乎執行相同的邏輯?有沒有辦法只創建一種方法來實作我的功能?
uj5u.com熱心網友回復:
您可以這樣做,但基本上代碼量不會改變。
這增加了一個判斷:
Totals calculateTotals(Totals t, bool Flag)
{
//function1:
if (Flag)
{
if (true)
{ //(condition) {
t.Total1 ;
}
else
{
t.Total2 ;
}
}
//function2:
else
{
if (true)
{ //(condition) {
t.Total3 ;
}
else
{
t.Total4 ;
}
}
return t;
}
像這樣稱呼它:
Totals totals = new Totals();
totals.Total1=0;
totals.Total2=0;
totals.Total3=0;
totals.Total4=0;
calculateTotals(totals,true);//function1:
calculateTotals(totals,false);//function2:
uj5u.com熱心網友回復:
反射是一種方式,雖然它很慢并且不是特定領域的語言:
Type totalsType = typeof(Totals);
var totalToIncrement = condition;
PropertyInfo prop = totalsType.GetProperty("Total" totalToIncrement);
prop.SetValue(null, 76);
或者,也許您想抽象出要增加的屬性:
private Totals calculateTotals(Totals t)
{
bool condition = true;
AbstractAdds(ref t.Total1, ref t.Total2, condition);
return t;
}
private void AbstractAdds(ref int a, ref int b, bool condition = false)
{
if (condition)
{
a ;
}
else
{
b ;
}
}
}
public class Totals
{
public int Total1;//{ get; set; }
public int Total2;//{ get; set; }
public int Total3;//{ get; set; }
public int Total4;//{ get; set; }
}
我個人有一個List<int>orint[3]并使條件計算索引 0-3:
var index = calcCondition;
Totals[index] ;
通過這種方式,它可以擴展為更多總數,并且您可以獲得像 LINQ 這樣的內置函式,例如Totals.Sum().
uj5u.com熱心網友回復:
有沒有辦法改進我的代碼?,我怎樣才能避免創建兩種不同的方法,它們在不同的屬性上幾乎執行相同的邏輯?有沒有辦法只創建一種方法來實作我的功能?
然后,這取決于您希望您的方法(功能)如何。(例如,你如何定義你的函式將做什么以及你的類和屬性如何具有特征——目前,許多想要幫助你的人仍然想知道。)
讓我再舉一個明顯的例子。
假設您回答您的附加要求是:
- 我的物件只有 4 個“總計”屬性
- 我希望這些新函式在呼叫時只增加 1,不需要添加超過 1
- 從另一個類呼叫此函式以修改我的物件值
- 我希望我的酷函式名稱
calculateOtherTotals是私有的,因為一些無法解釋的原因,例如“我不喜歡其他人知道它的存在”。
然后
public OtherClass{
Public Totals ExposeThePrivateCalculateOtherTotals(Totals t, bool IncrementT1 , bool IncrementT2 , bool IncrementT3, bool IncrementT4)
{
calculateOtherTotals(t, IncrementT1 , IncrementT2 , IncrementT3, IncrementT4);
}
Private Totals calculateOtherTotals(Totals t, bool IncrementT1 , bool IncrementT2 , bool IncrementT3, bool IncrementT4) {
if( IncrementT1 ) t.Total1 = 1; //choose your style
if( IncrementT2==true ) t.Total2;//choose your style
if( IncrementT3!=false ) t.Total3 ; //choose your style
t.Total4 = IncrementT4==true?1:0;//choose your style
return t;
}
}
//In main (how to use)
Totals t= new Totals();
OtherClass doMyFunc = new OtherClass();
t = doMyFunc.ExposeThePrivateCalculateOtherTotals(t, true, false,false,false); // result of operation => t.total1 = 1;
t = doMyFunc.ExposeThePrivateCalculateOtherTotals(t, false, true,false,false); // result of operation => t.total2 = 1;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/491888.html
下一篇:我如何修改網址(Flask)
