Base构造函数的参数丢失了

本文关键字:参数 构造函数 Base | 更新日期: 2023-09-27 18:15:54

我正在研究一个派生类,我已经将派生类链接到父类,我正在派生类中设置构造函数,但我得到的错误是"值不存在于当前上下文中"当它们已经在父类中声明时。造成这种情况的原因是什么,怎样才能解决?

父类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HousingNameSpace
{
    public abstract class Housing
    {
        private int sqFt;
        private int zip;
        private int yrBlt;
        public Housing(int TSqFt, int Tzip, int TyrBlt)
        {
            sqFt = TSqFt;
            zip = Tzip;
            yrBlt = TyrBlt;
        }
       public int SqFt
       {
           get { return sqFt; }
           set { sqFt = value; }
       }

    public int ZIP
    {
        get { return zip; }
        set { zip = value; }

       }
       public int YrBlt
       {
           get{ return yrBlt; }
           set { yrBlt = value; }
       }

       public abstract double GetRentAmmount();
   }
  }

派生类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HousingNameSpace;
namespace MultiTennant
{
public class Multi:Housing
{
    private string buildingtype;
    private int occupants;
    public string BuildingType
    {
        get{ return buildingtype; }
        set { buildingtype = value; }
    }
    public int Occupants
    {
        get { return occupants; }
        set { occupants = value; }
    }
    public Multi (string TbuildType) : base (TSqrFt, Tzip,TyrBlt)
    { }
}
}

Base构造函数的参数丢失了

你需要在子类的构造函数中使用你的基构造函数参数。

public Multi (string TbuildType, int TSqrFt, int Tzip, int TyrBlt) 
    : base (TSqrFt, Tzip,TyrBlt)

样式提示:变量的命名更清晰。squareFootage, yearBuilt等…你以后会感谢自己的。

哦,我知道这与问题无关(对不起),但如果我能提出另一个建议:

public enum BuildingType
{
    House,
    Apartment,
    Condo
    //etc
}
public interface IHousing
{
    int SquareFootage { get; set; }
    int YearBuilt { get; set; }
    int ZipCode { get; set; }
    int MaximumOccupants { get; set; }    
    int MortgageAmount { get; }    
    int RentalIncome { get; } 
    BuildingType BuildingType { get; }
}

public class MultiPersonHouse 
    : IHousing
{
    public int SquareFootage { get; set; }
    public int YearBuilt { get; set; }
    public int ZipCode { get; set; }
    public int MaximumOccupants { get; set; }
    public int MortgageAmount { get; private set; }    
    public int RentalIncome { get; private set;} 
    public BuildingType BuildingType { get; private set;}
    public MultiPersonHouse(int squareFootage, int yearBuilt /*etc...*/)
    {
        //set properties.
        this.BuildingType = BuildingType.House;
        this.MortgageAmount = 0; //Free!
        this.RentalIncome = 0; //Free rent too!
    }
}

然后每个房屋类型一个。你可能想让MaximumOccupants只得到一个。不希望有人改变你的最大占用率