我希望有人能够帮助我理解一段让我部分变成竹片的代码。它几乎是selenium loadablecomponent类的精确副本
https://github.com/SeleniumHQ/selenium/blob/master/dotnet/src/support/UI/LoadableComponent%7BT%7D.cs
class Program
{
static void Main(string[] args)
{
C newClass = new C();
newClass.Load().MethodInB();
Console.ReadLine();
}
}
public abstract class LoadableComponent<T> : ILoadableComponent where T : LoadableComponent<T> {
protected abstract void ExecuteLoad();
protected abstract bool EvaluateLoadedStatus();
protected virtual void HandleLoadError(WebDriverException ex)
{
}
public virtual string UnableToLoadMessage {
get;
set;
}
ILoadableComponent ILoadableComponent.Load()
{
return (ILoadableComponent)this.Load();
}
protected T TryLoad() {
try {
this.ExecuteLoad();
} catch (WebDriverException e) {
this.HandleLoadError(e);
}
return (T)this;
}
protected bool IsLoaded {
get {
bool isLoaded = false;
try
{
isLoaded = this.EvaluateLoadedStatus();
}
catch (WebDriverException)
{
return false;
}
return isLoaded;
}
}
public virtual T Load() {
if (this.IsLoaded) {
return (T)this;
} else {
this.TryLoad();
}
if (!this.IsLoaded) {
//throw new LoadableComponentException(this.UnableToLoadMessage);
}
return (T)this;
}
}
public class B : LoadableComponent<B> {
protected override void ExecuteLoad() {
//throw new NotImplementedException();
}
public void MethodInB() {
}
protected override bool EvaluateLoadedStatus() {
Console.WriteLine("I'm in B");
return true;
}
}
public class C : B {
protected override bool EvaluateLoadedStatus() {
Console.WriteLine("I'm in C");
return false;
}
public void MethodInC() {
}
}
我的第一个问题是这段代码在做什么?
ILoadableComponent ILoadableComponent.Load()
{
return (ILoadableComponent)this.Load();
}
它与我在相对较短的经历中看到的任何方法,属性或构造函数不同。它似乎正在运行/覆盖虚拟方法Load()
,但我无法确定。
第二个问题是我认为创建对象C的实例并运行Load()
方法将返回类型T等于C.而不是它似乎返回B,因此我的newClass.Load().MethodInB();
主要方法。
如果我创建了一个B类实例,并且如果我创建了一个C类实例,那么让Load()方法返回B需要进行哪些修改?
非常感谢,