Chinaunix首页 | 论坛 | 博客
  • 博客访问: 648902
  • 博文数量: 107
  • 博客积分: 4135
  • 博客等级: 上校
  • 技术积分: 1182
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-06 16:01
文章分类

全部博文(107)

文章存档

2020年(2)

2012年(5)

2011年(6)

2010年(23)

2009年(17)

2008年(35)

2007年(19)

我的朋友

分类: C/C++

2009-09-28 10:24:26

c#属性详解

C# 编程指南
属性(C# 编程指南)

属性是这样的成员:它们提供灵活的机制来读取、编写或计算私有字段的值。可以像使用公共数据成员一样使用属性,但实际上它们是称为“访问器”的特殊方法。这使得数据在可被轻松访问的同时,仍能提供方法的安全性和灵活性。

在本示例中,类 TimePeriod 存储了一个时间段。类内部以秒为单位存储时间,但提供一个称为 Hours 的属性,它允许客户端指定以小时为单位的时间。Hours 属性的访问器执行小时和秒之间的转换。

C#
class TimePeriod
{
    private double seconds;

    public double Hours
    {
        get { return seconds / 3600; }
        set { seconds = value * 3600; }
    }
}

class Program
{
    static void Main()
    {
        TimePeriod t = new TimePeriod();

        // Assigning the Hours property causes the 'set' accessor to be called.
        t.Hours = 24;

        // Evaluating the Hours property causes the 'get' accessor to be called.
        System.Console.WriteLine("Time in hours: " + t.Hours);
    }
}

Time in hours: 24

  • 属性使类能够以一种公开的方法获取和设置值,同时隐藏实现或验证代码。

  • 属性访问器用于返回属性值,而 访问器用于分配新值。这些访问器可以有不同的访问级别。有关更多信息,请参见。

  • 关键字用于定义由 set 索引器分配的值。

  • 不实现 set 方法的属性是只读的

 

 

C#属性是对类中的字段(fields)的保护,像访问字段一样来访问属性。同时 也就封装了类的内部数据
using System;
using System.Collections.Generic;
using System.Text;

namespace Example_1
{
    
class age
    
{
        
private int _age;
        
public void SetAge(int a)
        
{
            
if (a < 0)
            
{
                Console.WriteLine(
"must an +int");
 
            }

            
else
            
{
                _age 
= a;
            }

        }


        
public int GetAge()
        
{
            
return _age;
        }

    }


    
class TestAge
    
{
        [STAThread]
        
static void main(string[] args)
        
{
            age Age 
= new age();
            Console.WriteLine(
"请输入年龄:");
            Age.SetAge(Int32.Parse( Console.ReadLine()));
            
int a = Age.GetAge();
            Console.WriteLine(
"{0}",a);
        }

    }

}

上面的例子是假如没有属性时候 ,我们限制对字段的访问。

感觉起来它很烦琐,因为每次访问的时候 1 个field就需调用2个方法。有了属性就不用这样麻烦了。

属性的类型:1.读写属性 2.只读属性 3只写属性(他的诞生是错误的) 4.静态属性

属性的声明:【访问符号】(static) 数据类型 属性名
            { 
                set{};
                get{};
            }
属性的特点:每当赋值运算的时候自动调用set访问器,其他时候则调用get访问器。 以帕斯卡命名 不能冠以Get Set

注意:静态属性是通过类名调用的哦 下面的一个例子里面具体有展示 请仔细阅读
using System;

namespace Example_1
{
    
class SavingsAccount
    
{
        
//用于存储帐户号码、余额和已获利息的类字段。
        private int _accountNumber;
        
private double _balance;
        
private double _interestEarned;
        
        
// 利率是静态的,因为所有的帐户都使用相同的利率
        private static double _interestRate;

        
// 构造函数初始化类成员
        public SavingsAccount(int accountNumber, double balance)
        
{
            
this._accountNumber = accountNumber;
            
this._balance       = balance;
        }


        
// AccountNumber只读属性
        public int AccountNumber
        
{
            
get
            
{
                
return _accountNumber;
            }

        }


        
// Balance 只读属性
        public double Balance
        
{
            
get
            
{
                
if (_balance < 0)
                    Console.WriteLine(
"无余额");
                
return _balance;
            }

        }


        
// InterestEarned 读/写属性
        public double InterestEarned
        
{
            
get
            
{
                
return _interestEarned;
            }


            
set
            
{
                
// 验证数据
                if (value < 0.0)
                
{
                    Console.WriteLine(
"InterestEarned 不能为负数");
                    
return;
                }

                _interestEarned 
= value;
            }

        }


        
// InterestRate 读/写属性为静态,
        
// 因为所有特定类型的帐户都具有相同的利率
        public static double InterestRate
        
{
            
get
            
{
                
return _interestRate;
            }


            
set
            
{
                
// 验证数据
                if (value < 0.0)
                
{
                    Console.WriteLine(
"InterestRate 不能为负数");
                    
return;
                }

                
else
                
{
                    _interestRate 
= value / 100;
                }

            }

        }

    }

    

    
class TestSavingsAccount
    
{
        
/// <摘要>
        
/// 应用程序的主入口点。
        
/// 

        [STAThread]
        
static void Main(string[] args)
        
{
            
// 创建 SavingsAccount 的对象
            SavingsAccount objSavingsAccount = new SavingsAccount(123455000);;
            
// 用户交互
            Console.WriteLine("输入到现在为止已获得的利息和利率");
            objSavingsAccount.InterestEarned 
= Int64.Parse(Console.ReadLine());
            SavingsAccount.InterestRate 
= Int64.Parse(Console.ReadLine());
            
            
// 使用类名访问静态属性
            objSavingsAccount.InterestEarned += objSavingsAccount.Balance * SavingsAccount.InterestRate;
            Console.WriteLine(
"获得的总利息为: {0}", objSavingsAccount.InterestEarned);
        }

    }

}

阅读(1054) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~