namespace the Test { / * fat exemplary interfaces * / public class Program { static void the Main ( String [] args) { int [] = {nums1 . 1 , 2 , . 3 , . 4 , . 5 }; ArrayList nums2 = new ArrayList { 1, 2, 3, 4, 5 }; var nums3 = new ReadOnlyCollection(nums1); Console.WriteLine(Sum(nums1)); Console.WriteLine(Sum(nums2)); Console.WriteLine(Sum(nums3)); Console.Read(); } // This interface is too fat, more demand, we actually need is foreach iteration function, so IEnumerable enough // static int Sum (ICollection nums) static int Sum (IEnumerable nums) { int SUM = 0 ; foreach ( var the n- in nums) sum += (int)n; return sum; } } public class ReadOnlyCollection : IEnumerable { private int[] _array; public ReadOnlyCollection(int[] array) { this._array = array; } public IEnumerator GetEnumerator() { return new Enumerator(this); } public class Enumerator : IEnumerator { private ReadOnlyCollection _collection; private int _head; public Enumerator(ReadOnlyCollection collection) { this._collection = collection; _head = -1; } public object Current { get { object o = _collection._array[_head]; return o; } } public bool MoveNext() { if(++_head < _collection._array.Length) { return true; } else { return false; } } public void Reset() { _head = -1; } } } }
Notes _ _ fat Interface Segregation Principle Interface
Guess you like
Origin www.cnblogs.com/hansel520/p/12141649.html
Recommended
Ranking