本帖最后由 朱勋 于 2011-12-11 23:16 编辑
王旭 发表于 2011-12-11 22:45
构造函数的操作,说白了就是在实例化对象时,按照不同的需要去构造特殊的对象。构造函数之间的调用应该说和 ...
构造函数调用够造函数就是出现在同一类当中,- using System;
- namespace WindowsApplication2
- {
- /// <summary>
- /// Summary description for BankCustomer.
- /// </summary>
- public class BankCustomer
- {
- public string firstName;
- public string lastName;
- private double balance;
- private Person[] relatives;
- public BankCustomer()
- {
- //
- // TODO: Add constructor logic here
- //
- this.firstName = "";
- this.lastName = "";
- this.balance = 0.0;
- relatives = new Person[100];
- }
- public BankCustomer(string fn, string ln, double bal)
- {
- this.firstName = fn;
- this.lastName = ln;
- this.balance = bal;
- }
- // public BankCustomer(string fn, string ln)
- // {
- // this.firstName = fn;
- // this.lastName = ln;
- // this.balance = 0.0;
- // }
- public BankCustomer(string fn, string ln) //调用了三个参数的构造函数
- : this(fn, ln, 0.0)
- { }
- public double Balance
- {
- get
- {
- return this.balance;
- }
- set
- {
- if(value<0.0)
- System.Console.WriteLine("The amount under zero");
- else
- this.balance = value;
- }
- }//property
- public Person this [int index]
- {
- get
- {
- return relatives[index];
- }
- set
- {
- if (value != null)
- {
- relatives [index] = value;
- }
- }
- }//indexer
- }
- }
复制代码 |