# 面向对象编程(一)

   1:   static void Main(string[] args)
   2:   {
   3:        Console.WriteLine("长为:{0},宽为:{1}的长方形面积为:{2}", 6, 3, 6 * 3);
   4:   }

1.这个程序只能计算长为:6,宽为3的的长方形面积.(如果只是这样,那么一点问题都没有,但是我们以后也许会在很多时候需要求各种长方形的面积);

   1:   static void Main(string[] args)
   2:   {
   3:         int a = 10;
   4:         int b = 4;
   5:         Console.WriteLine("长为:{0},宽为:{1}的长方形面积为:{2}", a,b,a*b);
   6:   }

   1:  static void Main(string[] args)
   2:   {
   3:         int a = 10;
   4:         int b = 4;
   5:         Console.WriteLine("长为:{0},宽为:{1}的长方形面积为:{2}", a,b,a*b);
   6:
   7:         a = 20;
   8:         b = 6;
   9:         Console.WriteLine("长为:{0},宽为:{1}的长方形面积为:{2}", a, b, a * b);
  10:
  11:         //......
  12:         //......
  13:  }

   1:          static void Main(string[] args)
   2:          {
   3:              int a = 10;
   4:              int b = 4;
   5:              CalculatingArea(a, b);
   6:
   7:              a = 20;
   8:              b = 6;
   9:              CalculatingArea(a, b);
  10:
  11:              //......
  12:              //......
  13:          }
  14:
  15:          private static void CalculatingArea(int a, int b)
  16:          {
  17:              Console.WriteLine("长为:{0},宽为:{1}的长方形面积为:{2}", a, b, a * b);
  18:          }

   1:      public class Test
   2:      {
   3:          public void TestCalculate()
   4:          {
   5:              int a = 10;
   6:              int b = 5;
   7:              this.CalculatingArea(a, b);
   8:          }
   9:
  10:          private void CalculatingArea(int a, int b)
  11:          {
  12:              Console.WriteLine("长为:{0},宽为:{1}的长方形面积为:{2}", a, b, a * b);
  13:          }
  14:      }

   1:      public class Calculate
   2:      {
   3:          public void CalculatingArea(int a, int b)
   4:          {
   5:              Console.WriteLine("长为:{0},宽为:{1}的长方形面积为:{2}", a, b, a * b);
   6:          }
   7:      }
   8:      public class Test
   9:      {
  10:          public void TestCalculate()
  11:          {
  12:              Calculate calculate = new Calculate();
  13:              int a = 10;
  14:              int b = 5;
  15:              calculate.CalculatingArea(a, b);
  16:          }
  17:      }
  18:      class Program
  19:      {
  20:          static void Main(string[] args)
  21:          {
  22:              Calculate calculate = new Calculate();
  23:
  24:              int a = 10;
  25:              int b = 4;
  26:              calculate.CalculatingArea(a, b);
  27:
  28:              a = 20;
  29:              b = 6;
  30:              calculate.CalculatingArea(a, b);
  31:
  32:              //......
  33:              //......
  34:          }
  35:      }

   1:      public class CalculateTriangle
   2:      {
   3:          public void CalculatingArea(int a, int b)
   4:          {
   5:              Console.WriteLine("底为:{0},高为:{1}的三角形面积为:{2}", a, b, a * b*0.5);
   6:          }
   7:      }

   1:          static void Main(string[] args)
   2:          {
   3:              int switchKey = 0;
   4:              switchKey=int.Parse(Console.ReadLine());
   5:              int a = 0;
   6:              int b = 0;
   7:              a=int.Parse(Console.ReadLine());
   8:              b = int.Parse(Console.ReadLine());
   9:              if (switchKey == 1)
  10:              {
  11:                  CalculateRectangle calculate = new CalculateRectangle();
  12:                  calculate.CalculatingArea(a, b);
  13:              }
  14:              if (switchKey == 2)
  15:              {
  16:                  CalculateTriangle calculate = new CalculateTriangle();
  17:                  calculate.CalculatingArea(a, b);
  18:              }
  19:          }

   1:      public class CalculateCircle
   2:      {
   3:          public void CalculatingArea()
   4:          {
   5:              Double r = 0;
   6:              r = Double.Parse(Console.ReadLine());
   7:              Console.WriteLine("半径为:{0}的圆形面积为:{1}", r, 3.14*r*r);
   8:          }
   9:      }
  10:      public class CalculateRectangle
  11:      {
  12:          public void CalculatingArea()
  13:          {
  14:              int a = 0;
  15:              int b = 0;
  16:              a = int.Parse(Console.ReadLine());
  17:              b = int.Parse(Console.ReadLine());
  18:              Console.WriteLine("长为:{0},宽为:{1}的长方形面积为:{2}", a, b, a * b);
  19:          }
  20:      }
  21:      public class CalculateTriangle
  22:      {
  23:          public void CalculatingArea()
  24:          {
  25:              int a = 0;
  26:              int b = 0;
  27:              a = int.Parse(Console.ReadLine());
  28:              b = int.Parse(Console.ReadLine());
  29:              Console.WriteLine("底为:{0},高为:{1}的三角形面积为:{2}", a, b, a * b*0.5);
  30:          }
  31:      }
  32:      class Program
  33:      {
  34:          static void Main(string[] args)
  35:          {
  36:              int switchKey = 0;
  37:              switchKey=int.Parse(Console.ReadLine());
  38:              if (switchKey == 1)
  39:              {
  40:                  CalculateRectangle calculate = new CalculateRectangle();
  41:                  calculate.CalculatingArea();
  42:              }
  43:              if (switchKey == 2)
  44:              {
  45:                  CalculateTriangle calculate = new CalculateTriangle();
  46:                  calculate.CalculatingArea();
  47:              }
  48:              if (switchKey == 3)
  49:              {
  50:                  CalculateCircle calculate = new CalculateCircle();
  51:                  calculate.CalculatingArea();
  52:              }
  53:          }
  54:      }

“CalculatingArea()”方法呢? 这样我们就用到了接口,代码为:

   1:      public interface ICalculate
   2:      {
   3:          void CalculatingArea();
   4:      }
   5:      public class CalculateCircle : ICalculate
   6:      {
   7:          public void CalculatingArea()
   8:          {
   9:              Double r = 0;
  10:              r = Double.Parse(Console.ReadLine());
  11:              Console.WriteLine("半径为:{0}的圆形面积为:{1}", r, 3.14*r*r);
  12:          }
  13:      }
  14:      public class CalculateRectangle : ICalculate
  15:      {
  16:          public void CalculatingArea()
  17:          {
  18:              int a = 0;
  19:              int b = 0;
  20:              a = int.Parse(Console.ReadLine());
  21:              b = int.Parse(Console.ReadLine());
  22:              Console.WriteLine("长为:{0},宽为:{1}的长方形面积为:{2}", a, b, a * b);
  23:          }
  24:      }
  25:      public class CalculateTriangle : ICalculate
  26:      {
  27:          public void CalculatingArea()
  28:          {
  29:              int a = 0;
  30:              int b = 0;
  31:              a = int.Parse(Console.ReadLine());
  32:              b = int.Parse(Console.ReadLine());
  33:              Console.WriteLine("底为:{0},高为:{1}的三角形面积为:{2}", a, b, a * b*0.5);
  34:          }
  35:      }
  36:      class Program
  37:      {
  38:          static void Main(string[] args)
  39:          {
  40:              int switchKey = 0;
  41:              switchKey=int.Parse(Console.ReadLine());
  42:              ICalculate calculate=null;
  43:              if (switchKey == 1)
  44:              {
  45:                   calculate = new CalculateRectangle();
  46:              }
  47:              if (switchKey == 2)
  48:              {
  49:                   calculate = new CalculateTriangle();
  50:              }
  51:              if (switchKey == 3)
  52:              {
  53:                   calculate = new CalculateCircle();
  54:              }
  55:              calculate.CalculatingArea();
  56:          }
  57:      }

   1:      public class CalculateFactory
   2:      {
   3:          public static ICalculate GetCalculate()
   4:          {
   5:              int switchKey = 0;
   6:              switchKey = int.Parse(Console.ReadLine());
   7:              ICalculate calculate = null;
   8:              if (switchKey == 1)
   9:              {
  10:                  calculate = new CalculateRectangle();
  11:              }
  12:              if (switchKey == 2)
  13:              {
  14:                  calculate = new CalculateTriangle();
  15:              }
  16:              if (switchKey == 3)
  17:              {
  18:                  calculate = new CalculateCircle();
  19:              }
  20:              return calculate;
  21:          }
  22:      }
  23:      class Program
  24:      {
  25:          static void Main(string[] args)
  26:          {
  27:              ICalculate calculate = CalculateFactory.GetCalculate();
  28:              calculate.CalculatingArea();
  29:          }
  30:      }

posted @ 2011-11-04 12:20  风枫峰  阅读(1942)  评论(6编辑  收藏  举报