2013-12-21 136 views
1

我目前正在學習一個涉及繼承和構造函數的程序。我實際上是C#中的新手,所以我只是想知道爲什麼下面的代碼在第57行有一個錯誤,它表示貸款不包含帶0參數的構造函數。請幫我分析一下它的錯誤。非常感謝你。使用系統的 ;繼承和構造函數的錯誤

class DemoCarLoan3 
{ 
    static void Main() 
    { 
     Loan aLoan = new Loan(333, "Hanson", 7000.00); 
CarLoan aCarLoan = new CarLoan(444, "Carlisle", 30000.00, 2011, "BMW"); 
     Console.WriteLine("Loan #{0} for {1} is for Php{2}", 
     aLoan.LoanNumber, aLoan.LastName, 
     aLoan.LoanAmount.ToString("0,000.00")); 
     Console.WriteLine("Loan #{0} for {1} is for Php{2}", 
aCarLoan.LoanNumber, aCarLoan.LastName, 
aCarLoan.LoanAmount.ToString("0.00")); 
     Console.WriteLine(" Loan #{0} is for a {1} {2}", 
     aCarLoan.LoanNumber, aCarLoan.Year, 
     aCarLoan.Make); 
     Console.ReadLine(); 
    } 
} 

class Loan 
{ 
    public const double MINIMUM_LOAN = 5000; 
    protected double loanAmount; 
    public int LoanNumber { get; set; } 
    public string LastName { get; set; } 
    public double LoanAmount 
    { 
     set 
     { 
      if (value < MINIMUM_LOAN) 
       loanAmount = MINIMUM_LOAN; 
      else 
       loanAmount = value; 
     } 
     get 
     { 
      return loanAmount; 
     } 
    } 

    public Loan(int num, string name, double amount) 
    { 
     LoanNumber = num; 
     LastName = name; 
     LoanAmount = amount; 
    } 
} 

class CarLoan : Loan 
{ 
    private const int EARLIEST_YEAR = 2006; 
    private const int LOWEST_INVALID_NUM = 1000; 
    private int year; 

    public CarLoan(int num, string name, double amount, int year, string make) 
    { 
     Year = year; 
     Make = make; 
    } 

    public int Year 
    { 
     set 
     { 
      if (value < EARLIEST_YEAR) 
      { 
       year = value; 
       loanAmount = 0; 
      } 
      else 
       year = value; 
     } 
     get 
     { 
      return year; 
     } 
    } 

    public string Make { get; set; } 

    public new int LoanNumber 
    { 
     get 
     { 
      return base.LoanNumber; 
     } 
     set 
     { 
      if (value < LOWEST_INVALID_NUM) 
       base.LoanNumber = value; 
      else 
       base.LoanNumber = value % LOWEST_INVALID_NUM; 
     } 
    } 
} 
+0

檢查http://stackoverflow.com/questions/7230544/c-sharp-error-parent-does-not-contain-a-constructor-that-takes-0-arguments – argaz

回答

2

只需添加調用基類的構造

public CarLoan(int num, string name, double amount, int year, string make) 
    : base(num, name, amount) 
{ 
    Year = year; 
    Make = make; 
} 

當你聲明在派生類不叫基本構造器的構造,那麼它會自動調用0參數,做一個構造函數不存在那裏。如果你把你的代碼,它會編譯爲

public CarLoan(int num, string name, double amount, int year, string make) 
    : base() 
{ 
    Year = year; 
    Make = make; 
} 

但是,這是無效的,因爲有一個在基類0參數沒有構造函數。所以要麼創建一個,要麼調用其中一個。

+0

感謝您的糾正,馬特:) –

0

你得到這個錯誤,因爲你的Loan類不包含任何默認構造函數。默認構造器意味着,構造器沒有任何指示(參數)。

只要你在任何類中創建一個參數的構造函數很快,默認構造函數被破壞,因此,你不能像這樣創建該類的實例:

Loan obj = new Loan(); 

,除非你創建一個默認的構造函數(不帶任何參數)

在您的貸款類

public Loan() 
{ 
} 
2

還創建此當你不聲明構造,C#編譯器會生成一個對無米之一。但是當你聲明一個帶參數的構造函數時,C#編譯器不會給你那個默認的,所以你必須編寫一個。

public Loan() 
{ 
} 
+0

哦,我明白這就是爲什麼我需要聲明一個無參數的構造函數。另一個問題是代碼運行,但在這些行中,Console.WriteLine(「{1}的貸款#{0}用於Php {2}」,aCarLoan.LoanNumber,aCarLoan.LastName,aCarLoan.LoanAmount.ToString(「0.00」 )); Console.WriteLine(「貸款#{0}用於{1} {2}」,aCarLoan.LoanNumber,aCarLoan.Year,aCarLoan.Make);'它不顯示aCarLoan.LoanNumber和aCarLoan.LastName 。爲什麼? – user3103939

+0

@ user3103939這是因爲當你創建'CarLoan'時,無參數的空構造函數在基類上運行。在'Loan'屬性設置的基類中沒有運行的構造函數。因此,也許在你的情況下,重載'Loan'的構造函數是一個壞主意。對於沒有無參數實例構造函數的類來說是好的;繼承人必須知道。 –

0

在C#中的所有類從Object類繼承,如果沒有定義參數的構造函數,類從Object類繼承一個默認的無參數的構造函數,但是如果你定義一個構造函數,需要一些參數,默認構造函數都會被忽略。所以在這種情況下,你必須定義一個參數的構造函數:

public Loan() 
{ 

} 
0

您的來電創建CarLoan對象:

CarLoan aCarLoan = new CarLoan(444, "Carlisle", 30000.00, 2011, "BMW"); 

調用此構造函數:

public CarLoan(int num, string name, double amount, int year, string make) 
    { 
     ... 
    } 

什麼可能不是很明顯的是,如果你這樣寫的CarLoan構造函數編譯:

public CarLoan(int num, string name, double amount, int year, string make) 
     : base() 
    { 
     ... 
    } 

在構建派生的對象,基類構造函數首先自動調用(即使你不要明確)。更一般地說,對象是以自頂向下的方式構建的,從最基類到最派生類。因爲您沒有指定要使用哪個構造函數,所以編譯器默認使用無參數構造函數。該解決方案是提供關於Loan類參數的構造函數:

public Loan() 
    { 
    } 

另一種選擇是使用由@Ondrej雅納切克建議的解決方案。