我從this article複製了此代碼,我不明白爲什麼將類內部的類定義爲屬性。另外,當類PersonalLoan
被實例化時會發生什麼?將類聲明爲另一個類中的屬性
public class PersonalLoan
{
public string AccountNumber { get; set; }
public string AccounHolderName { get; set; }
public Loan LoanDetail { get; set; }
public PersonalLoan(string accountNumber)
{
this.AccountNumber = accountNumber;
this.AccounHolderName = "Sourav";
this.LoanDetail = new Loan(this.AccountNumber);
}
}
public class Loan
{
public string AccountNumber { get; set; }
public float LoanAmount { get; set; }
public bool IsLoanApproved { get; set; }
public Loan(string accountNumber)
{
Console.WriteLine("Loan loading started");
this.AccountNumber = accountNumber;
this.LoanAmount = 1000;
this.IsLoanApproved = true;
Console.WriteLine("Loan loading started");
}
}
你在哪裏看到一個類作爲屬性? – 2014-10-31 15:10:29
如果你問這個'公共貸款LoanDetail {get;組; }',那麼它是[Composition](http://en.wikipedia.org/wiki/Object_composition)。您可能還會看到:[構圖與聚合之間的區別](http://www.c-sharpcorner.com/UploadFile/pcurnow/compagg07272007062838AM/compagg.aspx) – Habib 2014-10-31 15:10:38
@Habib謝謝,請您告訴我, PersonalLoan'被實例化? – Constantine 2014-10-31 15:15:20