新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
重寫ToString與實(shí)現(xiàn)擴(kuò)展方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Money m = new Money(); m.Amount = 30.00m; Console.WriteLine(m.ToString());//輸出$:30.00 //使用擴(kuò)展方法 m.AddToMoney(1.00m); Console.WriteLine(m.ToString());//輸出$:31.00 Console.ReadKey(); } } public class Money { private decimal amount; public decimal Amount { set { amount = value; } get { return amount; } } public override string ToString()//重寫ToString方法 { return "$:" + amount.ToString(); } } //擴(kuò)展方法 public static class MoneyExtension { //this Money m 擴(kuò)展方法的規(guī)則,告訴編譯器這個(gè)方法是Money類型的一部分 public static void AddToMoney(this Money m, decimal d) { m.Amount += d; } } }
分享題目:重寫ToString與實(shí)現(xiàn)擴(kuò)展方法
文章地址:http://fisionsoft.com.cn/article/phoopj.html