C#紧耦合的例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClosedCouple
{
    class Program
    {
        static void Main(string[] args)
        {
            Engine engine = new Engine();
            
            Car  myCar= new Car(engine);
        
            myCar.Run();
            Console.ReadLine();
        }
    }
    class Engine
    {
        public int rpm=0;
        public int gas { get; set; }
        public void Acculate()
        {
            this.gas-=1;
           this.rpm += 1000;
        }
    }
    class Car
    {
        private Engine engine;
        private int Speed;

        public Car(Engine engine)
        {
            this.engine = engine;
            this.Speed = engine.rpm / 10;
            
        }
        
       
        public void Run()
        {
            engine.Acculate();
            this.Speed = engine.rpm / 10;
            Console.WriteLine("My speed is{0}",this.Speed);
        }

    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Review
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a1 = new int[] { 1, 2, 3, 4, 5 };
            ArrayList a2 = new ArrayList() {1,2,3,4,5 };

            Console.WriteLine( Add(a1) );
            Console.WriteLine( Average(a1));
            Console.WriteLine(Add(a2));
            Console.WriteLine(Average(a2)); 
           Console.ReadLine();
        }

        static int Add(IEnumerable  a)
        {
            int res = 0;
            foreach (var item in a)
            {
                res +=(int)item;
            }
            return res;
        }
        static double Average(IEnumerable a)
        {
            int res = 0, count = 0;
            foreach (var item in a)
            {
                res +=(int) item;
                count++;
            }
            return res / count;
        }
    }
}
原文地址:https://www.cnblogs.com/1521681359qqcom/p/11421811.html