2.C#面向对象基础属性

属性:

1.一般属性开头字母大写,字段开头字母小写。

2.通过public来赋值的方法无法判断赋值是否非法!

3.无论赋值如何,取值如果就是不采用赋值的结果,那么无论赋值什么都不管用。

4.经典错误之死循环。

5.只读属性。

6..net3.0以上支持简写代码

例一:

通过public来赋值的方法无法判断赋值是否非法!

验证代码如下:

using System;
using System.Collections.Generic;
using System.Text;
namespace stduy2 { class Program { static void Main(string[] args) { Person p = new Person(); p.Age = 22;//赋值 Console.WriteLine("星云的年龄:Age={0}",p.Age);//取值结果22 p.Age = -100; Console.WriteLine("所以星云修改后的年龄:Age={0}\n", p.Age);//取值结果22 p.Age1 = -100;//这种赋值方法,无法判断合法值 Console.WriteLine("通过public来赋值的方法无法判断赋值是否非法!\n所以星云新的年龄为错误值:Age1={0},", p.Age1);//取值结果-100 Console.ReadKey(); } } class Person { private int age; public int Age1; public int Age { set //赋值 { if (value< 0) { Console.WriteLine("\n警告:年龄修改失败,年龄将保持不变,失败原因:年龄不能为负数!"); } else this.age = value; } get //取值 { return this.age; } } } }

运行截图:

例二:

无论赋值如何,取值如果就是不采用赋值的结果,那么无论赋值什么都不管用。

代码验证如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace stduy2
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person();

            p.Age = 22;//赋值
            Console.WriteLine("星云的年龄:Age={0}",p.Age);//取值返回值520
            p.Age = -100;
            Console.WriteLine("所以星云修改后的年龄:Age={0}\n", p.Age);//取值,返回值为520
            p.Age = 22;
            p.Age = p.Age + 1;
            Console.WriteLine("\n计算后星云年龄为p.Age={0}",p.Age);//取值,所以返回值是520
            Console.ReadKey();
        }
    }
    class Person 
    {
        private int age;
        public int Age1;
        public int Age 
        {
            set //赋值
            {
                if (value< 0)
                {
                    Console.WriteLine("\n警告:年龄修改失败,年龄将保持不变,失败原因:年龄不能为负数!");
                }
                else
                    this.age = value;
            }
            get //取值
            {
                return 520;               
            }
        }
    }
}

程序运行截图:

例三:经典错误之死循环:

代码如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace stduy2
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person();

            p.Age = 22;//赋值
            Console.WriteLine("星云的年龄:Age={0}",p.Age);//取值死循环
            Console.ReadKey();
        }
    }
    class Person 
    {
        private int age;
        public int Age 
        {
            set //赋值
            {
                    this.Age = value;
            }
            get //取值
            {
                return this.Age;
                
            }
        }
    }
}

 例四:只读属性:

using System;
using System.Collections.Generic;
using System.Text;

namespace Person
{
    class Program
    {
        static void Main(string[] args)
        {
            person p = new person();
            p.IncAge();
            Console.WriteLine("年龄:{0}", p.Age);
            p.IncAge();
            Console.WriteLine("年龄:{0}",p.Age);
            Console.ReadKey();
        }
    }

    class person 
    {
        private int age;
        public int Age  //只读属性
        {
            get { return age; }

        }
        public void IncAge()
        {
            age++;
        }
    }
}

运行截图:

例五:.net3.0以上支持简写代码:

using System;
using System.Collections.Generic;
using System.Text;

namespace Person
{
    class Program
    {
        static void Main(string[] args)
        {
            person p = new person();
            p.Age = 22;
            Console.WriteLine("星云年龄:{0}", p.Age);
            Console.ReadKey();
        }
    }

    class person 
    {

        public int Age  
        {
            set;//编译器自动帮我们生成私有字段和set、get代码块。
            get;

        }
        public String Name
        {
            set;
            get;
        }
    }
}

运行截图:

posted @ 2014-08-01 20:09  技术宅星云  阅读(430)  评论(0编辑  收藏  举报