| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦   
共有 234 人关注过本帖
标题:初学C#明白怎样设置和调用属性
收藏  订阅  推荐  打印
kevin87
Rank: 1
等级:新手上路
帖子:17
积分:306
注册:2008-7-26
初学C#明白怎样设置和调用属性

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication8
{
    class SavingAccount
    {
        //用于存储帐号\余额和已获利息的类字段
        private int _accountNumber;
        private double _balance;
        private double _interestEarned;
        //利率是静态的,因为所有的帐户都使用相同的利率
        private static double _interestRate;
        //构造函数初始化类成员
        public SavingAccount(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("利息不能为负数");
                    return;
                }
                _interestEarned = value;
            }
        }
        //InterestRate 读/写属性为静态
        //因为所有特定类型的帐户都具有相同的利率
        public static double  InterestRate
        {
            get
            {
                return _interestRate;
            }
            set
            {
                //验证数据
                if (value < 0.0)
                {
                    Console.WriteLine("利率不能为负数");
                    return;
                }
                else
                {
                    _interestRate = value / 100;
                }
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication8
{
    class TestSavingAccount
    {
        [STAThread ]
        static void Main(string[] args)
        {
            //创建 SavingAccount 的对象
            SavingAccount objSavingAccount = new SavingAccount(12345,5000);
            //用户交互
            Console.WriteLine("----------------C#第六章示例1--此程序让我明白怎样设置和调用属性-----------------");
            Console.WriteLine("输入到现在为止已经获得的利息和利率");
            objSavingAccount.InterestEarned = Int64.Parse(Console .ReadLine ());
            SavingAccount.InterestRate = Int64.Parse(Console .ReadLine ());
            //使用类名访问静态属性
            objSavingAccount.InterestEarned += objSavingAccount.Balance * SavingAccount.InterestRate;
            Console.WriteLine("获得的总利息为:{0}", objSavingAccount.InterestEarned);
        }
    }
}
2008-7-28 11:26
共有 233 人关注过本帖
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.052240 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved