| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 678 人关注过本帖
标题:[新手求助]关于get、set
只看楼主 加入收藏
bananachueng
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-12-7
结帖率:50%
收藏
 问题点数:0 回复次数:4 
[新手求助]关于get、set
大家先看下面的代码:

程序代码:
using System;

public class AccountTest
{
    // Main method begins execution of C# application
    public static void Main(string[] args)
    {
        Account account1 = new Account( 50.00M ); // create Account object
        Account account2 = new Account( -7.53M ); // create Account object

        // display initial balance of each object using a property
        Console.WriteLine( "account1 balance: {0:C}",
            account1.Balance ); // display Balance property
        Console.WriteLine( "account2 balance: {0:C}\n",
            account2.Balance ); // display Balance property

        decimal depositAmount; // deposit amount read from user

        // prompt and obtain user input
        Console.Write( "Enter deposit amount for account1: " );
        depositAmount = Convert.ToDecimal( Console.ReadLine() );
        Console.WriteLine( "adding {0:C} to account1 balance\n",
            depositAmount );
        account1.Credit( depositAmount ); // add to account1 balance

        // display balances
        Console.WriteLine( "account1 balance: {0:C}",
            account1.Balance );
        Console.WriteLine( "account2 balance: {0:C}\n",
            account2.Balance );

        // prompt and obtain user input
        Console.Write( "Enter deposit amount for account2: " );
        depositAmount = Convert.ToDecimal( Console.ReadLine() );
        Console.WriteLine( "adding {0:C} to account2 balance\n",
            depositAmount );
        account2.Credit( depositAmount ); // add to account2 balance

        // display balances
        Console.WriteLine( "account1 balance: {0:C}", account1.Balance );
        Console.WriteLine( "account2 balance: {0:C}", account2.Balance );
    }
}



程序代码:
public class Account
{
    private decimal balance; // instance variable that stores the balance

    // constructor
    public Account(decimal initialBalance)
    {
        Balance = initialBalance; // set balance using property
    } // end Account constructor

    // credit (add) an amount to the account
    public void Credit(decimal amount)
    {
        Balance = Balance + amount; // add amount to balance
    } // end method Credit

    // a property to get and set the account balance
    public decimal Balance
    {
        get
        {
            return balance;
        } // end get
        set
        {
            // validate that value is greater than or equal to 0;
            // if it is not, balance is left unchanged
            if (value >= 0)
                balance = value;
        } // end set
    } // end property Balance
} // end class Account

最后运行结果是:
程序代码:
account1 balance: ¥50.00
account2 balance: ¥0.00

Enter deposit amount for account1: 10
adding ¥10.00 to account1 balance

account1 balance: ¥60.00
account2 balance: ¥0.00

Enter deposit amount for account2: 10
adding ¥10.00 to account2 balance

account1 balance: ¥60.00
account2 balance: ¥10.00


问题:
在Account类中的set和get只判定了value >= 0的情况就返回值,而如果value < 0的话,运行结果Balance则会是0. 为什么没有添加value < 0的情况,返0值呢?


2011-01-09 11:55
qingshuiliu
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:17
帖 子:323
专家分:1538
注 册:2009-12-28
收藏
得分:0 
这里其实有个默认的值,decimal默认为0,所以,当value<=0的情况下,自然为0,不需要程序中写。
当然你也可以写成这样的。
public decimal Balance
    {
        get
        {
            return balance;
        } // end get
        set
        {
            // validate that value is greater than or equal to 0;
            // if it is not, balance is left unchanged
            if (value >= 0)
            {
                balance = value;
            }
            else
            {
                balance=0;
             }
        } // end set
    } // end property Balance
2011-01-09 16:47
bananachueng
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-12-7
收藏
得分:0 
回复 2楼 qingshuiliu
谢谢帮助。我再研究下
2011-01-10 22:11
makebest
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:3
帖 子:658
专家分:962
注 册:2005-3-17
收藏
得分:0 
很明显,这是一个借记卡帐号,不可以为负值.
2011-01-14 12:20
jacky_2010
Rank: 2
等 级:论坛游民
帖 子:19
专家分:36
注 册:2010-10-13
收藏
得分:0 
顶2楼一下
其实说的再明白点,类在初始化的时候会调用构造函数,如果调用的是无参构造函数的话,类中所有的字段的初始化情况如下:
1.值类型初始化为0。
2.引用类型初始化为NULL。
3.bool类型初始化为FALSE。
4.枚举初始化为枚举的第一项值。(这个是凭印象的,楼主可以自己设个枚举字段调式)
5.结构字段,这个结构本身什么值不清楚,但是结构里面的字段类型同上。
2011-01-14 12:48
快速回复:[新手求助]关于get、set
数据加载中...
 
   



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

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