【问题标题】:How to inherit from a class with a protected data type?如何从具有受保护数据类型的类继承?
【发布时间】:2016-10-20 10:56:01
【问题描述】:

我有一个带有内部受保护类的基本泛型类。如何从基类继承并访问受保护的内部类?

作为一个例子,这段代码不会编译:

unit uFoo;

interface

type

  TFoo<T> = class
  protected
    type
      TFooProtected = class

      end;
  end;

  TFoo2<T> = class(TFoo<T>)
  protected
    item: TFooProtected;
  end;

【问题讨论】:

  • 您需要完全限定类型:item: TFoo&lt;T&gt;.TFooPrivate;
  • 这个问题与泛型本身无关。它适用于在内部声明类型的任何类。

标签: delphi inheritance protected


【解决方案1】:

像这样:

type
  TFoo<T> = class
  protected
    type
      TFooProtected = class
      end;
  end;

  TFoo2<T> = class(TFoo<T>)
  protected
    item: TFoo<T>.TFooProtected;
  end;

请注意,这与泛型无关。它适用于在内部声明类型的任何类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-14
    • 2017-03-11
    • 1970-01-01
    • 2010-09-26
    • 2014-01-30
    • 1970-01-01
    • 2019-10-03
    • 2012-08-19
    相关资源
    最近更新 更多