在 C# 中覆盖 类 并访问使用覆盖方法的虚拟方法
Overriding classes in c# and accessing a virtual method which uses an override method
我是第一次在 C# 中使用覆盖方法,我认为我已经很好地掌握了基础知识。但是我的方法有点复杂,有些调用方法在方法中。
我本质上是想通过找到一种使用基本 class 方法但调用基本 class 方法使用的覆盖方法来节省自己的代码编写。
这是我的问题的一个例子,因为我可能没有很好地解释它:
namespace exampleCode
{
public class BaseClass : AnotherClass
{
public static string OrderStorageContainer = "Deliverables";
public virtual string CustomerStorageContainer = "CustomerDocs";
public virtual string GetSomething(typeStorage st, string FileName)
{
if (String.IsNullOrWhiteSpace(FileName))
{
throw new ArgumentNullException("FileName", "The filename of the file should be supplied!");
}
var c = GetReference(GetName(st));
return c.ToString();
}
public virtual string GetName(typeStorage st)
{
switch (st)
{
case typeStorage.CustomerProvided:
return CustomerStorageContainer.ToLower();
case typeStorage.SystemGenerated:
return OrderStorageContainer.ToLower();
}
throw new InvalidOperationException("No container defined for storage type: " + st.ToString());
}
public virtual string GetReference()
{
//does stuff
}
}
public class DervivedClass : BaseClass
{
public static string QuoteStorageContainer = "QuoteDeliverables";
public override string CustomerStorageContainer = "QuoteCustomerDocs";
public override string GetSomething(typeStorage st, string FileName)
{
if (String.IsNullOrWhiteSpace(FileName))
{
throw new ArgumentNullException("FileName", "The filename of the file should be supplied!");
}
var c = GetReference(GetName(st));
return c.ToString();
}
public override string GetName(typeStorage st)
{
switch (st)
{
case typeStorage.CustomerProvided:
return CustomerStorageContainer.ToLower();
case typeStorage.SystemGenerated:
return QuoteStorageContainer.ToLower();
}
throw new InvalidOperationException("No container defined for storage type: " + st.ToString());
}
}
}
基本上,我希望 Derived class 使用覆盖 GetSomething 方法。但是,我希望它使用覆盖的 GetName()
方法的结果调用 Base Class GetReference()
方法。
这是正确的路线吗?我一直发现很难在网上找到类似的例子。
任何指点都会很棒!
我认为您的方向是正确的。您最好使用 base 和 this 来避免混淆并强制编译器使用您想要的实现。
您的实施似乎是正确的。在您不覆盖 DerivedClass 中的 GetReference() 方法之前,它将是正确的。
如果你想让var c = GetReference(GetName(st));
这一行总是调用baseclass,你应该小心写成base.GetReference(GetName(st))
我是第一次在 C# 中使用覆盖方法,我认为我已经很好地掌握了基础知识。但是我的方法有点复杂,有些调用方法在方法中。
我本质上是想通过找到一种使用基本 class 方法但调用基本 class 方法使用的覆盖方法来节省自己的代码编写。
这是我的问题的一个例子,因为我可能没有很好地解释它:
namespace exampleCode
{
public class BaseClass : AnotherClass
{
public static string OrderStorageContainer = "Deliverables";
public virtual string CustomerStorageContainer = "CustomerDocs";
public virtual string GetSomething(typeStorage st, string FileName)
{
if (String.IsNullOrWhiteSpace(FileName))
{
throw new ArgumentNullException("FileName", "The filename of the file should be supplied!");
}
var c = GetReference(GetName(st));
return c.ToString();
}
public virtual string GetName(typeStorage st)
{
switch (st)
{
case typeStorage.CustomerProvided:
return CustomerStorageContainer.ToLower();
case typeStorage.SystemGenerated:
return OrderStorageContainer.ToLower();
}
throw new InvalidOperationException("No container defined for storage type: " + st.ToString());
}
public virtual string GetReference()
{
//does stuff
}
}
public class DervivedClass : BaseClass
{
public static string QuoteStorageContainer = "QuoteDeliverables";
public override string CustomerStorageContainer = "QuoteCustomerDocs";
public override string GetSomething(typeStorage st, string FileName)
{
if (String.IsNullOrWhiteSpace(FileName))
{
throw new ArgumentNullException("FileName", "The filename of the file should be supplied!");
}
var c = GetReference(GetName(st));
return c.ToString();
}
public override string GetName(typeStorage st)
{
switch (st)
{
case typeStorage.CustomerProvided:
return CustomerStorageContainer.ToLower();
case typeStorage.SystemGenerated:
return QuoteStorageContainer.ToLower();
}
throw new InvalidOperationException("No container defined for storage type: " + st.ToString());
}
}
}
基本上,我希望 Derived class 使用覆盖 GetSomething 方法。但是,我希望它使用覆盖的 GetName()
方法的结果调用 Base Class GetReference()
方法。
这是正确的路线吗?我一直发现很难在网上找到类似的例子。
任何指点都会很棒!
我认为您的方向是正确的。您最好使用 base 和 this 来避免混淆并强制编译器使用您想要的实现。
您的实施似乎是正确的。在您不覆盖 DerivedClass 中的 GetReference() 方法之前,它将是正确的。
如果你想让var c = GetReference(GetName(st));
这一行总是调用baseclass,你应该小心写成base.GetReference(GetName(st))