扩展基类方法
我是 C# 新手,正在尝试理解基本概念。预先感谢您的帮助。我下面有一些示例类(在此窗口中键入,因此可能存在一些错误),并且有两个问题:
是否可以调用派生类方法来执行具有相同名称的基类方法中的代码,然后执行派生类方法中的代码?每个派生类都需要执行 RunCheck 的基类代码,然后执行特定于其类的专门代码。我可以在基类中将 RunCheck() 命名为其他名称,然后在调用派生类的 RunCheck() 时调用它,但我必须记住在派生类中的 RunCheck() 上调用它。
在 Program.cs 中,如果该字段不在我传入的派生类中,我想输出所有带有空白值的字段。我会传入什么?
这是我的代码:
class baseCheck
{
public DateTime StartTime { get; set; }
public DateTime LastRun { get; set; }
public int Runs { get; set; }
//Others
public void RunCheck()
{
if (Started != null)
started = DateTime.Now;
LastRun = DateTime.Now;
Runs++;
}
}
class FileCheck : baseCheck
{
public string FileName { get; set; }
public void RunCheck()
{
//I want all the code in the base class to run plus
//any code I put here when calling this class method
}
}
class DirectoryCheck : baseCheck
{
public string DirectoryName { get; set; }
public void RunCheck()
{
//I want all the code in the base class to run plus
//any code I put here when calling this class method
}
}
//Program.cs
static void Main()
{
//Create derived class - either DirectoryCheck or FileCheck
//depending on what the user chooses.
if (Console.ReadLine()=="F")
{
FileCheck c = new FileCheck();
}
else
{
DirectoryCheck c = new DirectoryCheck();
}
PrintOutput(c);
}
private void PrintOut(What do I put here?)
{
Console.WriteLine("Started: {0}",f.StartTime)
Console.WriteLine("Directory: {0}", f.DirectoryName)
Console.WriteLine("File: {0}", f.FileName}
}
I am new to C# and am trying to understand basic concepts. Thank you in advance for your help. I have some sample classes below (typed in this window so there may be some errors)and have two questions:
Is it possible to Call a derived class method that executes the code in the base class method with the same name, then executes the code in the derived class method? Every derived class will need to perform the base class code for the RunCheck then do specialized code specific to its class. I could name RunCheck() something else in the base class and then call it when I call the RunCheck() of the derived class but then I have to remember to call it on the RunCheck() in the derived class.
In the Program.cs I want to output all fields with a blank value if it is on a field that is not in the derived class I pass in. What would I pass in?
Here is my code:
class baseCheck
{
public DateTime StartTime { get; set; }
public DateTime LastRun { get; set; }
public int Runs { get; set; }
//Others
public void RunCheck()
{
if (Started != null)
started = DateTime.Now;
LastRun = DateTime.Now;
Runs++;
}
}
class FileCheck : baseCheck
{
public string FileName { get; set; }
public void RunCheck()
{
//I want all the code in the base class to run plus
//any code I put here when calling this class method
}
}
class DirectoryCheck : baseCheck
{
public string DirectoryName { get; set; }
public void RunCheck()
{
//I want all the code in the base class to run plus
//any code I put here when calling this class method
}
}
//Program.cs
static void Main()
{
//Create derived class - either DirectoryCheck or FileCheck
//depending on what the user chooses.
if (Console.ReadLine()=="F")
{
FileCheck c = new FileCheck();
}
else
{
DirectoryCheck c = new DirectoryCheck();
}
PrintOutput(c);
}
private void PrintOut(What do I put here?)
{
Console.WriteLine("Started: {0}",f.StartTime)
Console.WriteLine("Directory: {0}", f.DirectoryName)
Console.WriteLine("File: {0}", f.FileName}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需在
DirectoryCheck
类中调用base.RunCheck()
即可:此外,在当前的实现中,您还隐藏了基类
RunCheck()
方法 - 您应该真正覆盖它 - 这将基类中的方法签名更改为派生类中的方法签名,
我怀疑您真正想要的东西类似于 非虚拟接口模式 (NVI) - 在基类中公开受保护的虚拟方法,子类可以覆盖该方法,但在实际调用的基类上有一个公共方法该方法在内部 - 这种方法允许您在调用之前和之后扩展您正在做的事情。
在您的示例中,这将如下所示:
Just call
base.RunCheck()
in yourDirectoryCheck
class:Also with your current implementation you are hiding the base class
RunCheck()
method - you should really override it - this changes the method signature in the base class toand in the derived classes to
I suspect what you really want though is something like the Non Virtual interface pattern (NVI) - Expose a protected virtual method in your base class, that child classes can override, but have a public method on the base class that is actually calling that method internally - this approach allows you to extend what you are doing before and after that call.
In your example this would look like this:
在派生类中,您可以使用以下方式调用基类中的方法:
正如注释中提到的,基方法需要声明为
virtual
以允许重写:对于您的 PrintOut() 方法,没有什么神奇的方法,但是您可以让它以基类作为参数,然后测试类型。
或者您可以使用重载:
或者您可以将 PrintOut 方法作为类的一部分(甚至可以使用现有的 ToString() 方法)并根据需要重写它。
In a derived class, you can call the method in the base class using:
As mentioned in the comments, the base method will need to be declared as
virtual
to allow overriding:For your PrintOut() method, there is no magic way, but you could have it take the base class as a parameter, and then test for the type.
Or you could use overloads:
Or you could have your PrintOut method part of your class (maybe even use the existing
ToString()
method) and override it as required.