打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
重载 C#

关注我,为您分享C#技术实现

类成员

方法重载特点:

C# 方法是包含一系列语句的代码块

  1. 方法名必须相同
  2. 参数列表必须不相同
  3. 返回类型可以不相同

方法重载规则:

  1. 两方法不能仅在返回类型上有区分
  2. 两个方法不能仅根据参数是声明为ref还是out来区分

方法重载:

byte b1 = Convert.ToByte(true);

byte b2 = Convert.ToByte("123");

byte b3 = Convert.ToByte(3.14m);

代码示例:

public class Product

{

/// <summary>

/// 修改,无参数

/// </summary>

public void Modify()

{

}

/// <summary>

/// 修改,int类型参数

/// </summary>

/// <param name="i">参数i</param>

/// <returns></returns>

public int Modify(int i)

{

return i;

}

/// <summary>

/// 修改,string类型参数

/// </summary>

/// <param name="s">参数s</param>

/// <returns></returns>

public int Modify(string s)

{

return 1;

}

/// <summary>

/// 修改,两个参数

/// </summary>

/// <param name="s">参数s</param>

/// <param name="i">参数i</param>

/// <returns></returns>

public string Modify(string s,int i)

{

return "";

}

}

构造函数重载特点:

  1. 没有返回值。
  2. 方法名必须相同
  3. 参数列表必须不相同

运行结果:

private void button1_Click(object sender, EventArgs e)

{

try

{

int id = int.Parse(tb_id.Text);

string name = tb_name.Text;

string price = tb_price.Text;

Product p = new Product();

lbx_id.Text = p.Id.ToString();

lbx_name.Text = p.Name;

lbx_pric.Text = p.Price.ToString();

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

//Product product = new Product();

//product.Modify("");

}

代码示例:

try

{

int id = int.Parse(tb_id.Text);

string name = tb_name.Text;

string price = tb_price.Text;

Product p = new Product(id,name);

lbx_id.Text = p.Id.ToString();

lbx_name.Text = p.Name;

lbx_pric.Text = p.Price.ToString();

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

构造函数之间的调用:

运行结果:

调自己:this

/// <summary>

/// 实例化一个id和名字的Product对象

/// </summary>

/// <param name="_id">id</param>

/// <param name="_name">名称</param>

public Product(int _id,string _name)

:this(_id,_name,666)

{

id = _id;

name = _name;

}

调父类:(以后在讲)

代码示例:

public class Product

{

int id;

string name;

decimal price;

/// <summary>

/// 实例化一个空的Product对象

/// </summary>

public Product()

{

//空的构造函数

}

/// <summary>

/// 实例化一个有id的Product对象

/// </summary>

/// <param name="_id">id</param>

public Product(int _id)

{

id = _id;

}

/// <summary>

/// 实例化一个id和名字的Product对象

/// </summary>

/// <param name="_id">id</param>

/// <param name="_name">名称</param>

public Product(int _id,string _name)

:this(_id,_name,666)

{

id = _id;

name = _name;

}

/// <summary>

/// 实例化一个有id、名字、价格的Product对象

/// </summary>

/// <param name="_id">id</param>

/// <param name="_name">名称</param>

/// <param name="_price">价格</param>

public Product(int _id, string _name,decimal _price)

{

id = _id;

name = _name;

price = _price;

}

public int Id { get => id; set => id = value; }

public string Name { get => name; set => name = value; }

public decimal Price { get => price; set => price = value; }

#region 重载方法示例

/// <summary>

/// 修改,无参数

/// </summary>

public void Modify()

{

}

/// <summary>

/// 修改,int类型参数

/// </summary>

/// <param name="i">参数i</param>

/// <returns></returns>

public int Modify(int i)

{

return i;

}

/// <summary>

/// 修改,string类型参数

/// </summary>

/// <param name="s">参数s</param>

/// <returns></returns>

public int Modify(string s)

{

return 1;

}

/// <summary>

/// 修改,两个参数

/// </summary>

/// <param name="s">参数s</param>

/// <param name="i">参数i</param>

/// <returns></returns>

public string Modify(string s,int i)

{

return "";

}

#endregion

}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C# 应用 - 使用 HttpWebRequest 发起 Http 请求
C# 实现 复数 运算 类
Asp.Net 操作 Sqlserver通用类
dotnetcore 3.0+Dapper+MySQL
asp.net c# request常用
泛型的排序问题
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服