詳解C#如何使用重載方法實現(xiàn)不同類型數(shù)據(jù)的計算
一、涉及到的相關(guān)知識
1.重載的方法
重載方法就是方法名稱相同,但是每個方法中參數(shù)的數(shù)據(jù)類型、個數(shù)或順序不同的方法。如果一個類中存在兩個以上的同名方法,并且方法的參數(shù)類型、個數(shù)或者順序不同,當(dāng)調(diào)用這樣的方法時,編譯器會根據(jù)傳入的參數(shù)自動進(jìn)行判斷,決定調(diào)用哪個方法。
2.Convert.ToInt32(String)方法
將數(shù)字的指定字符串表示形式轉(zhuǎn)換為等效的 32 位帶符號整數(shù)。
public static int ToInt32 (string? value);
參數(shù)
value String 包含要轉(zhuǎn)換的數(shù)字的字符串。
返回
Int32 一個與 value 中數(shù)字等效的 32 位帶符號整數(shù),如果 value 為 null,則為 0(零)。
例外
FormatException
value 不由一個可選符號后跟一系列數(shù)字 (0-9) 組成。
OverflowException
value 表示小于 Int32.MinValue 或大于 Int32.MaxValue 的數(shù)字。
在C#中,Convert.ToInt32(string)方法用于將字符串轉(zhuǎn)換為整數(shù)。如果字符串包含非數(shù)字字符,例如小數(shù)點,該方法將引發(fā)異常。例如,字符串是"123.456",包含非數(shù)字字符"."。因此,直接使用Convert.ToInt32(string)會引發(fā)異常。
為了避免異常,可以先使用Decimal.Parse(string)方法將字符串轉(zhuǎn)換為小數(shù),然后再使用Convert.ToInt32(decimal)方法將小數(shù)轉(zhuǎn)換為整數(shù)。
string str = "123.456"; decimal decimalValue = Decimal.Parse(str); int intValue = Convert.ToInt32(decimalValue);
或者,使用string.Split()方法將字符串按指定的分隔符拆分為一個字符串?dāng)?shù)組。例如,可以使用小數(shù)點"."作為分隔符,然后取第一個元素作為整數(shù)部分。
3.判斷字符串是否帶有小數(shù)點
string str = "123.456";
string[] parts = str.Split('.');
// 如果有小數(shù)點,取小數(shù)點前面的部分作為整數(shù)
// 如果沒有小數(shù)點,整個字符串就是整數(shù)部分
string integerPart = parts.Length > 0 ? parts[0] : str;
int intValue = Convert.ToInt32(integerPart);使用正則表達(dá)式@"^\d+\.\d+$"判斷字符串是否含有“.”,然后執(zhí)行相應(yīng)操作。
二、實例
1.示例
//重載加法運算
using System.Text.RegularExpressions;
namespace _111
{
public partial class Form1 : Form
{
private GroupBox? groupBox1;
private GroupBox? groupBox2;
private RadioButton? radioButton3;
private RadioButton? radioButton2;
private RadioButton? radioButton1;
private TextBox? textBox1;
private Label? label2;
private Label? label1;
private TextBox? textBox2;
private TextBox? textBox3;
private Button? button1;
private Label? label3;
public Form1()
{
InitializeComponent();
StartPosition = FormStartPosition.CenterScreen;
Load += Form1_Load;
}
private void Form1_Load(object? sender, EventArgs e)
{
//
// radioButton1
//
radioButton1 = new RadioButton
{
AutoSize = true,
Location = new Point(11, 17),
Name = "radioButton1",
Size = new Size(40, 21),
TabIndex = 0,
TabStop = true,
Text = "int",
UseVisualStyleBackColor = true
};
//
// radioButton2
//
radioButton2 = new RadioButton
{
AutoSize = true,
Location = new Point(11, 39),
Name = "radioButton2",
Size = new Size(90, 21),
TabIndex = 1,
TabStop = true,
Text = "int+double",
UseVisualStyleBackColor = true
};
//
// radioButton3
//
radioButton3 = new RadioButton
{
AutoSize = true,
Location = new Point(11, 61),
Name = "radioButton3",
Size = new Size(67, 21),
TabIndex = 2,
TabStop = true,
Text = "double",
UseVisualStyleBackColor = true
};
//
// label1
//
label1 = new Label
{
AutoSize = true,
Location = new Point(6, 23),
Name = "label1",
Size = new Size(44, 17),
TabIndex = 0,
Text = "加數(shù):"
};
//
// label2
//
label2 = new Label
{
AutoSize = true,
Location = new Point(6, 53),
Name = "label2",
Size = new Size(56, 17),
TabIndex = 1,
Text = "被加數(shù):"
};
//
// textBox1
//
textBox1 = new TextBox
{
Location = new Point(56, 17),
Name = "textBox1",
Size = new Size(91, 23),
TabIndex = 2
};
//
// textBox2
//
textBox2 = new TextBox
{
Location = new Point(56, 47),
Name = "textBox2",
Size = new Size(91, 23),
TabIndex = 3
};
//
// groupBox1
//
groupBox1 = new GroupBox
{
Location = new Point(12, 12),
Name = "groupBox1",
Size = new Size(153, 92),
TabIndex = 0,
TabStop = false,
Text = "數(shù)據(jù)運算"
};
groupBox1.Controls.Add(textBox2);
groupBox1.Controls.Add(textBox1);
groupBox1.Controls.Add(label2);
groupBox1.Controls.Add(label1);
groupBox1.SuspendLayout();
//
// groupBox2
//
groupBox2 = new GroupBox
{
Location = new Point(171, 12),
Name = "groupBox2",
Size = new Size(127, 92),
TabIndex = 0,
TabStop = false,
Text = "選擇數(shù)據(jù)類型"
};
groupBox2.Controls.Add(radioButton3);
groupBox2.Controls.Add(radioButton2);
groupBox2.Controls.Add(radioButton1);
groupBox2.SuspendLayout();
//
// textBox3
//
textBox3 = new TextBox
{
Location = new Point(88, 107),
Name = "textBox3",
Size = new Size(100, 23),
TabIndex = 1
};
//
// button1
//
button1 = new Button
{
Location = new Point(223, 107),
Name = "button1",
Size = new Size(75, 23),
TabIndex = 2,
Text = "開始計算",
UseVisualStyleBackColor = true
};
button1.Click += Button1_Click;
//
// label3
//
label3 = new Label
{
AutoSize = true,
Location = new Point(12, 113),
Name = "label3",
Size = new Size(68, 17),
TabIndex = 3,
Text = "運算結(jié)果:"
};
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 17F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(309, 136);
Controls.Add(label3);
Controls.Add(button1);
Controls.Add(textBox3);
Controls.Add(groupBox2);
Controls.Add(groupBox1);
Name = "Form1";
Text = "重載加法運算";
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
groupBox2.ResumeLayout(false);
groupBox2.PerformLayout();
}
private void Button1_Click(object? sender, EventArgs e)
{
textBox3!.Text = "";
try
{
if (radioButton1!.Checked)
{
if (!IsDecimalNumber(textBox1!.Text) && !IsDecimalNumber(textBox2!.Text))
{
textBox3!.Text = Add(Convert.ToInt32(textBox1!.Text), Convert.ToInt32(textBox2!.Text)).ToString();
}
else
{
MessageBox.Show("文本內(nèi)數(shù)字不能是小數(shù)","警示");
}
}
else if (radioButton2!.Checked)
{
if (!IsDecimalNumber(textBox1!.Text))
{
textBox3!.Text = Add(Convert.ToInt32(textBox1!.Text), Convert.ToDouble(textBox2!.Text)).ToString();
}
else
{
MessageBox.Show("加數(shù)不能是小數(shù)", "警示");
}
}
else if (radioButton3!.Checked)
{
textBox3!.Text = Add(Convert.ToDouble(textBox1!.Text) ,Convert.ToDouble(textBox2!.Text)).ToString();
}
}
catch { }
}
public static int Add(int x, int y)//定義一個靜態(tài)方法Add,返回值為int類型,有兩個int類型的參數(shù)
{
return x + y;
}
public static double Add(int x, double y)//重新定義方法Add,它與第一個方法的返回值類型及參數(shù)類型不同
{
return x + y;
}
public static double Add(double x, double y)//重新定義方法Add,它與第一個方法的返回值類型及參數(shù)類型不同
{
return x + y;
}
/// <summary>
/// 使用正則表達(dá)式判斷字符串是否為帶小數(shù)的數(shù)字
/// ^\d+\.\d+$ : ^ 表示字符串開始, \d+ 表示一個或多個數(shù)字,
/// \.? 表示可能存在的小數(shù)點, \d+ 表示小數(shù)點后面的一個或多個數(shù)字,
/// $ 表示字符串結(jié)束
/// </summary>
public static bool IsDecimalNumber(string str)
{
return MyRegex().IsMatch(str);
}
[GeneratedRegex(@"^\d+\.\d+$")]
private static partial Regex MyRegex();
}
}2.生成成果









到此這篇關(guān)于詳解C#如何使用重載方法實現(xiàn)不同類型數(shù)據(jù)的計算的文章就介紹到這了,更多相關(guān)C#重載方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實現(xiàn)子窗體與父窗體通信方法實例總結(jié)
這篇文章主要介紹了C#實現(xiàn)子窗體與父窗體通信方法,實例總結(jié)了常用的四種窗體通信方法,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09
C# async await 異步編程實現(xiàn)機(jī)制詳解
async/await是C# 5.0 引入的語法糖,它基于**狀態(tài)機(jī)(State Machine)**模式實現(xiàn),將異步方法轉(zhuǎn)換為編譯器生成的狀態(tài)機(jī)類,本文給大家介紹C# async await 異步編程實現(xiàn)機(jī)制,感興趣的朋友一起看看吧2025-08-08
C#使用Spire.XLS讀取Excel數(shù)據(jù)的代碼示例
在現(xiàn)代企業(yè)應(yīng)用中,Excel文件扮演著至關(guān)重要的角色,無論是數(shù)據(jù)導(dǎo)入、報表生成、還是數(shù)據(jù)分析,都離不開對Excel數(shù)據(jù)的處理,對于C#開發(fā)者而言,如何高效、穩(wěn)定地在應(yīng)用程序中C#讀取Excel內(nèi)容,常常是一個需要面對的挑戰(zhàn),本文介紹了C#如何使用Spire.XLS讀取Excel數(shù)據(jù)2025-09-09

