Różnice między class a record ?

0

Witam serdecznie,
proszę o wskazanie jakie są różnice pomiędzy definicją record a class, bo wydaje się wygodniej tworzyć rekord i nie myśleć nad zwalnianiem pamięci jak to jest w przypadku obiektu danej klasy, koszt wiadomy wieksze zapotrzebowanie na pamięć ale czy są jakieś inne za i przeciw. Bardzo proszę o Wasze zdanie z uzasadnieniem co lepiej stosować;

Przykład:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TMyRecord = record
    a: integer;
    b: string;
    procedure Clear;
  end;

type
  TMyClass = class
    a: integer;
    b: string;
    procedure Clear;
  end;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    myrecord: TMyRecord;
    myclass: TMyClass;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TMyRecord.Clear;
begin
 a:=0;
 b:='';
end;

procedure TMyClass.Clear;
begin
 a:=0;
 b:='';
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 myrecord.a:=1;
 myrecord.b:=IntToStr(myrecord.a);
 ShowMessage(myrecord.b);
 myrecord.Clear;
 ShowMessage(myrecord.b);

 myclass:=TMyClass.Create;
 myclass.a:=2;
 myclass.b:=IntToStr(myclass.a);
 ShowMessage(myclass.b);
 myclass.Clear;
 ShowMessage(myclass.b);
 myclass.Free;
end;

end.
0

jak chcesz dziedziczyć po innym rekordzie, zaimplementować interfejs, albo użyć getterów/setterów czy modyfikatorów dostępu do pola/metody w rekordzie?
możliwość tworzenia "metod" w rekordach wydaje mi się pomyłką projektantów Delphi.

0

to ja jeszcze dodam, że nie ma konstruktorów/destruktorów

0

Dziękuję za odpowiedź ŁF,Misiekd,
możecie mi zaoprezentować na przykładzie który zamieściłem ja by wygladał

zaimplementować interfejs, albo użyć getterów/setterów czy modyfikatorów dostępu do pola/metody

Przepraszam za moje mało ambitne zapytania, jestem na etapie przyswajania wiedzy.
Pozdrawiam

0

Znalazłem w helpie Delphi 2007 taką wzmiankę:

Records (advanced)
In addition to the traditional record types, the Delphi language allows more complex and �class-like� record types. In addition to fields, records may have properties and methods (including constructors), class properties, class methods, class fields, and nested types. For more information on these subjects, see the documentation on Classes and Objects. Here is a sample record type definition with some �class-like� functionality.

type
  TMyRecord = record
    type
      TInnerColorType = Integer;
    var
      Red: Integer;
    class var
      Blue: Integer;
    procedure printRed();
    constructor Create(val: Integer);
    property RedProperty: TInnerColorType read Red write Red;
    class property BlueProp: TInnerColorType read Blue write Blue;
end;

constructor TMyRecord.Create(val: Integer);
begin
  Red := val;
end;

procedure TMyRecord.printRed;
begin
  writeln('Red: ', Red);
end;
Though records can now share much of the functionality of classes, there are some important differences between classes and records. 

Records do not support inheritance.
Records can contain variant parts; classes cannot.
Records are value types, so they are copied on assignment, passed by value, and allocated on the stack unless they are declared globally or explicitly allocated using the New and Dispose function. Classes are reference types, so they are not copied on assignment, they are passed by reference, and they are allocated on the heap.
Records allow operator overloading on the Win32 and .NET platforms; classes allow operator overloading only for .NET.
Records are constructed automatically, using a default no-argument constructor, but classes must be explicitly constructed. Because records have a default no-argument constructor, any user-defined record constructor must have one or more parameters.
Record types cannot have destructors.
Virtual methods (those specified with the virtual, dynamic, and message keywords) cannot be used in record types.
Unlike classes, record types on the Win32 platform cannot implement interfaces; however, records on the .NET platform can implement interfaces.

To już całkiem jestem zagubiony w tym natłkou informacji po części sprzecznymi ze sobą...

0

Tomasz:

Records are value types, so they are copied on assignment, passed by value, and allocated on the stack unless they are declared globally or explicitly allocated using the New and Dispose function. Classes are reference types, so they are not copied on assignment, they are passed by reference, and they are allocated on the heap.

To jest w zasadzie najważniejsza różnica.

0
Tomasz Zubień napisał(a)

To już całkiem jestem zagubiony w tym natłkou informacji po części sprzecznymi ze sobą...

Nie, po prostu jak widać nie wszyscy jesteśmy na bieżąco...

0

Metody wewnątrz rekordów można traktować jako „lukier syntaktyczny” dla procedur globalnych z rekordem jako pierwszym parametrem. I tak:

type TRekord=record
  ...
  procedure FooBar;
end;

procedure TRekord.FooBar;
begin
  ...
end;

będzie funkcjonalnie równoważne do „starego” zapisu

type TRekord=record
    ...
end;

procedure FooBar(self:TRecord);
begin
  ...
end;
0

A dla czego ja nie mogę procedury do recordu przypisać ? mam D7

type
  TMyRecord = record
    a: integer;
    b: string;
    procedure Clear;
  end;

Build
[Error] Unit1.pas(13): 'END' expected but 'PROCEDURE' found
[Error] Unit1.pas(14): 'IMPLEMENTATION' expected but ';' found
[Error] Unit1.pas(27): '.' expected but 'IMPLEMENTATION' found
[Error] Unit1.pas(13): Unsatisfied forward or external declaration: 'Clear'
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'

Ok. poczytałem :) dla zielonych cytuję:

Delphi 8 wprowadza pewne innowacje w zapisie rekordów. Dotychczas rekord mógł zawierać jedynie pola (ang. fields), czyli zmienne rekordowe. Teraz jest możliwe także dodawanie metod, co bardziej upodobnia rekordy do klas. Oczywiście, rekordy są prostszą strukturą niż klasy i nie mogą zwierać metod wirtualnych czy dynamicznych. Nie można dziedziczyć rekordów oraz przedefiniowywać metod.

0
lankusiek napisał(a)

mam D7
tu jest Twoja odpowiedź - metody w rekordach są od TD

1 użytkowników online, w tym zalogowanych: 0, gości: 1