Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi-属性能否从属性中读取?_Delphi_Properties - Fatal编程技术网

Delphi-属性能否从属性中读取?

Delphi-属性能否从属性中读取?,delphi,properties,Delphi,Properties,所以我加入这个论坛是因为我找不到一个简单问题的答案 我想声明一个只读属性,它应该从私有成员只读属性读取。这似乎行不通。我能绕过封锁线吗 以下是代码片段: property Mine: TMineType read mMine.MineType; 编辑:也许我应该澄清一下。mMine属于TMine类,它具有属性MineType。MineType是TMineType类型,并且是只读的。属性getter可以是以下两种类型之一: 田地,或 函数。 您的代码试图用属性实现getter,但该属性不

所以我加入这个论坛是因为我找不到一个简单问题的答案

我想声明一个只读属性,它应该从私有成员只读属性读取。这似乎行不通。我能绕过封锁线吗

以下是代码片段:

    property Mine: TMineType read mMine.MineType;

编辑:也许我应该澄清一下。mMine属于TMine类,它具有属性MineType。MineType是TMineType类型,并且是只读的。

属性getter可以是以下两种类型之一:

田地,或 函数。 您的代码试图用属性实现getter,但该属性不满足上述要求


文档包含所有详细信息:

属性getter可以是以下两种情况之一:

田地,或 函数。 您的代码试图用属性实现getter,但该属性不满足上述要求


文档包含所有详细信息:

在实践层面上,您可以执行以下操作:

function GetMineType : TMineType;
begin
  result := mMine.MineType;
end;

property Mine: TMineType read GetMineType;

如果将GetMineType函数声明为内联函数,它将不会生成任何代码。

在实际层面上,您可以执行以下操作:

function GetMineType : TMineType;
begin
  result := mMine.MineType;
end;

property Mine: TMineType read GetMineType;

如果您将GetMineType函数声明为内联函数,它将不会生成任何代码。

根据您在评论中提供的其他信息,这些信息应该从一开始就放在原始问题中。我相信我可以提供一个答案,希望能够为您所遇到的问题提供解决方案面向

在下面的代码中,我展示了如何通过使用属性转发或属性发布使父类属性通过子类访问

  //Base class we use
  TBaseClass = class(TObject)
  //Private section. Methods, fields and properties declared here are only visible from within classes declared in same unit
  //but not from classes or method declared in other units.
  private
    FPrivateField: Integer;
    function GetPrivateField: Integer;
    procedure SetPrivateField(AValue: Integer);
    //This property is making use of getter and setter methods in order to access FPrivateField
    property PrivateFieldProperty: Integer read GetPrivateField write SetPrivateField;
  //Strict private section. Methods, fields and properties declared here are only visible from descendant classes and their
  //methods but not from other classes and their methods even if they are declared in the same unit.
  strict private
    FStrictPrivateField: Integer;
    function GetStrictPrivateField: Integer;
    procedure SetStrictPrivateField(AValue: Integer);
  //Public section. Methods, Field and properties declared here are visible from any other class or methods regardless of
  //where they are declared
  public
    //This property is accessing its field directly
    property DirectPrivateFieldProperty: Integer read FPrivateField write FPrivateField;
    //This property is making use of getter and setter methods in order to access FStrictPrivateField
    property StrictPrivateFieldProperty: Integer read GetStrictPrivateField write SetStrictPrivateField;
  end;

  //Sub class is actually a descendant class from our base class
  TSubClass = class(TBaseClass)
  public
    //If your parent class already has declared certain property and you want to use that property in your descendant
    //class you can use so called "property forwarding" approach where you only specify the parents property name that
    //you want to make available to your descendant class. This approach only allows accessing fields that are declared
    //in parents class.
    //If you need to access field declared in descendant class then its property must directly access that field or use
    //getter and/or setter methods declared in descendant class. Such approach is called property overloading.
    property DirectPrivateFieldProperty;
    property PrivateFieldProperty;
    //If you want to limit the access level of a descendants class property (making it read only) you need to follow standard
    //property declaration guideline where you declare property type and the field to which it is accessing either directly or
    //by using getter or setter methods
    property ReadOnlyPrivateFieldProperty1: Integer read FPrivateField;
    property ReadOnlyPrivatefieldProperty2: Integer read GetPrivateField;
    //You can also declare forwarded descendant class property with different visibility of the one declared in parent class.
    //Such approach is usually referred as property publishing.
    property StrictPrivateFieldProperty;
  end;
注意:上述方法仅在从子类访问父类的属性或字段时有效,而不用于访问其他类的字段或属性

原因是属性只能直接访问其自身类或任何父类的字段

属性只能使用在其自己的类或任何父类中声明的getter和setter方法

我希望我的这个答案能让你更好地了解如何使用属性


现在,我想你可能会觉得这些属性有点困惑,但相信我,当你学会使用它们时,你会意识到它们实际上是多么强大。我必须承认,我仍在时不时地寻找新的方法在我的项目中使用它们。

根据您在评论中提供的其他信息,这些信息应该从一开始就放在原始问题本身中。我相信我可以提供一个答案,希望能够提供问题的解决方案你正在面对

在下面的代码中,我展示了如何通过使用属性转发或属性发布使父类属性通过子类访问

  //Base class we use
  TBaseClass = class(TObject)
  //Private section. Methods, fields and properties declared here are only visible from within classes declared in same unit
  //but not from classes or method declared in other units.
  private
    FPrivateField: Integer;
    function GetPrivateField: Integer;
    procedure SetPrivateField(AValue: Integer);
    //This property is making use of getter and setter methods in order to access FPrivateField
    property PrivateFieldProperty: Integer read GetPrivateField write SetPrivateField;
  //Strict private section. Methods, fields and properties declared here are only visible from descendant classes and their
  //methods but not from other classes and their methods even if they are declared in the same unit.
  strict private
    FStrictPrivateField: Integer;
    function GetStrictPrivateField: Integer;
    procedure SetStrictPrivateField(AValue: Integer);
  //Public section. Methods, Field and properties declared here are visible from any other class or methods regardless of
  //where they are declared
  public
    //This property is accessing its field directly
    property DirectPrivateFieldProperty: Integer read FPrivateField write FPrivateField;
    //This property is making use of getter and setter methods in order to access FStrictPrivateField
    property StrictPrivateFieldProperty: Integer read GetStrictPrivateField write SetStrictPrivateField;
  end;

  //Sub class is actually a descendant class from our base class
  TSubClass = class(TBaseClass)
  public
    //If your parent class already has declared certain property and you want to use that property in your descendant
    //class you can use so called "property forwarding" approach where you only specify the parents property name that
    //you want to make available to your descendant class. This approach only allows accessing fields that are declared
    //in parents class.
    //If you need to access field declared in descendant class then its property must directly access that field or use
    //getter and/or setter methods declared in descendant class. Such approach is called property overloading.
    property DirectPrivateFieldProperty;
    property PrivateFieldProperty;
    //If you want to limit the access level of a descendants class property (making it read only) you need to follow standard
    //property declaration guideline where you declare property type and the field to which it is accessing either directly or
    //by using getter or setter methods
    property ReadOnlyPrivateFieldProperty1: Integer read FPrivateField;
    property ReadOnlyPrivatefieldProperty2: Integer read GetPrivateField;
    //You can also declare forwarded descendant class property with different visibility of the one declared in parent class.
    //Such approach is usually referred as property publishing.
    property StrictPrivateFieldProperty;
  end;
注意:上述方法仅在从子类访问父类的属性或字段时有效,而不用于访问其他类的字段或属性

原因是属性只能直接访问其自身类或任何父类的字段

属性只能使用在其自己的类或任何父类中声明的getter和setter方法

我希望我的这个答案能让你更好地了解如何使用属性


现在,我想你可能会觉得这些属性有点困惑,但相信我,当你学会使用它们时,你会意识到它们实际上是多么强大。我必须承认,我仍在时不时地寻找新的方法在我的项目中使用它们。

如果编译器足够聪明,能够为这样一个getter自动生成代码,那该有多好。@Stefan。你有多少次真的需要这样的东西?很少,我怀疑。如果你练习封装,你经常会遇到这样的情况:你不是将整个对象公开,而是将特定的属性公开。因此,是的,在我的一生中,我已经编写了相当多的一行getter和setter,它们只是委托。如果编译器足够聪明,能够为这样的getter自动生成代码,那该有多好?@Stefan。你有多少次真的需要这样的东西?很少,我怀疑。如果你练习封装,你经常会遇到这样的情况:你不是将整个对象公开,而是将特定的属性公开。是的,在我的生活中,我写了很多单行getter和setter,它们只是委托。当你说private members property时,你指的是在同一个类中声明的属性还是在父类中声明的属性?@SilverWarrior我指的是在同一个类中声明的属性。但现在我已经知道我尝试做的是不可能的
如果该属性是在同一个类中声明的,为什么不简单地将可见性从private更改为public呢。还要记住,可以有多个属性访问同一字段。所以你可以有一个既能读又能写这个字段的私有属性,然后是一个只能读这个字段的公共属性。@SilverWarrior Super抱歉,我错了。该属性在另一个类中声明。不过,作为一个新手,我相信你的评论中有一些非常有价值的信息,所以谢谢你。我可能经常需要它。我已经提供了一个答案,其中包含一些关于使用类属性的更多信息,这些信息可能对您有用。当您说private members属性时,您指的是在同一个类中声明的属性还是在父类中声明的属性?@SilverWarrior我指的是在同一类中声明的属性。但是现在我已经了解到,如果没有一个手写的getter,我想做的事情是不可能的。如果该属性在同一个类中声明,为什么不简单地将可见性从private更改为public呢。还要记住,可以有多个属性访问同一字段。所以你可以有一个既能读又能写这个字段的私有属性,然后是一个只能读这个字段的公共属性。@SilverWarrior Super抱歉,我错了。该属性在另一个类中声明。不过,作为一个新手,我相信你的评论中有一些非常有价值的信息,所以谢谢你。我可能经常需要它。我提供了一个答案,其中包含了一些关于使用类属性的更多信息,这些信息可能对您有用。尽管您的答案看起来很简单,但我想不起来。这真的很有帮助。尽管你的答案看起来很简单,但我想不起来。这真的很有帮助。我想我已经掌握了我最初的问题所需要的所有信息,我想情况并非如此。对不起,我是新来的。你的回答毫无用处。正如您所猜测的,我仍然处于学习delphi的初级阶段,我很高兴我决定使用properties。现在我相信我对它们的工作原理有了更多的了解,我将尝试一下该属性。非常感谢您的时间。我想我已经掌握了我最初问题中需要的所有信息,我想情况并非如此。对不起,我是新来的。你的回答毫无用处。正如您所猜测的,我仍然处于学习delphi的初级阶段,我很高兴我决定使用properties。现在我相信我对它们的工作原理有了更多的了解,我将尝试一下该属性。非常感谢您抽出时间。