继承和构造函数出错

时间:2013-12-21 08:47:16

标签: c# oop inheritance constructor

我目前正在研究一个涉及继承和构造函数的程序。我实际上是C#的新手,所以我只是想知道为什么下面的代码在第57行有错误,它说贷款不包含带0参数的构造函数。请帮我分析一下它的错误。非常感谢你。 使用System;

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;
        }
    }
}

5 个答案:

答案 0 :(得分:2)

如果不声明构造函数,C#编译器将生成无参数编译器。但是当你声明一个带有args的构造函数时,C#编译器不会给你那个默认值,所以你必须写一个。

 public Loan()
 {
 }

答案 1 :(得分: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参数的构造函数。因此要么创建一个,要么调用现有的一个。

答案 2 :(得分:0)

您收到该错误,因为您的Loan类不包含任何默认构造函数。默认构造函数意味着,Constructor没有任何arugments(参数)。

只要在任何类中创建参数化构造函数,就会销毁默认构造函数,因此,您无法像这样创建此类的实例:

  Loan obj = new Loan();

除非您创建默认构造函数(没有任何参数)

也在贷款类

中创建
public Loan()
{
}

答案 3 :(得分:0)

c#中的所有类都继承自Object类,如果没有定义无参数构造函数,则类继承Object类的默认无参数构造函数,但是如果定义构造函数需要一些参数,默认构造函数将被忽略。因此,在这种情况下,您必须定义无参数构造函数:

public Loan()
{

}

答案 4 :(得分: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构造函数,所以编译器默认使用无参数构造函数。解决方案是在Loan类上提供无参数构造函数:

    public Loan()
    {
    }

另一种选择是使用@Ondrej Janacek建议的解决方案。