我试图使用 FreshItem 子类中的新方法重载 PurchasedItem 中的 FindCost 方法。新的 FreshItem FindCost 方法使用权重而不是数量来确定成本。
我得到错误:
没有任何论据符合所需的形式参数' gItem'
' Program.GroceryItem.PurchasedItem.FreshItem.FindCost()'隐藏继承的成员' Program.GroceryItem.PurchasedItem.FindCost()'。如果想要隐藏,请使用new关键字。
如果项目是新鲜的,我如何才能使用 FreshItem FindCost 方法?
class Program
{
public class GroceryItem
{
public string Name;
public double Price;
public PurchasedItem pItem
{
get;
set;
}
public GroceryItem()
{
}
public GroceryItem(string name, double price)
{
this.Name = name;
this.Price = price;
}
public class PurchasedItem
{
public int Quantity;
GroceryItem item;
public PurchasedItem(GroceryItem gItem)
{
item = new GroceryItem(gItem.Name, gItem.Price);
}
public double FindCost()
{
return item.Price * this.Quantity * 1.10;
}
public class FreshItem : PurchasedItem
{
public FreshItem()
{
}
public double weight;
public double FindCost()
{
return this.item.Price * this.weight;
}
}
}
public static void Main(string[] args)
{
var item1 = new GroceryItem("Lettuce", 2.05);
item1.pItem = new GroceryItem.PurchasedItem(item1);
item1.pItem.Quantity = 12;
double cost1 = item1.pItem.FindCost();
Console.WriteLine(item1.Name + " " + item1.Price);
Console.WriteLine(cost1);
}
}
}
答案 0 :(得分:3)
当基类具有带参数的构造函数时,您应该将其称为明确的。 它一定是:
public FreshItem():base(/*pass paramter*/)
{
}
请参阅:Derived class explicit base constructor call
对于您的第二个问题,PurchasedItem
和FreshItem
必须从GroceryItem
继承并实施FindMethod
:
public abstract class GroceryItem
{
public string Name {set;get;}
public double Price {set;get;}
public GroceryItem(string name, double price){
this.Name = name;
this.Price = price;
}
public abstract double FindCost();
}
public class PurchasedItem: GroceryItem
{
public int Quantity {set;get;}
public PurchasedItem(string name, double price, int quntity):base(name, price)
{
this.Quantity = quntity;
}
public override double FindCost()
{
return this.Price * this.Quantity * 1.10;
}
}
public class FreshItem : GroceryItem
{
public double Weight {set;get;}
public FreshItem(string name, double price, int weight) : base(name, price)
{
this.Weight= weight;
}
public override double FindCost()
{
return this.Price * this.Weight;
}
}
public static void Main(string[] args)
{
var item1 = new PurchasedItem("Lettuce", 2.05,12);
double cost1 = item1.FindCost();
}
答案 1 :(得分:1)
要修复第一个错误,请使用Mazaher Bazari回答
但您的代码仍然无法像您想要的那样工作
尝试像这样修复
class Program
{
public static void Main(string[] args)
{
var item1 = new GroceryItem("Lettuce", 2.05);
item1.pItem = new FreshItem(item1);
item1.pItem.Quantity = 12;
double cost1 = item1.pItem.FindCost();
Console.WriteLine(item1.Name + " " + item1.Price);
Console.WriteLine(cost1);
Console.ReadLine();
}
}
public class PurchasedItem
{
public int Quantity;
protected GroceryItem item;
public PurchasedItem(GroceryItem gItem)
{
item = new GroceryItem(gItem.Name, gItem.Price);
}
public virtual double FindCost()
{
return item.Price * this.Quantity * 1.10;
}
}
public class FreshItem : PurchasedItem
{
public double weight;
public FreshItem(GroceryItem gItem) : base(gItem)
{
}
public override double FindCost()
{
return this.item.Price * this.weight;
}
}
public class GroceryItem
{
public string Name;
public double Price;
public PurchasedItem pItem
{
get;
set;
}
public GroceryItem()
{
}
public GroceryItem(string name, double price)
{
this.Name = name;
this.Price = price;
}
}
你应该添加Virtual
和Override
来覆盖方法,如果不需要新实例作为FreshItem来使用FreshItem FindCost
答案 2 :(得分:1)
你这样做:
FreshItem : PurchasedItem
所以你继承了PurchasedItem.FindCost
,但你在FindCost
中也有FreshItem
方法,所以你会收到这个错误,这个错误非常清楚并抱怨你上面做了什么:
Program.GroceryItem.PurchasedItem.FreshItem.FindCost()'隐藏继承的成员' Program.GroceryItem.PurchasedItem.FindCost()'。如果想要隐藏,请使用new关键字。
要修复此错误,您可以将PurchasedItem.FindCost
声明为虚拟,以便您可以覆盖它:
public virtual double FindCost()
然后在FreshItem
中覆盖它:
public override double FindCost()
对于其他错误,请参阅@MazaherBazari的答案。