C#无法覆盖继承的成员
我正在从一本名叫切格维登·格拉迪斯的书中学习C。我正在制作与书中所写相同的程序和代码。但有一个问题。我无法重写父类中的方法。从第一章开始,我已经完整地阅读了这本书,读了5遍,所有内容都是一样的,但我不明白为什么我不能重写父类中的方法。下面是基类PassFailActivity.cs中的代码C#无法覆盖继承的成员,c#,object,inheritance,base,C#,Object,Inheritance,Base,我正在从一本名叫切格维登·格拉迪斯的书中学习C。我正在制作与书中所写相同的程序和代码。但有一个问题。我无法重写父类中的方法。从第一章开始,我已经完整地阅读了这本书,读了5遍,所有内容都是一样的,但我不明白为什么我不能重写父类中的方法。下面是基类PassFailActivity.cs中的代码 using System; namespace ProtectedMembers { public class PassFailActivity : GradedActivity2 {
using System;
namespace ProtectedMembers
{
public class PassFailActivity : GradedActivity2
{
private double minPassingScore; // Minimum passing score
/// <summary>
/// The constructor sets the minimum passing score
/// </summary>
/// <param name="mps">The minimum passing score.</param>
public PassFailActivity(double mps)
{
minPassingScore = mps;
}
/// <summary>
/// The GetGrade method returns a letter grade determined
/// from the score field. This methos overrides the base class method.
/// </summary>
/// <returns>The letter grade</returns>
public override char GetGrade()
{
char letterGrade;
if (base.GetScore() >= minPassingScore)
letterGrade = 'P';
else
letterGrade = 'F';
return letterGrade;
}
}
}
输出应该是
How many questions are on the exam? 100
How many questions did the student miss? 25
What is the minimum passing score? 60
Each question counts 1 points.
The exam score is 75
The exam grade is P
我尝试将基方法设置为虚拟,并在尝试重写时调用override,这只会导致以下错误“'ProtectedMembers.PassFailActivity.GetGrade()”:无法重写继承的成员'ProtectedMembers.GradedActivity2.GetGrade()',因为它未标记为虚拟、抽象或重写”。我完全检查了错误是不言自明的,请在
GradedActivity2
中标记函数:
public virtual char GetGrade()
从MSDN:
virtual关键字用于修改方法、属性、索引器或
事件声明,并允许在派生
班级。例如,此方法可以被任何
继承它:
基类中的方法
GetGrade
应该告诉您允许使用以下关键字进行派生:
这只是一种故障保护机制,也是一种优化,因为不必在派生类中的派生方法上检查非虚拟方法。您需要在继承树的根类中使
GetGrade
虚拟。您需要尝试只包含与您的问题相关的代码,您似乎包含了所有代码,因此很难确定问题所在。很抱歉,我没有在MSDN上搜索它。显然,我不能说“+1 for错误是自解释的
”如果您理解关键字Virtual you will not day错误不是自解释的,它确实适用于任何想要快速参考的人如果错误是自解释的,prouser135不会问这个问题,我也不会用谷歌搜索它。哦,FSCK,这就像turboPascal一样,完全是Java的后盾™ 因此☹ 如果您需要覆盖系统行为,则非常恼人…
using System;
namespace ProtectedMembers
{
public class PassFailExamDemo
{
public static void Main111()
{
int questions, // Number of questions
missed; // Number of questions missed
double minPassing; // Minmum passing score
// Get the number of questions on the exam
Console.Write("How many questions are " + "on the exam? ");
questions = Convert.ToInt32(Console.ReadLine());
// Get the number of questions missed.
Console.Write("How many questions did " + "the student miss? ");
missed = Convert.ToInt32(Console.ReadLine());
// Get the minimum passing score
Console.Write("What is the minimum " + "passing score? ");
minPassing = Convert.ToInt32(Console.ReadLine());
// Create a PassFailExam project
PassFailExam exam = new PassFailExam(questions, missed, minPassing);
// Display the teset results.
Console.WriteLine("Each questions counts {0} points.",
exam.GetPointsEach());
Console.WriteLine("The exam score is {0} ",
exam.GetScore());
Console.WriteLine("The exam grade is {0} ",
exam.GetGrade());
Console.ReadLine();
}
}
}
How many questions are on the exam? 100
How many questions did the student miss? 25
What is the minimum passing score? 60
Each question counts 1 points.
The exam score is 75
The exam grade is P
public virtual char GetGrade()
public virtual char GetGrade()