| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 805 人关注过本帖
标题:初学C#明白怎样设置和调用属性
只看楼主 加入收藏
kevin87
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2008-7-26
收藏
 问题点数:0 回复次数:0 
初学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);
        }
    }
}
搜索更多相关主题的帖子: 利息 double 属性 private using 
2008-07-28 11:26
快速回复:初学C#明白怎样设置和调用属性
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033015 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved