码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-01 22:51:32      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   2014   cti   ar   

属性:

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

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

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

4.经典错误之死循环。

例一:

通过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; } } } }

运行截图:

bubuko.com,布布扣

例二:

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

代码验证如下:

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;               
            }
        }
    }
}

程序运行截图:

bubuko.com,布布扣

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

代码如下:

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;
                
            }
        }
    }
}

 

2.C#面向对象基础属性,布布扣,bubuko.com

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

标签:style   blog   http   color   io   2014   cti   ar   

原文地址:http://www.cnblogs.com/xingyunblog/p/3885758.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!