没有给出对应于水管工所需参数的参数

时间:2017-03-12 04:07:02

标签: c# inheritance

using System;

namespace lab4
{
    class Lab4
    {
        static void Main(string[] args)
        {
            Prompt.prompt();
        }
    }
}

public class CommissionEmployee
{
    public string FirstName { get; }
    public string LastName { get;}
    public string SSN { get;}
    private decimal grossSales;
    private decimal commissionRate;




        public CommissionEmployee( string firstName, string lastName, string ssn, decimal grossSales, decimal commissionRate)
    {
        FirstName = firstName;
        LastName = lastName;
        GrossSales = grossSales;
        CommissionRate = commissionRate;//might be wrong


    }

    public decimal GrossSales
    {
        get
        {
            return grossSales;
        }
        set
        {
            if (value < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(value),
                value, $"{nameof(GrossSales)} must be >=0");
            }


            grossSales = value;
        }
    }

    public decimal CommissionRate
    {
        get
        {
            return commissionRate;
        }
        set
        {
            if (value <=0 || value >=1)
            {
                throw new ArgumentOutOfRangeException(nameof(value),
                value, $"{nameof(CommissionRate)} must be > 0 and <1");
            }
            grossSales = value;
        }



    }

    public virtual decimal Earnings() => CommissionRate * GrossSales;



}


public class Plumber : CommissionEmployee
{

    private string custName;
    private string Location;

    public Plumber(string firstName, string lastName, string ssn, decimal grossSales, decimal commissionRate, string custName, string location) :
         base (firstName, lastName, ssn, grossSales, commissionRate)
    {

        custName = CustName;
        Location = location;

    }

    public string CustName
    {
        get
        {
            return custName;
        }
        set
        {
            CustName = value;
        }
    }
    public string location
    {
        get
        {
            return Location;
        }
        set
        {
            location = value;
        }
    }



    public override decimal Earnings() => GrossSales * CommissionRate;

    public override string ToString() =>
        $"Name: {FirstName} {LastName}/n" +
        $"Social Security Number: {SSN}/n" +
        $"Sales: {GrossSales}/n" +
        $"Rate of Commission: {CommissionRate}/n" +
        $"Customer Name: {CustName}/n" +
        $"Location of Service: {Location}/n" +
        $"Earnings: {Earnings()}/n";



}

public class Prompt
{

    public static void prompt()
    {

        Plumber Plumber1 = new Plumber();
        Console.WriteLine("Please enter the plumber's first name: ");
        string first = Console.ReadLine();
        Plumber1.CustName = first;


    }
}

}

它不允许我在不填写参数的情况下创建新的Plumber对象。但是,分配试图让我提示用户输入这些属性。它不会让我使用空参数。错误CS7036

1 个答案:

答案 0 :(得分:0)

Plumber Plumber1 = new Plumber();

这是调用默认构造函数,但您的Plumber类没有默认构造函数(没有参数)。 Plumber中唯一的构造函数是重载的构造函数:

 public Plumber(string firstName, string lastName, string ssn, decimal grossSales, decimal commissionRate, string custName, string location) :

如果你想这样做,你需要为Plumber添加一个默认构造函数,但是Plumber继承自CommissionEmployee,它只有一个非默认的构造函数,所以现在任何Plumber的构造函数都必须调用非基类CommissionEmployee的默认构造函数。因此,您可能希望向CommisionEmployee添加默认构造函数:

public CommissionEmployee() {}
...
public Plumber() : base() {}