using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Forms;

namespace @string
{
    class Program//这段是用属性的
    {
        static void Main(string[] args)
        {
            propertyTest pro = new propertyTest(20,"匿名");
            Console.WriteLine(pro.Age);
            Console.WriteLine(pro.Name);
            pro.Age = 23;
            pro.Name = "Jerryzhang";
            Console.WriteLine(pro.Age);
            Console.WriteLine(pro.Name);
            pro.Age = 666;
            pro.Name = "Jerryzhang";
          
            Console.WriteLine(pro.Name);
            Console.WriteLine(pro.Age);
        }
    }
    class propertyTest
    {
        private int _age;
        private string _name;
        public propertyTest(int age, string name)
        {
            this._age = age;//age字段
            this._name = name;//name字段
        }


        public int Age//age的属性
        {
            get { return _age; }
            set
            {

                if (value >= 100 || value <= 0)
                {
                  MessageBox.Show("您的年龄不合常理");//这里需要抛出异常,我就简单点用个messagebox显示
                }
                else
                {
                    _age = value;//这个value是系统默认的一个参数
                }


            }
        }
        public string Name//name的属性
        {
            get { return _name; }
            set { _name = value; }
        }
    }
}


res4

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Forms;

namespace @string
{
    class Program//这段是没带属性的
    {
        static void Main(string[] args)
        {
            propertyTest pro = new propertyTest(20, "匿名");
            Console.WriteLine(pro.GetAge());
            Console.WriteLine(pro.GetName());
            int inAge = int.Parse(Console.ReadLine());
            string inName = Console.ReadLine();
            pro.SetName(inName);
            pro.SetAge(inAge);
            Console.WriteLine(pro.GetAge());
            Console.WriteLine(pro.GetName());
        }
    }
    class propertyTest
    {
        private int _age;
        private string _name;
        public propertyTest(int age, string name)
        {
            this._age = age;
            this._name = name;
        }
        public void SetAge(int value)
        {
            if (value >= 100 || value <= 0)
            {
                MessageBox.Show("您的年龄填写不是很正确");
            }
            else
            {
                this._age = value;
            }
        }
        public int GetAge()
        {
            return _age;
        }
        public void SetName(string value)
        {
            this._name = value;
        }
        public string GetName()
        {
            return this._name;
        }


res5

         从这两个例子可以看出来,虽然做的不是很好但是可以看出,其实属性和方法在一定意义上可以转换,就是说在一定程度上可以把属性看做方法。

但是属性有不等同于方法,有些人说属性比较安全,因为它对字段进行了封装,但我觉得用方法也没什么不安全的,所以我觉得方法和属性只是在不同的

区域所进行的不同的表示而已,小人也只是乱说,望了解的朋友多多指点!

posted on 2009-04-21 20:24  Jerryzhang  阅读(5860)  评论(0编辑  收藏  举报