标签:style blog class code java tar
数据库中的数字和编程语言中的数字有显著的不同,数据库中的数字可以为空,C#中的数字不能为空。
int? x = null; //此时x可以为空
测试类(NullableTest)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ListMethod { /// <summary> /// 可空类型的测试 /// </summary> class NullableTest { public NullableTest() { } /// <summary> /// 输出测试结果 /// </summary> /// <param name="cur"></param> public void WriteTest(int? cur) { int? x = cur; int z = 6; if (x == null) { Console.WriteLine("X is NULL"); //空转非空 Console.WriteLine("-----空转非空-----"); int y = x ?? 0; //如果x为null 则y=0; ??合并运算符 或强制类型转换 int y = (int)x Console.WriteLine("x为null时y的值{0}:", y); //非空转空 Console.WriteLine("-----非空转空-----"); x = z; //直接复制 Console.WriteLine("x为null时z的值赋给x后x={0}:", x); } else if(x<0) { Console.WriteLine("X is smaller than 0"); } } } }
主方法(Main)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ListMethod { class Program { static void Main(string[] args) { NullableTest test = new NullableTest(); test.WriteTest(null); test.WriteTest(-10); } } }
输出结果
2.泛型方法调用和约束
账务处理接口(IAmount)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace List_Method { public interface IAmount { decimal Balance { get; } string Name { get; } } }
账务类(Amount) 实现接口IAmount
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace List_Method { class Amount:IAmount { public string Name { get; private set; } public decimal Balance { get; private set; } public Amount(string name, decimal balance) { this.Name = name; this.Balance = balance; } } }
账务类(Amount2) 实现接口IAmount
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace List_Method { class Amount1:IAmount { public string Name { get; private set; } public decimal Balance { get; private set; } public Amount1(string name, decimal balance) { this.Name = name; this.Balance = balance; } } }
计算账务合计类(Algorithm)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace List_Method { public static class Algorithm { /// <summary> /// (泛型约束)此方法的优势是TAccount可以为任何类 /// </summary> /// <typeparam name="TAccount"></typeparam> /// <param name="sourse"></param> /// <returns></returns> public static decimal AccumulateSimple<TAccount>(IEnumerable<TAccount> sourse) where TAccount : IAmount { decimal sum = 0; foreach (TAccount account in sourse) { sum += account.Balance; } return sum; } } }
主方法类
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace List_Method { class Program { static void Main(string[] args) { var account = new List<Amount>() { new Amount("zhang", 1500), new Amount("wang",1600), new Amount("li", 1700), new Amount("zhao", 1800), }; decimal amount = Algorithm.AccumulateSimple(account); //调用泛型的方法 Console.WriteLine("Amount合计为:{0}", amount); //账务类二 var account1 = new List<Amount1>() { new Amount1("zhang", 1500), new Amount1("wang",1600), new Amount1("li", 1700), new Amount1("zhao", 1800), new Amount1("other",2000), }; decimal amount1 = Algorithm.AccumulateSimple(account1); //调用泛型的方法 Console.WriteLine("Amount2合计为:{0}", amount1); } } }
输出结果:(本例泛型同时计算两个类的账务,可以多个)
标签:style blog class code java tar
原文地址:http://www.cnblogs.com/zxd543/p/3716387.html