关于开发过程中的想法

来源:互联网 发布:java生成svg 编辑:程序博客网 时间:2024/06/05 10:03

(1)对一个对象进行操作,首先应想到这个对象有什么样的属性,方法,字段,然后可以通过查看定义(可以网上找/.net用第三方工具来实现查看源码)

(2)面向对象的想法:求长方形面积。过程化编程:得到长,宽,使用面积计算公式得出面积。面向对象编程:长方形是个类,类有长,宽字段封装成属性作为外部接口,类有成员函数面积的计算;我们实例化一个类,输出类的属性值面积。

<pre name="code" class="csharp">namespace 过程化编程思想和面向对象编程思想{    class Program    {        static void Main(string[] args)        {            //过程化编程思想            int area1 = area(2,4);            Console.WriteLine(area1);            //面向对象编程思想            oblong oblong1 = new oblong(2,4);            Console.WriteLine(oblong1.Area);            Console.ReadKey();        }        static public int area(int x,int y)        {            if (x <= 0 || y <= 0)            {                return 0;            }            else            {                return (x * y);            }        }    }    class oblong    {        private int length;        private int width;        private int area;        //默认构造函数        public oblong() { }        //自定义构造函数        public oblong(int x,int y)        {            this.length = x;            this.width = y;            this.area = this.computeArea();        }        //非法值控制        public int Length        {            get            {                return this.length;            }            set{                if (value <= 0)                {                    return;                }                else                {                    this.length = value;                }        }        }        public int Width        {            get            {                return this.width;            }            set            {                if (value <= 0)                {                    return;                }                else                {                    this.width=value;                }            }        }        public int Area        {            get            {                return this.area;            }        }        //成员函数        private int computeArea()        {            return this.length * this.width;        }    }}



0 0