德尔福 - 可以从房产中读取房产吗?

时间:2015-11-24 12:11:14

标签: delphi properties

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

我想声明一个只读属性,它应该从私有成员读取只读属性。这似乎不会起作用。我能解决这个封锁吗?

以下是代码段:

    property Mine: TMineType read mMine.MineType;

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

3 个答案:

答案 0 :(得分:2)

属性getter可以是以下两种方法之一:

  • 字段,或
  • 一个功能。

您的代码尝试使用属性实现getter,但这不符合上述要求。

该文档包含所有详细信息:http://docwiki.embarcadero.com/RADStudio/en/Properties

答案 1 :(得分:2)

在实际层面上,你可以这样做:

{System.IO.DirectoryNotFoundException: Could not find a part of the path "/Users/builder/data/lanes/2185/53fce373/source/monodroid/builds/install/mono-   x86/etc/mono/2.1/machine.config".
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x001e2] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/corlib/System.IO/FileStream.cs:233 
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, System.String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) [0x00000] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/corlib/System.IO/FileStream.cs:152 
at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int,System.IO.FileOptions,string,bool,bool,bool)
at System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost) [0x00065] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/io/streamreader.cs:255 
at System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) [0x00000] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/io/streamreader.cs:236 
at System.IO.StreamReader..ctor (System.String path, Boolean detectEncodingFromByteOrderMarks) [0x00000] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/io/streamreader.cs:207 
 at System.IO.StreamReader..ctor (System.String path) [0x00000] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/io/streamreader.cs:184 
at (wrapper remoting-invoke-with-check) System.IO.StreamReader:.ctor (string)
at System.Runtime.Remoting.RemotingConfiguration.LoadDefaultDelayedChannels () [0x0002f] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/corlib/System.Runtime.Remoting/RemotingConfiguration.cs:147 
at System.Runtime.Remoting.Channels.ChannelServices.CreateClientChannelSinkChain (System.String url, System.Object remoteChannelData, System.String& objectUri) [0x00092] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/corlib/System.Runtime.Remoting.Channels/ChannelServices.cs:113 
 at System.Runtime.Remoting.RemotingServices.GetClientChannelSinkChain (System.String url, System.Object channelData, System.String& objectUri) [0x00000] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/corlib/System.Runtime.Remoting/RemotingServices.cs:683 
 at System.Runtime.Remoting.RemotingServices.GetOrCreateClientIdentity (System.Runtime.Remoting.ObjRef objRef, System.Type proxyType, System.Object& clientProxy) [0x0001d] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/corlib/System.Runtime.Remoting/RemotingServices.cs:639 
 at System.Runtime.Remoting.RemotingServices.GetRemoteObject (System.Runtime.Remoting.ObjRef objRef, System.Type proxyType) [0x00000] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/corlib/System.Runtime.Remoting/RemotingServices.cs:750 
 at System.Runtime.Remoting.RemotingServices.Connect (System.Type classToProxy, System.String url) [0x00009] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/corlib/System.Runtime.Remoting/RemotingServices.cs:193 
 at System.Activator.GetObject (System.Type type, System.String url) [0x00017] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/corlib/System/Activator.cs:350 
 at App1.MainActivity+<>c.b__0_0 (System.Object , System.EventArgs ) [0x00002] in C:\Users\User\Documents\GitHubVisualStudio\InHome\MainActivity.cs:34 }

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

答案 2 :(得分:0)

根据您在评论中提供的其他信息(此类信息应从一开始就将其放入原始问题中)我相信我可以提供答案,希望能够为您所面临的问题提供解决方案。< / p>

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

  //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方法。

我希望我的这个答案可以让你更好地了解与物业合作。

现在,我觉得属性可能看起来有点令人困惑,但相信我,当你学会与他们合作时,你会发现他们实际上有多么强大。我必须承认,我仍然在寻找新的方法在我的项目中不时使用它们。