C# 新对象无法访问新方法
你要给谁打电话? 提出问题的无关代码。不是最好的编辑器工具,所以要简洁。谢谢 作为派生类一部分的新方法无法被新方法访问 对象所有Intellisense看到的都是基类的抽象部分。输入并运行它们会出错。如果无法添加方法和字段,则派生的基点是什么,并向下。我搜索了所有的例子,结果都是空的C# 新对象无法访问新方法,c#,class,derived,C#,Class,Derived,你要给谁打电话? 提出问题的无关代码。不是最好的编辑器工具,所以要简洁。谢谢 作为派生类一部分的新方法无法被新方法访问 对象所有Intellisense看到的都是基类的抽象部分。输入并运行它们会出错。如果无法添加方法和字段,则派生的基点是什么,并向下。我搜索了所有的例子,结果都是空的 public class SalesEmployee : Employee { public decimal salesbonus; // Additional field public Sale
public class SalesEmployee : Employee
{
public decimal salesbonus; // Additional field
public SalesEmployee(string name, decimal basepay, decimal salesbonus)
{
this.salesbonus = salesbonus; // Create new field
}
public override decimal CalculatePay() // Override abstract
{
return basepay + salesbonus;
}
public decimal CalculateExtraBonus() // Not an override
{
return basepay + (0.5 * salesbonus); // Belongs to this class only
}
}
static void Main()
{
// Create new employee.
SalesEmployee employee1 = new SalesEmployee("Alice", 1000, 500);
decimal = employee1.CalculateExtraBonus(); // Can't see the new method
// Derived class cannot get to new method.
}
我想试试下面的方法。把问题打出来真的很有帮助
{ SalesEmployee salesEmpInstance = employee1
decimal = salesEmpInstance.CalculateExtraBonus()
// Maybe this could see the method.
我将忽略语法错误,并假设您在实际代码中的语法错误是正确的。但是,您似乎忘记调用派生类构造函数上的父构造函数(“base”),然后尝试访问仅由父构造函数实例化的变量。还需要将文字“0.5”转换为十进制 工作代码如下。产量为1250
using System;
public abstract class Employee
{
public string name;
public decimal basepay;
public Employee(string name, decimal basepay)
{
this.name = name;
this.basepay = basepay;
}
public abstract decimal CalculatePay();
}
public class SalesEmployee : Employee
{
public decimal salesbonus; // Additional field
// -->ERROR HERE, You forgot to call the base and instantiate
// the fields of the parent.
public SalesEmployee(string name, decimal basepay, decimal salesbonus): base(name, basepay)
{
this.salesbonus = salesbonus; // Create new field
}
public override decimal CalculatePay() // Override abstract
{
return basepay + salesbonus;
}
public decimal CalculateExtraBonus() // Not an override
{
return basepay + ((decimal)0.5 * salesbonus); // Belongs to this class only
}
}
class Program
{
static void Main(string[] args)
{
SalesEmployee employee1 = new SalesEmployee("Alice", 1000, 500);
decimal aliceBonus = employee1.CalculateExtraBonus();
Console.WriteLine(aliceBonus);
}
}
您的代码看起来不错,我建议您:
新项目
中删除对一级库的引用
一流库
新项目中的第一类库
dll文件
,可能引用了较旧版本的dll。您应该参考上次创建的一级库的dll
无论如何,如果你在这种情况下没有任何参考。将以下代码与您的代码进行比较。我测试过了。它是有效的 注1:您应该在构造函数中使用
base
将name
和basepay
传递给基类中相应的字段
注2:现在重建您的项目,您有任何错误吗?我没有!您是否有VS Intellisense
问题
代码看起来不错,只需添加适当的分号即可。您重新启动IDE(VisualStudio)了吗?您需要在底部为十进制变量命名。我想您忘记了变量名:decimal bonus=employee1.CalculateExtraBonus()代码>我尝试了我的建议,它确实有效。我仍然想要一个答案来解释我遗漏了什么。正如我说的,我把它输入了我唯一能读到的文本,所以它并不完美。你知道我的问题的答案吗?谢谢。这个问题不涉及任何图书馆参考资料。它们都是同一类的一部分,外加一个Main的附加文件。您已经重复了代码,因为它显示不起作用。@user2863749,是的,您没有写过它,但当我不正确地了解您的项目结构时(您没有提供足够的信息),这是可能的。另外,您没有在问题中提供Employee类。我编辑了我的答案,请再次检查。
{
SalesEmployee employee1 = new SalesEmployee("Alice", 1000, 500);
SalesEmployee salesEmpInstance = employee1 ;
decimal result = salesEmpInstance.CalculateExtraBonus();
}