C# 面向对象(1)

1, 计算银行利息的类

using System;


public class SavingsAccount
{
    private static double annualInterestRate = 0.0; // the static variable must be assign value
    private double savingsBalance;

    public SavingsAccount() //constructor 
	{
        annualInterestRate = 0.04;
	}

    public SavingsAccount(double s) //second constructor
    {
        savingsBalance = s;
    }

    public void CalculateMonthlyInterest() // the method for calculate interest
    {
        savingsBalance += (savingsBalance * annualInterestRate) / 12;
    }

    public static void ModifyInterestRate(double newValue) // static method for modify the interest rate
    {
        annualInterestRate = newValue;
    }

    public override string ToString()  //Override the ToString() method, for eaiser
    {
        return string.Format("$ {0}", savingsBalance);
    }

}

public class displays {

    public static void Main()
    {
        SavingsAccount sa1 = new SavingsAccount(2000.00); //instance the class

        SavingsAccount.ModifyInterestRate(0.04); // the static method can call directly, without instantiation

        for (int i = 1; i <= 12; i++)
        {
            sa1.CalculateMonthlyInterest(); // call the calculate method
            Console.WriteLine("Month {0}: {1}", i, sa1.ToString()); //output by ToString(), which has been override
        }

    }

}

1, 静态方法可以被直接在其他类中调用 不需要实例化

2, 静态变量需要初始化

3, 覆盖.ToString()的方法 为了简便程序的输出

**************************************************************************************************************************************************************************************

2,重载构造函数,字段set方法的权限设置(以时间类为例子)

using System;

public class timers
{
    private int Hours;
    private int Minutes;
    private int Seconds;

    

    public timers(int h)
        : this(h, 0, 0) { }

    public timers(int h,int m)
        : this(h, m, 0) { }

    public timers(int h, int m, int s)
    {
        SetTimes(h,m,s);
    }
    public void SetTimes(int hour, int minute, int second)
    {
        
        this.Hours = hour;

        if (minute > 60)
        {
            Hours += minute / 60;
            Minutes = minute % 60;
        }
        else
        {
            Minutes = minute;
        }

        if (second > 60)
        {
            Minutes += second / 60;
            Seconds = second % 60;
        }
        else
        {
            Seconds = second;
        }
    }



    public int Hourss
    {
        get
        {
            return Hours;
        }

        private set
        {
            Hours = (value > 0 && value <= 24) ? value : 0;
        }
    }

    public int Minutess
    {
        get
        {
            return Minutes;
        }
        private set
        {
            Minutes = (value > 0 && value <= 60) ? value : 0;
        }
    }

    public int Secondss
    {
        get
        {
            return Seconds;
        }
        private set
        {
            Seconds = (value > 0 && value <= 60) ? value : 0;
        }
    }



    public string ToTimeFormat()
    {
        return string.Format("{0}:{1}:{2}",Hours, Minutes, Seconds);
    }

    public override string ToString()
    {
        return this.ToTimeFormat();
    }
}

public class useTimer {

    public static void Main(String[] args)
    { 
        int h = 0;
        int m = 0;
        int s = 0;
        timers t = new timers(h,m,s);
       

        Console.WriteLine("Please enter hours:");
        h = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Please enter minute:");
        m = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Please enter second:");
        s = Convert.ToInt32(Console.ReadLine());

        t.SetTimes(h,m,s);
        Console.Write(t.ToTimeFormat());
        Console.WriteLine();
        t.ToString();

        //t.Hourss = 15;  // the set method's accessible is private, can not set value without 
        Console.WriteLine("The Hour is: {0}", t.Hourss);
    }

}

***************************************************************************************************************************************************************************************

3, 静态成员的作用

例如 输入员工信息 其ID进行递增 如果将ID设置成静态 则可以节省资源 不必每次都创建一个新的对象

using System;


public class employees
{
    private static int eid = 0;
    private string firstname;
    private string surname;

    public static int Eid {
        get {
            return eid;
        }
    }

	public employees(string f, string s)
	{
        this.firstname = f;
        this.surname = s;
        eid++;
	}

    public void SetName(string f, string s)
    {
        this.firstname = f;
        this.surname = s;
        eid++;
    }

    public override string ToString()
    {
        return string.Format("ID: {0}  UserName: {1} {2}",eid, firstname, surname);
    }
}

public class employeeTest {
    public static void Main(string[] args)
    {
        
        employees e1 = new employees("kidd", "zhang");

        while (true)
        {
            Console.WriteLine("please input your firstname:");
            string fname = Console.ReadLine().ToString();
            Console.WriteLine("please input your surname:");
            string sname = Console.ReadLine().ToString();
            e1.SetName(fname, sname);
            Console.WriteLine(e1.ToString());
            Console.WriteLine("Continue? y/n");
            string yon = Console.ReadLine().ToString();
            if (yon == "n")
            {
                break;
            }
        }
    }
}

Output:

***************************************************************************************************************************************************************************************

Lambda   =>

http://msdn.microsoft.com/zh-cn/library/bb397687.aspx

using System;

public class delegating
{
    delegate int del(int i);
    static void Main(string[] args)
    {
        del myDelegate = x => x * x;
        int j = myDelegate(5); //j = 25
        Console.WriteLine(j.ToString());

        del myDelegate1 = y => y + 10; // 相当于将新的对象当成y 然后进入操作
        int j1 = myDelegate1(5); //j1 = 50
        Console.WriteLine(j1.ToString());

    }
}



***************************************************************************************************************************************************************************************

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值